From 9aa1bba2483194095bcf0fabaa4f8f7487566811 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Tue, 26 Mar 2024 14:44:12 +0800 Subject: [PATCH] Add a new way to generate verify links --- fresh.gen.ts | 2 ++ routes/file/[verify]/[id].ts | 47 ++++++++++++++++++++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 routes/file/[verify]/[id].ts diff --git a/fresh.gen.ts b/fresh.gen.ts index c41e87f..6bd3d9a 100644 --- a/fresh.gen.ts +++ b/fresh.gen.ts @@ -27,6 +27,7 @@ import * as $api_thumbnail_id_ from "./routes/api/thumbnail/[id].ts"; import * as $api_token from "./routes/api/token.ts"; import * as $api_user from "./routes/api/user.ts"; import * as $file_id_ from "./routes/file/[id].ts"; +import * as $file_verify_id_ from "./routes/file/[verify]/[id].ts"; import * as $file_middleware from "./routes/file/_middleware.ts"; import * as $index from "./routes/index.tsx"; import * as $manifest_json from "./routes/manifest.json.ts"; @@ -67,6 +68,7 @@ const manifest = { "./routes/api/token.ts": $api_token, "./routes/api/user.ts": $api_user, "./routes/file/[id].ts": $file_id_, + "./routes/file/[verify]/[id].ts": $file_verify_id_, "./routes/file/_middleware.ts": $file_middleware, "./routes/index.tsx": $index, "./routes/manifest.json.ts": $manifest_json, diff --git a/routes/file/[verify]/[id].ts b/routes/file/[verify]/[id].ts new file mode 100644 index 0000000..07f6309 --- /dev/null +++ b/routes/file/[verify]/[id].ts @@ -0,0 +1,47 @@ +import { Handlers } from "$fresh/server.ts"; +import { get_task_manager } from "../../../server.ts"; +import { + get_file_response, + GetFileResponseOptions, +} from "../../../server/get_file_response.ts"; +import pbkdf2Hmac from "pbkdf2-hmac"; +import { encodeBase64 as encode } from "std/encoding/base64.ts"; + +export const handler: Handlers = { + async GET(req, ctx) { + const id = parseInt(ctx.params.id); + if (isNaN(id)) { + return new Response("Bad Request", { status: 400 }); + } + const m = get_task_manager(); + if (!m.cfg.img_verify_secret) { + return new Response("Can not verify.", { status: 400 }); + } + const verify = decodeURIComponent(ctx.params.verify); + if (!verify) return new Response("Verify is needed.", { status: 400 }); + const tverify = encode( + new Uint8Array( + await pbkdf2Hmac( + `${id}`, + m.cfg.img_verify_secret, + 1000, + 64, + "SHA-512", + ), + ), + ); + if (verify !== tverify) { + return new Response("verify is invalid.", { status: 400 }); + } + const f = m.db.get_file(id); + if (!f) { + return new Response("File not found.", { status: 404 }); + } + const opts: GetFileResponseOptions = {}; + opts.cache_control = "public, no-transform, max-age=31536000"; + opts.range = req.headers.get("range"); + opts.if_modified_since = req.headers.get("If-Modified-Since"); + opts.if_unmodified_since = req.headers.get("If-Unmodified-Since"); + return await get_file_response(f.path, opts); + }, +};