summaryrefslogtreecommitdiff
path: root/app/api/upload
diff options
context:
space:
mode:
authordujinkim <dujin.kim@dtsolution.co.kr>2025-03-26 00:37:41 +0000
committerdujinkim <dujin.kim@dtsolution.co.kr>2025-03-26 00:37:41 +0000
commite0dfb55c5457aec489fc084c4567e791b4c65eb1 (patch)
tree68543a65d88f5afb3a0202925804103daa91bc6f /app/api/upload
3/25 까지의 대표님 작업사항
Diffstat (limited to 'app/api/upload')
-rw-r--r--app/api/upload/route.ts38
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