261021_01_ENH: 1차 마이그레이션 완료
This commit is contained in:
43
src/app/api/import/[[...slug]]/route.ts
Normal file
43
src/app/api/import/[[...slug]]/route.ts
Normal file
@@ -0,0 +1,43 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { cookies } from "next/headers";
|
||||
import { errorToResponse } from "@kri/common/http";
|
||||
import { createServerApi } from "@/lib/http-server";
|
||||
import { isImportPath } from "@/lib/project-proxy-policy";
|
||||
import { readImportResult } from "@/types/export";
|
||||
import { CookieKeys } from "@/store/cookie/cookie-keys";
|
||||
import type { RouteContext } from "@/types/common";
|
||||
|
||||
// Imports replace project data on the server and may run for minutes.
|
||||
export const maxDuration = 660;
|
||||
|
||||
/** Multipart upload of one `file` part, re-sent with a FormData the fetch layer encodes. */
|
||||
export async function POST(request: NextRequest, context: RouteContext) {
|
||||
const { slug = [] } = await context.params;
|
||||
if (!isImportPath(slug))
|
||||
return NextResponse.json({ message: "지원하지 않는 경로입니다." }, { status: 404 });
|
||||
if (!(await cookies()).get(CookieKeys.AuthToken)?.value)
|
||||
return NextResponse.json({ message: "로그인이 필요합니다." }, { status: 401 });
|
||||
let file: File;
|
||||
try {
|
||||
const part = (await request.formData()).get("file");
|
||||
if (!(part instanceof File) || part.size === 0) throw new Error();
|
||||
file = part;
|
||||
} catch {
|
||||
return NextResponse.json({ message: "파일을 선택해 주세요." }, { status: 400 });
|
||||
}
|
||||
try {
|
||||
const upload = new FormData();
|
||||
upload.append("file", file, file.name);
|
||||
const serverApi = await createServerApi();
|
||||
const result = await serverApi.postForm<unknown>(
|
||||
"/project/" + slug.slice(0, 2).map(encodeURIComponent).join("/") + "/import/" + slug[2],
|
||||
upload,
|
||||
{ timeoutMs: 600_000, cache: "no-store" },
|
||||
);
|
||||
return NextResponse.json(readImportResult(result), {
|
||||
headers: { "Cache-Control": "no-store" },
|
||||
});
|
||||
} catch (error: unknown) {
|
||||
return errorToResponse(error);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user