Add new config use_path_based_img_url

Fix parseBitInt
This commit is contained in:
2024-06-02 13:33:46 +08:00
parent 329a91e9c0
commit 0ea6da38e4
8 changed files with 119 additions and 6 deletions

View File

@@ -12,6 +12,7 @@ import type { EhFileExtend } from "../../../server/files.ts";
import { User, UserPermission } from "../../../db.ts";
import { SortableURLSearchParams } from "../../../server/SortableURLSearchParams.ts";
import { isNumNaN, parseBigInt } from "../../../utils.ts";
import { extname } from "@std/path";
export const handler: Handlers = {
async GET(req, ctx) {
@@ -77,6 +78,12 @@ export const handler: Handlers = {
),
);
if (verify === null) {
if (m.cfg.use_path_based_img_url) {
const ext = extname(f.path);
return Response.redirect(
`${get_host(req)}/file/${tverify}/${f.id}${ext}`,
);
}
const b = new URLSearchParams();
b.append("verify", tverify);
return Response.redirect(

View File

@@ -167,6 +167,11 @@ export const handler: Handlers = {
);
const b = new URLSearchParams(bs.toString());
b.append("verify", tverify);
if (m.cfg.use_path_based_img_url) {
return Response.redirect(
`${get_host(req)}/thumbnail/${b}/${f.id}.jpg`,
);
}
return Response.redirect(
`${get_host(req)}/thumbnail/${f.id}?${b}`,
);

View File

@@ -21,10 +21,6 @@ export const handler: Handlers = {
const m = get_task_manager();
const b = m.cfg.thumbnail_dir;
await sure_dir(b);
const f = m.db.get_file(id);
if (!f) {
return new Response("File not found.", { status: 404 });
}
const u = new URL(req.url);
if (!m.cfg.img_verify_secret) {
return new Response("Can not verify.", { status: 400 });
@@ -57,6 +53,10 @@ export const handler: Handlers = {
) {
return new Response("params is missing", { status: 400 });
}
const f = m.db.get_file(id);
if (!f) {
return new Response("File not found.", { status: 404 });
}
const cfg: ThumbnailConfig = {
width,
height,

View File

@@ -0,0 +1,80 @@
import { Handlers } from "$fresh/server.ts";
import { exists } from "@std/fs/exists";
import { get_task_manager } from "../../../server.ts";
import { parse_int } from "../../../server/parse_form.ts";
import { generate_filename, ThumbnailConfig } from "../../../thumbnail/base.ts";
import { isNumNaN, parseBigInt, sure_dir } from "../../../utils.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";
import { SortableURLSearchParams } from "../../../server/SortableURLSearchParams.ts";
export const handler: Handlers = {
async GET(req, ctx) {
const id = parseBigInt(ctx.params.id);
if (isNumNaN(id)) {
return new Response("Bad Request", { status: 400 });
}
const m = get_task_manager();
const b = m.cfg.thumbnail_dir;
await sure_dir(b);
if (!m.cfg.img_verify_secret) {
return new Response("Can not verify.", { status: 400 });
}
// U2 存在将 & 错误的编码为 & 的BUG
const tmp = ctx.params.verify.replaceAll("&", "&");
const search = new URLSearchParams(tmp);
const verify = search.get("verify");
if (!verify) return new Response("Verify is needed.", { status: 400 });
const bs = new SortableURLSearchParams(tmp, ["verify"]);
const tverify = encode(
new Uint8Array(
await pbkdf2Hmac(
`${id}${bs.toString2()}`,
m.cfg.img_verify_secret,
1000,
64,
"SHA-512",
),
),
);
if (verify !== tverify) {
return new Response("verify is invalid.", { status: 400 });
}
const width = await parse_int(search.get("width"), null);
const height = await parse_int(search.get("height"), null);
const quality = await parse_int(search.get("quality"), null);
const method = await parse_int(search.get("method"), null);
const align = await parse_int(search.get("align"), null);
if (
width === null || height === null || quality === null ||
method === null || align === null
) {
return new Response("params is missing", { status: 400 });
}
const f = m.db.get_file(id);
if (!f) {
return new Response("File not found.", { status: 404 });
}
const cfg: ThumbnailConfig = {
width,
height,
quality,
method,
align,
};
const output = generate_filename(b, f, cfg);
if (!(await exists(output))) {
return new Response("file not exists.", { status: 500 });
}
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(output, opts);
},
};