|
1 | | -import { handleBlobUpload, type HandleBlobUploadBody } from "@vercel/blob"; |
| 1 | +import { getPresignedUploadUrl } from "@/lib/utils/server/s3"; |
2 | 2 | import { NextResponse } from "next/server"; |
3 | 3 | import { auth } from "@clerk/nextjs"; |
4 | | -import c from "config"; |
| 4 | +import { staticUploads } from "config"; |
5 | 5 |
|
6 | | -export async function POST(request: Request): Promise<NextResponse> { |
7 | | - const body = (await request.json()) as HandleBlobUploadBody; |
8 | | - const { userId } = await auth(); |
| 6 | +interface RequestBody { |
| 7 | + location: string; |
| 8 | + fileName: string; |
| 9 | +} |
9 | 10 |
|
| 11 | +// TODO: Verify this route works with create team. |
| 12 | +export async function POST(request: Request): Promise<NextResponse> { |
10 | 13 | try { |
11 | | - const jsonResponse = await handleBlobUpload({ |
12 | | - body, |
13 | | - request, |
14 | | - onBeforeGenerateToken: async (pathname) => { |
15 | | - // Step 1. Generate a client token for the browser to upload the file |
16 | | - |
17 | | - // ⚠️ Authenticate users before allowing client tokens to be generated and sent to browsers. Otherwise, you're exposing your Blob store to be an anonymous upload platform. |
18 | | - // See https://nextjs.org/docs/pages/building-your-application/routing/authenticating for more information |
| 14 | + const body: RequestBody = (await request.json()) as RequestBody; |
19 | 15 |
|
20 | | - if (!userId) { |
21 | | - throw new Error("Not authenticated or bad pathname"); |
22 | | - } |
| 16 | + const { userId } = auth(); |
| 17 | + if (!userId) { |
| 18 | + return new NextResponse( |
| 19 | + "You do not have permission to upload files", |
| 20 | + { |
| 21 | + status: 401, |
| 22 | + }, |
| 23 | + ); |
| 24 | + } |
23 | 25 |
|
24 | | - return { |
25 | | - maximumSizeInBytes: c.maxProfilePhotoSizeInBytes, // optional, default and maximum is 500MB |
26 | | - allowedContentTypes: ["image/jpeg", "image/png"], // optional, default is no restriction |
27 | | - }; |
28 | | - }, |
29 | | - onUploadCompleted: async () => undefined, |
30 | | - }); |
| 26 | + const randomSeq = crypto.randomUUID(); |
| 27 | + const [fileName, extension] = body.fileName.split("."); |
| 28 | + const key = `${body.location}/${fileName}-${randomSeq}.${extension}`; |
| 29 | + const url = await getPresignedUploadUrl(staticUploads.bucketName, key); |
31 | 30 |
|
32 | | - return NextResponse.json(jsonResponse); |
| 31 | + return NextResponse.json({ url, key }); |
33 | 32 | } catch (error) { |
34 | 33 | return NextResponse.json( |
35 | 34 | { error: (error as Error).message }, |
|
0 commit comments