diff --git a/routes/api/file/random.ts b/routes/api/file/random.ts index ebfd9de..38869cb 100644 --- a/routes/api/file/random.ts +++ b/routes/api/file/random.ts @@ -1,6 +1,7 @@ import { Handlers } from "$fresh/server.ts"; import { get_task_manager } from "../../../server.ts"; import { parse_bool } from "../../../server/parse_form.ts"; +import { get_host } from "../../../server/utils.ts"; export const handler: Handlers = { async GET(req, _ctx) { @@ -12,6 +13,6 @@ export const handler: Handlers = { const f = m.db.get_random_file(is_nsfw, is_ad); if (!f) return new Response("File not found.", { status: 404 }); const t = thumb ? "thumbnail" : "file"; - return Response.redirect(`${u.origin}/api/${t}/${f.id}`); + return Response.redirect(`${get_host(req)}/api/${t}/${f.id}`); }, }; diff --git a/routes/api/thumbnail/[id].ts b/routes/api/thumbnail/[id].ts index d62dcb6..90531a6 100644 --- a/routes/api/thumbnail/[id].ts +++ b/routes/api/thumbnail/[id].ts @@ -10,6 +10,7 @@ import { get_file_response, GetFileResponseOptions, } from "../../../server/get_file_response.ts"; +import { get_host } from "../../../server/utils.ts"; export const handler: Handlers = { async GET(req, ctx) { @@ -52,7 +53,7 @@ export const handler: Handlers = { } if (!force) { if (cfg.width > f.width || cfg.height > f.height) { - return Response.redirect(`${u.origin}/api/file/${f.id}`); + return Response.redirect(`${get_host(req)}/api/file/${f.id}`); } } const output = generate_filename(b, f, cfg); diff --git a/server/i18ns.ts b/server/i18ns.ts index 37e942d..edec77b 100644 --- a/server/i18ns.ts +++ b/server/i18ns.ts @@ -3,6 +3,7 @@ import { parse } from "std/jsonc/mod.ts"; import { join } from "std/path/mod.ts"; import { I18NMap } from "./i18n.ts"; import { pick } from "accept-language-parser/"; +import { get_host } from "./utils.ts"; const whole_maps = new Map(); const LANGUAGES = ["zh-cn"]; @@ -67,6 +68,6 @@ export function i18n_handle_request(req: Request) { params.append(p[0], p[1]); } } - return Response.redirect(`${u.origin}${u.pathname}?${params}`); + return Response.redirect(`${get_host(req)}${u.pathname}?${params}`); } } diff --git a/server/utils.ts b/server/utils.ts index e028c99..4c7a9f3 100644 --- a/server/utils.ts +++ b/server/utils.ts @@ -40,3 +40,9 @@ export function return_json(data: T, status = 200) { headers: { "Content-Type": "application/json" }, }); } + +export function get_host(req: Request) { + const u = new URL(req.url); + const proto = req.headers.get("X-Forwarded-Proto"); + return proto ? `${proto}://${u.host}` : u.origin; +}