diff options
Diffstat (limited to 'app/api/rfq-upload/route.ts')
| -rw-r--r-- | app/api/rfq-upload/route.ts | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/app/api/rfq-upload/route.ts b/app/api/rfq-upload/route.ts new file mode 100644 index 00000000..97beafb1 --- /dev/null +++ b/app/api/rfq-upload/route.ts @@ -0,0 +1,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 }) + } +}
\ No newline at end of file |
