260623_01_ENH: 엑셀 다운로드를 제외한 기능 완성
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
import {NextRequest, NextResponse} from "next/server";
|
||||
import {cookies} from "next/headers";
|
||||
import {RouteContext} from "@/types/common";
|
||||
import {errorToResponse} from "@/lib/http-util";
|
||||
import {createBackApi} from "@/lib/server-api";
|
||||
import {PerformanceReview} from "@/types/gwcf";
|
||||
import {serverEnv} from "@/lib/env.server";
|
||||
import {CookieKeys} from "@/store/cookie/cookie-keys";
|
||||
|
||||
const reviewFileName = (prefix: string, performanceId: string, reviewType: string) =>
|
||||
`${prefix}-${performanceId}-${reviewType}`;
|
||||
@@ -12,14 +15,37 @@ export const GET = async (
|
||||
context: RouteContext
|
||||
) => {
|
||||
try {
|
||||
void req;
|
||||
const {slug = []} = await context.params;
|
||||
const [action, second, third] = slug;
|
||||
const [action, second, third, fourth] = slug;
|
||||
const backApi = await createBackApi();
|
||||
|
||||
if (action === "download" && second && third && slug.length === 3) {
|
||||
if (action === "template" && second === "download" && third && slug.length === 3) {
|
||||
const cookieStore = await cookies();
|
||||
const accessToken = cookieStore.get(CookieKeys.AuthToken)?.value;
|
||||
const response = await fetch(`${serverEnv.API_BACK_SERVER.replace(/\/+$/, "")}/review/template/download/${encodeURIComponent(third)}`, {
|
||||
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 headers = new Headers();
|
||||
headers.set("Content-Type", response.headers.get("content-type") ?? "application/octet-stream");
|
||||
const disposition = response.headers.get("content-disposition");
|
||||
if (disposition) headers.set("Content-Disposition", disposition);
|
||||
|
||||
return new NextResponse(result, {
|
||||
status: 200,
|
||||
headers,
|
||||
});
|
||||
}
|
||||
|
||||
if (action === "download" && second && third && fourth && slug.length === 4) {
|
||||
const result = await backApi.get<ArrayBuffer>(
|
||||
`/review/download/${encodeURIComponent(second)}/${encodeURIComponent(third)}`,
|
||||
`/review/download/${encodeURIComponent(second)}/${encodeURIComponent(third)}/${encodeURIComponent(fourth)}`,
|
||||
{ expectBinary: true }
|
||||
);
|
||||
|
||||
@@ -32,24 +58,24 @@ export const GET = async (
|
||||
});
|
||||
}
|
||||
|
||||
if (action === "pdf" && second && third && slug.length === 3) {
|
||||
if (action === "pdf" && second && third && fourth && slug.length === 4) {
|
||||
const result = await backApi.get<ArrayBuffer>(
|
||||
`/review/pdf/${encodeURIComponent(second)}/${encodeURIComponent(third)}`,
|
||||
`/review/pdf/${encodeURIComponent(second)}/${encodeURIComponent(third)}/${encodeURIComponent(fourth)}`,
|
||||
{ expectBinary: true }
|
||||
);
|
||||
|
||||
const disposition = req.nextUrl.searchParams.get("disposition") === "inline" ? "inline" : "attachment";
|
||||
return new NextResponse(result, {
|
||||
status: 200,
|
||||
headers: {
|
||||
"Content-Type": "application/pdf",
|
||||
"Content-Disposition": `attachment; filename="${reviewFileName("review-pdf", second, third)}.pdf"`,
|
||||
"Content-Disposition": `${disposition}; filename="${reviewFileName("review-pdf", second, third)}.pdf"`,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (action && second && slug.length === 2) {
|
||||
if (action && second && third && slug.length === 3) {
|
||||
const result = await backApi.get<PerformanceReview>(
|
||||
`/review/${encodeURIComponent(action)}/${encodeURIComponent(second)}`
|
||||
`/review/${encodeURIComponent(action)}/${encodeURIComponent(second)}/${encodeURIComponent(third)}`
|
||||
);
|
||||
return NextResponse.json(result, { status: 200 });
|
||||
}
|
||||
@@ -69,8 +95,8 @@ export const POST = async (
|
||||
const [action, second, third] = slug;
|
||||
const backApi = await createBackApi();
|
||||
|
||||
if (action === "start" && second && slug.length === 2) {
|
||||
await backApi.post<void>(`/review/start/${encodeURIComponent(second)}`);
|
||||
if (action === "start" && second && third && slug.length === 3) {
|
||||
await backApi.post<void>(`/review/start/${encodeURIComponent(second)}/${encodeURIComponent(third)}`);
|
||||
return new NextResponse(null, { status: 204 });
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user