184 lines
6.8 KiB
TypeScript
184 lines
6.8 KiB
TypeScript
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}`;
|
|
|
|
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
|
|
) => {
|
|
try {
|
|
const {slug = []} = await context.params;
|
|
const [action, second, third, fourth] = slug;
|
|
const backApi = await createBackApi();
|
|
|
|
if (action === "template" && second === "download" && third && fourth && slug.length === 4) {
|
|
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)}/${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 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 === "template" && second === "pdf" && third && fourth && slug.length === 4) {
|
|
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": replaceContentDispositionType(
|
|
response.headers.get("content-disposition"),
|
|
disposition,
|
|
`review-template-${third}-${fourth}.pdf`
|
|
),
|
|
},
|
|
});
|
|
}
|
|
|
|
if (action === "download" && second && third && fourth && slug.length === 4) {
|
|
const result = await backApi.get<ArrayBuffer>(
|
|
`/review/download/${encodeURIComponent(second)}/${encodeURIComponent(third)}/${encodeURIComponent(fourth)}`,
|
|
{ expectBinary: true }
|
|
);
|
|
|
|
return new NextResponse(result, {
|
|
status: 200,
|
|
headers: {
|
|
"Content-Type": "application/zip",
|
|
"Content-Disposition": `attachment; filename="${reviewFileName("review-original", second, third)}.zip"`,
|
|
},
|
|
});
|
|
}
|
|
|
|
if (action === "download" && second && third && slug.length === 3) {
|
|
const result = await backApi.get<ArrayBuffer>(
|
|
`/review/download/${encodeURIComponent(second)}/${encodeURIComponent(third)}`,
|
|
{ expectBinary: true }
|
|
);
|
|
|
|
return new NextResponse(result, {
|
|
status: 200,
|
|
headers: {
|
|
"Content-Type": "application/octet-stream",
|
|
"Content-Disposition": `attachment; filename="${reviewFileName("review-original", second, third)}"`,
|
|
},
|
|
});
|
|
}
|
|
|
|
if (action === "pdf" && second && third && fourth && slug.length === 4) {
|
|
const result = await backApi.get<ArrayBuffer>(
|
|
`/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": `${disposition}; filename="${reviewFileName("review-pdf", second, third)}.pdf"`,
|
|
},
|
|
});
|
|
}
|
|
|
|
if (action && second && third && slug.length === 3) {
|
|
const result = await backApi.get<PerformanceReview>(
|
|
`/review/${encodeURIComponent(action)}/${encodeURIComponent(second)}/${encodeURIComponent(third)}`
|
|
);
|
|
return NextResponse.json(result, { status: 200 });
|
|
}
|
|
|
|
return NextResponse.json({ message: "Not found" }, { status: 404 });
|
|
} catch (error: unknown) {
|
|
return errorToResponse(error);
|
|
}
|
|
};
|
|
|
|
export const POST = async (
|
|
req: NextRequest,
|
|
context: RouteContext
|
|
) => {
|
|
try {
|
|
const {slug = []} = await context.params;
|
|
const [action, second, third] = slug;
|
|
const backApi = await createBackApi();
|
|
|
|
if (action === "start" && second && third && slug.length === 3) {
|
|
await backApi.post<void>(`/review/start/${encodeURIComponent(second)}/${encodeURIComponent(third)}`);
|
|
return new NextResponse(null, { status: 204 });
|
|
}
|
|
|
|
if (action === "end" && second && third && slug.length === 3) {
|
|
await backApi.post<void>(`/review/end/${encodeURIComponent(second)}/${encodeURIComponent(third)}`);
|
|
return new NextResponse(null, { status: 204 });
|
|
}
|
|
|
|
if (action && second && slug.length === 2) {
|
|
const formData = await req.formData();
|
|
const file = formData.get("file") as File | null;
|
|
|
|
if (!file) {
|
|
return NextResponse.json({ message: "file field is required." }, { status: 400 });
|
|
}
|
|
|
|
const backendForm = new FormData();
|
|
backendForm.append("file", file, file.name);
|
|
|
|
const result = await backApi.postForm<PerformanceReview>(
|
|
`/review/${encodeURIComponent(action)}/${encodeURIComponent(second)}`,
|
|
backendForm
|
|
);
|
|
return NextResponse.json(result, { status: 200 });
|
|
}
|
|
|
|
return NextResponse.json({ message: "Not found" }, { status: 404 });
|
|
} catch (error: unknown) {
|
|
return errorToResponse(error);
|
|
}
|
|
};
|