39 lines
2.4 KiB
TypeScript
39 lines
2.4 KiB
TypeScript
|
|
// GET /api/note/files/open?fileUrl=/samples/… — ทางเปิดเอกสารตัวอย่างของหน้า /note/dev
|
||
|
|
// แบบ "A Link Get(new)" ของ /tmpl (คำขอผู้ใช้ 2026-10-30): ลิงก์เปล่า <Link> ยิง GET ได้เลย
|
||
|
|
// ไม่มี Bearer (browser navigation ใส่ header ไม่ได้) จึง**รับเฉพาะ path ในเครื่องที่ขึ้นต้น
|
||
|
|
// "/" เท่านั้น** — registerExternalFile ตรวจว่ามีไฟล์จริงใน public/ ผ่าน path guard เดิม
|
||
|
|
// (สร้างแถว url:… เท่านั้น — แถว ecourt: เกิดไม่ได้เพราะไม่รับ fileId และ path รีโมตโดนเด็ดขาด)
|
||
|
|
// แล้ว 302 เข้า viewer · ไฟล์ไม่ผ่าน = 302 /note?fileError=… ให้หน้าแรกแสดงสาเหตุ
|
||
|
|
import { NextResponse } from "next/server";
|
||
|
|
|
||
|
|
import { registerExternalFile } from "@/lib/external-import";
|
||
|
|
import { withBasePath } from "@/utils/base-path";
|
||
|
|
|
||
|
|
export const runtime = "nodejs";
|
||
|
|
export const dynamic = "force-dynamic";
|
||
|
|
|
||
|
|
export async function GET(request: Request): Promise<Response> {
|
||
|
|
const url = new URL(request.url);
|
||
|
|
// withBasePath("") = หน้าแรก /docedit/note · withBasePath("/annotate/x") = viewer —
|
||
|
|
// อย่าส่ง "/note" เข้า withBasePath (มันเติม app path ให้อีกชั้นเป็น /note/note — D11)
|
||
|
|
const redirectTo = (path: string, query = ""): NextResponse => {
|
||
|
|
const res = NextResponse.redirect(new URL(`${withBasePath(path)}${query}`, url), 302);
|
||
|
|
res.headers.set("cache-control", "no-store");
|
||
|
|
return res;
|
||
|
|
};
|
||
|
|
|
||
|
|
const fileUrl = url.searchParams.get("fileUrl") ?? "";
|
||
|
|
if (!fileUrl.startsWith("/") || fileUrl.includes("..") || fileUrl.length > 2048) {
|
||
|
|
return redirectTo(
|
||
|
|
"",
|
||
|
|
`?fileError=${encodeURIComponent("ลิงก์เปิดเอกสารตัวอย่างต้องเป็น path ในเครื่อง เช่น /samples/…")}`,
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
const result = await registerExternalFile(fileUrl, undefined, undefined, { trusted: false });
|
||
|
|
if (!result.ok) {
|
||
|
|
return redirectTo("", `?fileError=${encodeURIComponent(result.error)}`);
|
||
|
|
}
|
||
|
|
return redirectTo(`/annotate/${result.fileId}`);
|
||
|
|
}
|