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 = {
|
type FilePreviewPageProps = {
|
||||||
searchParams: Promise<{
|
searchParams: Promise<{
|
||||||
src?: string;
|
src?: string;
|
||||||
@@ -22,17 +25,49 @@ const isAllowedSrc = (src: string) =>
|
|||||||
src.startsWith("/api/review/template/pdf/") ||
|
src.startsWith("/api/review/template/pdf/") ||
|
||||||
src.startsWith("/api/file/");
|
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) {
|
export default async function FilePreviewPage({ searchParams }: FilePreviewPageProps) {
|
||||||
const params = await searchParams;
|
const params = await searchParams;
|
||||||
const title = params.title ?? "파일 미리보기";
|
|
||||||
const images = parseImages(params.images);
|
const images = parseImages(params.images);
|
||||||
const src = params.src ?? "";
|
|
||||||
const type = params.type ?? (images.length > 0 ? "image" : "pdf");
|
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 (
|
return (
|
||||||
<main className="flex min-h-screen flex-col bg-background text-foreground">
|
<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">
|
<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>
|
</header>
|
||||||
|
|
||||||
{type === "image" ? (
|
{type === "image" ? (
|
||||||
@@ -44,7 +79,7 @@ export default async function FilePreviewPage({ searchParams }: FilePreviewPageP
|
|||||||
>
|
>
|
||||||
<img
|
<img
|
||||||
src={imageSrc}
|
src={imageSrc}
|
||||||
alt={`${title} ${index + 1}`}
|
alt={`${headerTitle} ${index + 1}`}
|
||||||
className="mx-auto max-h-[80vh] rounded border border-border bg-white object-contain"
|
className="mx-auto max-h-[80vh] rounded border border-border bg-white object-contain"
|
||||||
/>
|
/>
|
||||||
<figcaption className="mt-2 truncate text-xs text-muted-foreground">
|
<figcaption className="mt-2 truncate text-xs text-muted-foreground">
|
||||||
@@ -59,7 +94,7 @@ export default async function FilePreviewPage({ searchParams }: FilePreviewPageP
|
|||||||
)}
|
)}
|
||||||
</div>
|
</div>
|
||||||
) : isAllowedSrc(src) ? (
|
) : 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">
|
<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 {RouteContext} from "@/types/common";
|
||||||
import {errorToResponse} from "@/lib/http-util";
|
import {errorToResponse} from "@/lib/http-util";
|
||||||
import {createBackApi} from "@/lib/server-api";
|
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";
|
const ALLOWED_ORIGIN = "https://kon.kric.com";
|
||||||
|
|
||||||
@@ -66,6 +66,11 @@ export const POST = async (
|
|||||||
const [action] = slug;
|
const [action] = slug;
|
||||||
const backApi = await createBackApi();
|
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) {
|
if(action === "assign-confirm" && slug.length === 3) {
|
||||||
const result = await backApi.post<string>(
|
const result = await backApi.post<string>(
|
||||||
`/assign/confirm/${encodeURIComponent(slug[1])}/${encodeURIComponent(slug[2])}`
|
`/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) =>
|
const reviewFileName = (prefix: string, performanceId: string, reviewType: string) =>
|
||||||
`${prefix}-${performanceId}-${reviewType}`;
|
`${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 (
|
export const GET = async (
|
||||||
req: NextRequest,
|
req: NextRequest,
|
||||||
context: RouteContext
|
context: RouteContext
|
||||||
@@ -44,16 +55,28 @@ export const GET = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (action === "template" && second === "pdf" && third && fourth && slug.length === 4) {
|
if (action === "template" && second === "pdf" && third && fourth && slug.length === 4) {
|
||||||
const result = await backApi.get<ArrayBuffer>(
|
const cookieStore = await cookies();
|
||||||
`/review/template/pdf/${encodeURIComponent(third)}/${encodeURIComponent(fourth)}`,
|
const accessToken = cookieStore.get(CookieKeys.AuthToken)?.value;
|
||||||
{ expectBinary: true }
|
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";
|
const disposition = req.nextUrl.searchParams.get("disposition") === "inline" ? "inline" : "attachment";
|
||||||
return new NextResponse(result, {
|
return new NextResponse(result, {
|
||||||
status: 200,
|
status: 200,
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/pdf",
|
"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`
|
||||||
|
),
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -195,11 +195,26 @@ export function MonitoringPerformanceDetailDialog({
|
|||||||
|
|
||||||
const reviewType = row?.reviewType ?? 1;
|
const reviewType = row?.reviewType ?? 1;
|
||||||
const templateClassification = Number(String(performance?.id ?? 0).charAt(0));
|
const templateClassification = Number(String(performance?.id ?? 0).charAt(0));
|
||||||
const downloadTemplate = () => downloadFromUrl(`/api/review/template/download/${reviewType}/${templateClassification}`);
|
|
||||||
const previewTemplate = () => {
|
const buildTemplateDownloadUrl = (classification: number) => `/api/review/template/download/${reviewType}/${classification}`;
|
||||||
openPreviewWindow(buildReviewTemplatePdfPreviewUrl(reviewType, templateClassification, `review-template-${reviewType}-${templateClassification}.pdf`));
|
const downloadTemplate = () => downloadFromUrl(buildTemplateDownloadUrl(templateClassification));
|
||||||
|
|
||||||
|
const previewTemplateFile = async (classification: number) => {
|
||||||
|
const previewWindow = window.open("about:blank", "_blank");
|
||||||
|
if (previewWindow) previewWindow.opener = null;
|
||||||
|
const previewUrl = buildReviewTemplatePdfPreviewUrl(reviewType, classification);
|
||||||
|
|
||||||
|
if (previewWindow) {
|
||||||
|
previewWindow.location.href = previewUrl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
openPreviewWindow(previewUrl);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const previewTemplate = () => void previewTemplateFile(templateClassification);
|
||||||
|
const previewEduFile = () => void previewTemplateFile(99);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Dialog open={open} onOpenChange={onOpenChange}>
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
||||||
<DialogContent className="flex max-h-[92vh] w-[calc(100vw-24px)] max-w-[430px] flex-col overflow-hidden p-0">
|
<DialogContent className="flex max-h-[92vh] w-[calc(100vw-24px)] max-w-[430px] flex-col overflow-hidden p-0">
|
||||||
@@ -229,13 +244,14 @@ export function MonitoringPerformanceDetailDialog({
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
<section className="space-y-3 rounded-md border p-3">
|
<section className="space-y-3 rounded-md border p-3">
|
||||||
<div className="flex items-center justify-between gap-2">
|
<div className="flex items-start justify-between gap-3">
|
||||||
<div className="font-semibold">나의 진행 상태</div>
|
<div className="shrink-0 font-semibold leading-6">나의 진행 상태</div>
|
||||||
<div className="flex items-center gap-2">
|
|
||||||
<ActionButton icon="preview" onClick={previewTemplate} className="min-h-5">양식</ActionButton>
|
|
||||||
<ActionButton icon="download" onClick={downloadTemplate} className="min-h-5">양식</ActionButton>
|
|
||||||
<StatusPill variant={status.variant}>{status.label}</StatusPill>
|
<StatusPill variant={status.variant}>{status.label}</StatusPill>
|
||||||
</div>
|
</div>
|
||||||
|
<div className="flex items-center justify-end gap-2">
|
||||||
|
<ActionButton icon="preview" onClick={previewEduFile} className="h-8 min-h-0 shrink-0 px-2 text-xs">교육자료</ActionButton>
|
||||||
|
<ActionButton icon="preview" onClick={previewTemplate} className="h-8 min-h-0 shrink-0 px-2 text-xs">양식</ActionButton>
|
||||||
|
<ActionButton icon="download" onClick={downloadTemplate} className="h-8 min-h-0 shrink-0 px-2 text-xs">양식 다운로드</ActionButton>
|
||||||
</div>
|
</div>
|
||||||
{row ? (
|
{row ? (
|
||||||
<MonitoringReviewActions
|
<MonitoringReviewActions
|
||||||
|
|||||||
@@ -6,6 +6,41 @@ const REVIEW_TIMESTAMP_PREFIX = /^\d{4}-\d{2}-\d{2}_\d{6}_/;
|
|||||||
export const removeTimestampPrefix = (fileName?: string) =>
|
export const removeTimestampPrefix = (fileName?: string) =>
|
||||||
fileName?.replace(REVIEW_TIMESTAMP_PREFIX, "");
|
fileName?.replace(REVIEW_TIMESTAMP_PREFIX, "");
|
||||||
|
|
||||||
|
const trimDispositionValue = (value: string) => value.trim().replace(/^"|"$/g, "");
|
||||||
|
|
||||||
|
const decodeDispositionValue = (value: string) => {
|
||||||
|
const trimmed = trimDispositionValue(value);
|
||||||
|
const encodedValue = trimmed.includes("''") ? trimmed.split("''").slice(1).join("''") : trimmed;
|
||||||
|
try {
|
||||||
|
return decodeURIComponent(encodedValue);
|
||||||
|
} catch {
|
||||||
|
return encodedValue;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const getDispositionParams = (contentDisposition: string) =>
|
||||||
|
contentDisposition
|
||||||
|
.split(";")
|
||||||
|
.slice(1)
|
||||||
|
.map((part) => part.trim())
|
||||||
|
.reduce<Record<string, string>>((params, part) => {
|
||||||
|
const separatorIndex = part.indexOf("=");
|
||||||
|
if (separatorIndex < 0) return params;
|
||||||
|
|
||||||
|
const key = part.slice(0, separatorIndex).trim().toLowerCase();
|
||||||
|
const value = part.slice(separatorIndex + 1).trim();
|
||||||
|
if (key && value) params[key] = value;
|
||||||
|
return params;
|
||||||
|
}, {});
|
||||||
|
|
||||||
|
export const getFileNameFromContentDisposition = (contentDisposition?: string | null) => {
|
||||||
|
if (!contentDisposition) return undefined;
|
||||||
|
|
||||||
|
const params = getDispositionParams(contentDisposition);
|
||||||
|
const fileName = params["filename*"] || params.filename;
|
||||||
|
return fileName ? decodeDispositionValue(fileName) : undefined;
|
||||||
|
};
|
||||||
|
|
||||||
export const downloadBlob = (
|
export const downloadBlob = (
|
||||||
buffer: ArrayBuffer,
|
buffer: ArrayBuffer,
|
||||||
fileName: string,
|
fileName: string,
|
||||||
@@ -49,7 +84,7 @@ export const buildReviewPdfPreviewUrl = (
|
|||||||
title?: string
|
title?: string
|
||||||
) => {
|
) => {
|
||||||
const src = `/api/review/pdf/${performanceId}/${reviewType}/${encodeURIComponent(userId)}?disposition=inline`;
|
const src = `/api/review/pdf/${performanceId}/${reviewType}/${encodeURIComponent(userId)}?disposition=inline`;
|
||||||
return `/file-preview?type=pdf&src=${encodeURIComponent(src)}&title=${encodeURIComponent(title || getReviewPdfDownloadName(performanceId, reviewType))}`;
|
return `/file-preview?type=pdf&src=${encodeURIComponent(src)}${title ? `&title=${encodeURIComponent(title)}` : ""}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const buildReviewTemplatePdfPreviewUrl = (
|
export const buildReviewTemplatePdfPreviewUrl = (
|
||||||
@@ -58,7 +93,7 @@ export const buildReviewTemplatePdfPreviewUrl = (
|
|||||||
title?: string
|
title?: string
|
||||||
) => {
|
) => {
|
||||||
const src = `/api/review/template/pdf/${reviewType}/${classification}?disposition=inline`;
|
const src = `/api/review/template/pdf/${reviewType}/${classification}?disposition=inline`;
|
||||||
return `/file-preview?type=pdf&src=${encodeURIComponent(src)}&title=${encodeURIComponent(title || `review-template-${reviewType}-${classification}.pdf`)}`;
|
return `/file-preview?type=pdf&src=${encodeURIComponent(src)}${title ? `&title=${encodeURIComponent(title)}` : ""}`;
|
||||||
};
|
};
|
||||||
|
|
||||||
export const buildEvidenceImageSrc = (
|
export const buildEvidenceImageSrc = (
|
||||||
|
|||||||
Reference in New Issue
Block a user