260629_01_ENH: PDF 미리보기 파일명 노출될 수 있도록 수정 및 KON용 code 조회 추가
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
import {headers} from "next/headers";
|
||||
import {getFileNameFromContentDisposition} from "@/lib/gwcf/file";
|
||||
|
||||
type FilePreviewPageProps = {
|
||||
searchParams: Promise<{
|
||||
src?: string;
|
||||
@@ -22,17 +25,49 @@ const isAllowedSrc = (src: string) =>
|
||||
src.startsWith("/api/review/template/pdf/") ||
|
||||
src.startsWith("/api/file/");
|
||||
|
||||
const getAbsoluteSrc = async (src: string) => {
|
||||
const headersList = await headers();
|
||||
const host = headersList.get("x-forwarded-host") ?? headersList.get("host");
|
||||
if (!host) return undefined;
|
||||
|
||||
const protocol = headersList.get("x-forwarded-proto") ?? "http";
|
||||
return new URL(src, `${protocol}://${host}`).toString();
|
||||
};
|
||||
|
||||
const getPdfTitleFromSrc = async (src: string) => {
|
||||
if (!isAllowedSrc(src)) return undefined;
|
||||
|
||||
try {
|
||||
const headersList = await headers();
|
||||
const absoluteSrc = await getAbsoluteSrc(src);
|
||||
if (!absoluteSrc) return undefined;
|
||||
|
||||
const response = await fetch(absoluteSrc, {
|
||||
headers: {
|
||||
cookie: headersList.get("cookie") ?? "",
|
||||
},
|
||||
cache: "no-store",
|
||||
});
|
||||
const fileName = getFileNameFromContentDisposition(response.headers.get("content-disposition"));
|
||||
await response.body?.cancel().catch(() => undefined);
|
||||
return fileName;
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
||||
export default async function FilePreviewPage({ searchParams }: FilePreviewPageProps) {
|
||||
const params = await searchParams;
|
||||
const title = params.title ?? "파일 미리보기";
|
||||
const images = parseImages(params.images);
|
||||
const src = params.src ?? "";
|
||||
const type = params.type ?? (images.length > 0 ? "image" : "pdf");
|
||||
const src = params.src ?? "";
|
||||
const title = params.title ?? "파일 미리보기";
|
||||
const headerTitle = type === "pdf" ? await getPdfTitleFromSrc(src) : title;
|
||||
|
||||
return (
|
||||
<main className="flex min-h-screen flex-col bg-background text-foreground">
|
||||
<header className="flex h-14 shrink-0 items-center justify-between border-b border-border bg-card px-4">
|
||||
<h1 className="truncate text-base font-medium text-foreground">{title}</h1>
|
||||
<h1 className="truncate text-base font-medium text-foreground">{headerTitle}</h1>
|
||||
</header>
|
||||
|
||||
{type === "image" ? (
|
||||
@@ -44,7 +79,7 @@ export default async function FilePreviewPage({ searchParams }: FilePreviewPageP
|
||||
>
|
||||
<img
|
||||
src={imageSrc}
|
||||
alt={`${title} ${index + 1}`}
|
||||
alt={`${headerTitle} ${index + 1}`}
|
||||
className="mx-auto max-h-[80vh] rounded border border-border bg-white object-contain"
|
||||
/>
|
||||
<figcaption className="mt-2 truncate text-xs text-muted-foreground">
|
||||
@@ -59,7 +94,7 @@ export default async function FilePreviewPage({ searchParams }: FilePreviewPageP
|
||||
)}
|
||||
</div>
|
||||
) : isAllowedSrc(src) ? (
|
||||
<iframe src={src} title={title} className="min-h-0 flex-1 bg-white" />
|
||||
<iframe src={src} title={headerTitle} className="min-h-0 flex-1 bg-white" />
|
||||
) : (
|
||||
<div className="flex flex-1 items-center justify-center text-sm text-muted-foreground">
|
||||
미리보기 경로가 올바르지 않습니다.
|
||||
|
||||
@@ -2,7 +2,7 @@ import {NextRequest, NextResponse} from "next/server";
|
||||
import {RouteContext} from "@/types/common";
|
||||
import {errorToResponse} from "@/lib/http-util";
|
||||
import {createBackApi} from "@/lib/server-api";
|
||||
import {PerformanceCompact, PerformanceFull} from "@/types/gwcf";
|
||||
import {Code, PerformanceCompact, PerformanceFull} from "@/types/gwcf";
|
||||
|
||||
const ALLOWED_ORIGIN = "https://kon.kric.com";
|
||||
|
||||
@@ -66,6 +66,11 @@ export const POST = async (
|
||||
const [action] = slug;
|
||||
const backApi = await createBackApi();
|
||||
|
||||
if(action === "code" && slug.length === 2) {
|
||||
const result = await backApi.get<Code[]>(`/code/${encodeURIComponent(slug[1])}`)
|
||||
return withCors(NextResponse.json(result, { status: 200 }));
|
||||
}
|
||||
|
||||
if(action === "assign-confirm" && slug.length === 3) {
|
||||
const result = await backApi.post<string>(
|
||||
`/assign/confirm/${encodeURIComponent(slug[1])}/${encodeURIComponent(slug[2])}`
|
||||
|
||||
@@ -10,6 +10,17 @@ import {CookieKeys} from "@/store/cookie/cookie-keys";
|
||||
const reviewFileName = (prefix: string, performanceId: string, reviewType: string) =>
|
||||
`${prefix}-${performanceId}-${reviewType}`;
|
||||
|
||||
const replaceContentDispositionType = (
|
||||
contentDisposition: string | null,
|
||||
disposition: "inline" | "attachment",
|
||||
fallbackFileName: string
|
||||
) => {
|
||||
const fileNamePart = contentDisposition?.split(";").slice(1).join(";");
|
||||
return fileNamePart
|
||||
? `${disposition};${fileNamePart}`
|
||||
: `${disposition}; filename="${fallbackFileName}"`;
|
||||
};
|
||||
|
||||
export const GET = async (
|
||||
req: NextRequest,
|
||||
context: RouteContext
|
||||
@@ -44,16 +55,28 @@ export const GET = async (
|
||||
}
|
||||
|
||||
if (action === "template" && second === "pdf" && third && fourth && slug.length === 4) {
|
||||
const result = await backApi.get<ArrayBuffer>(
|
||||
`/review/template/pdf/${encodeURIComponent(third)}/${encodeURIComponent(fourth)}`,
|
||||
{ expectBinary: true }
|
||||
);
|
||||
const cookieStore = await cookies();
|
||||
const accessToken = cookieStore.get(CookieKeys.AuthToken)?.value;
|
||||
const response = await fetch(`${serverEnv.API_BACK_SERVER.replace(/\/+$/, "")}/review/template/pdf/${encodeURIComponent(third)}/${encodeURIComponent(fourth)}`, {
|
||||
headers: accessToken ? {authorization: `Bearer ${accessToken}`} : undefined,
|
||||
cache: "no-store",
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
return new NextResponse(await response.text().catch(() => ""), {status: response.status});
|
||||
}
|
||||
|
||||
const result = await response.arrayBuffer();
|
||||
const disposition = req.nextUrl.searchParams.get("disposition") === "inline" ? "inline" : "attachment";
|
||||
return new NextResponse(result, {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": "application/pdf",
|
||||
"Content-Disposition": `${disposition}; filename="review-template-${third}-${fourth}.pdf"`,
|
||||
"Content-Disposition": replaceContentDispositionType(
|
||||
response.headers.get("content-disposition"),
|
||||
disposition,
|
||||
`review-template-${third}-${fourth}.pdf`
|
||||
),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user