Skip to content

Commit

Permalink
Add a new way to generate verify links
Browse files Browse the repository at this point in the history
  • Loading branch information
lifegpc committed Mar 26, 2024
1 parent 4bc3390 commit 9aa1bba
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
2 changes: 2 additions & 0 deletions fresh.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
Expand Down
47 changes: 47 additions & 0 deletions routes/file/[verify]/[id].ts
Original file line number Diff line number Diff line change
@@ -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);
},
};

0 comments on commit 9aa1bba

Please sign in to comment.