42 lines
1.7 KiB
TypeScript
42 lines
1.7 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { errorToResponse } from "@kri/common/http";
|
|
import { createServerApi, getServerAccessToken } from "@/lib/http-server";
|
|
import { isImportPath } from "@/lib/project-proxy-policy";
|
|
import { readImportResult } from "@/types/export";
|
|
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 getServerAccessToken()))
|
|
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);
|
|
}
|
|
}
|