2025-12-26 07:52:40 +08:00
|
|
|
import { NextResponse } from "next/server";
|
2026-01-17 10:12:53 +08:00
|
|
|
import { isConvexEnabled } from "@/lib/convex/enabled";
|
|
|
|
|
import { HttpError, requireAuthContext } from "@/lib/auth/authContext";
|
2025-12-26 07:52:40 +08:00
|
|
|
|
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
|
|
2026-01-21 18:21:10 +08:00
|
|
|
const base64UrlEncodeUtf8 = (input: string) => {
|
|
|
|
|
return Buffer.from(input, "utf8")
|
|
|
|
|
.toString("base64")
|
|
|
|
|
.replace(/\+/g, "-")
|
|
|
|
|
.replace(/\//g, "_")
|
|
|
|
|
.replace(/=+$/g, "");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getRequestOrigin = (request: Request) => {
|
|
|
|
|
const xfProto = request.headers.get("x-forwarded-proto")?.split(",")[0]?.trim();
|
|
|
|
|
const xfHost = request.headers.get("x-forwarded-host")?.split(",")[0]?.trim();
|
|
|
|
|
const host = xfHost || request.headers.get("host") || new URL(request.url).host;
|
|
|
|
|
const protocol = (xfProto || new URL(request.url).protocol.replace(/:$/, "")) + ":";
|
|
|
|
|
return `${protocol}//${host}`;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const isLocalHostname = (hostname: string) =>
|
|
|
|
|
hostname === "127.0.0.1" || hostname === "localhost" || hostname === "host.docker.internal";
|
|
|
|
|
|
|
|
|
|
const maybeProxyForBrowser = (request: Request, rawUrl: string) => {
|
|
|
|
|
const input = String(rawUrl || "").trim();
|
|
|
|
|
if (!input) return input;
|
|
|
|
|
if (input.startsWith("/api/onlyoffice/proxy")) return input;
|
|
|
|
|
try {
|
|
|
|
|
const u = new URL(input);
|
|
|
|
|
if (!isLocalHostname(u.hostname)) return input;
|
|
|
|
|
const origin = getRequestOrigin(request);
|
|
|
|
|
const proxy = new URL("/api/onlyoffice/proxy", origin);
|
|
|
|
|
proxy.searchParams.set("u", base64UrlEncodeUtf8(input));
|
|
|
|
|
return proxy.toString();
|
|
|
|
|
} catch {
|
|
|
|
|
return input;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2025-12-26 07:52:40 +08:00
|
|
|
export async function GET(request: Request) {
|
2026-01-17 10:12:53 +08:00
|
|
|
if (isConvexEnabled()) {
|
|
|
|
|
try {
|
2026-01-18 05:13:53 +08:00
|
|
|
await requireAuthContext();
|
2026-01-17 10:12:53 +08:00
|
|
|
} catch (err) {
|
|
|
|
|
if (err instanceof HttpError) {
|
|
|
|
|
return NextResponse.json({ error: "未登录" }, { status: err.status });
|
|
|
|
|
}
|
|
|
|
|
return NextResponse.json({ error: "未登录" }, { status: 401 });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { searchParams } = new URL(request.url);
|
|
|
|
|
const fileUrl = searchParams.get("fileUrl");
|
|
|
|
|
if (!fileUrl) {
|
|
|
|
|
return NextResponse.json({ error: "缺少 fileUrl" }, { status: 400 });
|
|
|
|
|
}
|
2026-01-18 19:01:31 +08:00
|
|
|
|
|
|
|
|
// 说明:Convex + MinIO 模式下,这个接口目前主要用于“外链文件”走 ONLYOFFICE 的场景。
|
|
|
|
|
// 由于外链本身已经是可访问的 URL,这里只做最小透传。
|
2026-01-21 18:21:10 +08:00
|
|
|
return NextResponse.json({ signedUrl: maybeProxyForBrowser(request, fileUrl) });
|
2026-01-17 10:12:53 +08:00
|
|
|
}
|
|
|
|
|
|
2026-01-18 19:01:31 +08:00
|
|
|
return NextResponse.json({ error: "当前仅支持 Convex 模式" }, { status: 501 });
|
2025-12-26 07:52:40 +08:00
|
|
|
}
|