diff options
| author | joonhoekim <26rote@gmail.com> | 2025-03-25 15:55:45 +0900 |
|---|---|---|
| committer | joonhoekim <26rote@gmail.com> | 2025-03-25 15:55:45 +0900 |
| commit | 1a2241c40e10193c5ff7008a7b7b36cc1d855d96 (patch) | |
| tree | 8a5587f10ca55b162d7e3254cb088b323a34c41b /app/api/upload | |
initial commit
Diffstat (limited to 'app/api/upload')
| -rw-r--r-- | app/api/upload/route.ts | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/app/api/upload/route.ts b/app/api/upload/route.ts new file mode 100644 index 00000000..3b1d8be0 --- /dev/null +++ b/app/api/upload/route.ts @@ -0,0 +1,38 @@ +// app/api/upload/route.ts +import { NextRequest, NextResponse } from "next/server" +import { createWriteStream } from "fs" +import path from "path" +import { v4 as uuid } from "uuid" + +export async function POST(request: NextRequest) { + const formData = await request.formData() + const file = formData.get("file") as File | null + if (!file) { + return NextResponse.json({ error: "No file" }, { status: 400 }) + } + + // 여기서는 로컬 /public/uploads 에 저장한다고 가정 + const fileExt = path.extname(file.name) + const newFileName = `${uuid()}${fileExt}` + const filePath = path.join(process.cwd(), "public", "uploads", newFileName) + + const arrayBuffer = await file.arrayBuffer() + const buffer = Buffer.from(arrayBuffer) + + // 로컬에 저장 + await new Promise<void>((resolve, reject) => { + const writeStream = createWriteStream(filePath) + writeStream.write(buffer) + writeStream.end() + writeStream.on("finish", resolve) + writeStream.on("error", reject) + }) + + // /uploads/xxxx.ext 로 접근 가능 + const url = `/uploads/${newFileName}` + return NextResponse.json({ + fileName: file.name, + url, + size: file.size, + }) +}
\ No newline at end of file |
