From 94feb24f7204129240f5e77cd9b915618f06cb78 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Sun, 11 Aug 2024 22:01:33 +0800 Subject: [PATCH] Allow download gallery --- routes/_middleware.ts | 47 +++++++++++++++++++++++++++++++++++++++ routes/api/_middleware.ts | 4 ++++ 2 files changed, 51 insertions(+) diff --git a/routes/_middleware.ts b/routes/_middleware.ts index f63341d..56415d7 100644 --- a/routes/_middleware.ts +++ b/routes/_middleware.ts @@ -8,6 +8,9 @@ import { exists } from "@std/fs/exists"; import { get_task_manager } from "../server.ts"; import { build_sw } from "../server/build_sw.ts"; import { i18n_get_lang } from "../server/i18ns.ts"; +import { SharedTokenType } from "../db.ts"; +import { initDOMParser } from "../utils.ts"; +import { DOMParser } from "deno_dom/wasm-noinit"; const STATIC_FILES = ["/common.css", "/scrollBar.css", "/sw.js", "/sw.js.map"]; @@ -61,6 +64,50 @@ export async function handler(req: Request, ctx: FreshContext) { } } } + if ( + u.pathname.startsWith("/flutter/gallery/") && + u.searchParams.has("share") + ) { + const token = u.searchParams.get("share")!; + const st = m.db.get_shared_token(token); + const now = Date.now(); + if ( + st && st.type == SharedTokenType.Gallery && + (st.expired === null || st.expired.getTime() >= now) + ) { + const b = `/flutter/gallery/${st.info.gid}`; + const g = m.db.get_gmeta_by_gid(st.info.gid); + if ((u.pathname == b || u.pathname.startsWith(b + "/")) && g) { + const html = await Deno.readTextFile(p); + await initDOMParser(); + try { + const dom = (new DOMParser()).parseFromString( + html, + "text/html", + ); + const doc = dom.documentElement!; + const title = g.title; + const desc = g.title_jpn; + doc.querySelector("head title")!.innerText = title; + doc.querySelector( + 'meta[name="apple-mobile-web-app-title"]', + )?.setAttribute("content", title); + doc.querySelector('meta[name="description"]') + ?.setAttribute("content", desc); + return new Response( + "\n" + doc.outerHTML, + { + headers: { + "Content-Type": "text/html; charset=UTF-8", + }, + }, + ); + } catch (_) { + null; + } + } + } + } const opts: GetFileResponseOptions = {}; opts.range = req.headers.get("range"); opts.if_modified_since = req.headers.get("If-Modified-Since"); diff --git a/routes/api/_middleware.ts b/routes/api/_middleware.ts index cff3cc2..6ba5695 100644 --- a/routes/api/_middleware.ts +++ b/routes/api/_middleware.ts @@ -60,6 +60,10 @@ function handle_auth(req: Request, ctx: FreshContext) { if (u.pathname === "/api/tag/rows" && req.method === "GET") { return true; } + if ( + u.pathname === `/api/export/gallery/zip/${st.info.gid}` && + req.method === "GET" + ) return true; // Follow API need extra checks. if ( u.pathname.match(/^\/api\/file\/\d+$/) &&