Allow download gallery

This commit is contained in:
2024-08-11 22:01:33 +08:00
parent 271d8202de
commit 94feb24f72
2 changed files with 51 additions and 0 deletions

View File

@@ -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(
"<!DOCTYPE html>\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");

View File

@@ -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+$/) &&