blob: 97beafb1a6073b1d177c7a342513d12376aec216 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
import { NextRequest, NextResponse } from "next/server"
import { promises as fs } from "fs"
import path from "path"
import { v4 as uuidv4 } from "uuid"
export async function POST(req: NextRequest) {
try {
const formData = await req.formData()
const file = formData.get("file") as File // "file" is default name from FilePond
if (!file) {
return NextResponse.json({ error: "No file" }, { status: 400 })
}
// e.g. parse a query param? or read 'rfqId' if we appended it
// const rfqId = ... (FilePond advanced config or handle differently)
// read file
const arrayBuffer = await file.arrayBuffer()
const buffer = Buffer.from(arrayBuffer)
// unique filename
const uniqueName = uuidv4() + "-" + file.name
const targetDir = path.join(process.cwd(), "public", "rfq", "123") // or your rfqId
await fs.mkdir(targetDir, { recursive: true })
const targetPath = path.join(targetDir, uniqueName)
// write
await fs.writeFile(targetPath, buffer)
// Return success. Typically you'd insert DB record here or return some reference
return NextResponse.json({ success: true, filePath: `/rfq/123/${uniqueName}` })
} catch (error) {
console.error("upload error:", error)
return NextResponse.json({ error: String(error) }, { status: 500 })
}
}
|