add web demo
This commit is contained in:
31
apps/web/app/api/upload/route.ts
Normal file
31
apps/web/app/api/upload/route.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { put } from "@vercel/blob";
|
||||
import { NextResponse } from "next/server";
|
||||
|
||||
export const runtime = "edge";
|
||||
|
||||
export async function POST(req: Request) {
|
||||
if (!process.env.BLOB_READ_WRITE_TOKEN) {
|
||||
return new Response(
|
||||
"Missing BLOB_READ_WRITE_TOKEN. Don't forget to add that to your .env file.",
|
||||
{
|
||||
status: 401,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
const file = req.body || "";
|
||||
const filename = req.headers.get("x-vercel-filename") || "file.txt";
|
||||
const contentType = req.headers.get("content-type") || "text/plain";
|
||||
const fileType = `.${contentType.split("/")[1]}`;
|
||||
|
||||
// construct final filename based on content-type if not provided
|
||||
const finalName = filename.includes(fileType)
|
||||
? filename
|
||||
: `${filename}${fileType}`;
|
||||
const blob = await put(finalName, file, {
|
||||
contentType,
|
||||
access: "public",
|
||||
});
|
||||
|
||||
return NextResponse.json(blob);
|
||||
}
|
Reference in New Issue
Block a user