|
| 1 | +import { auth } from "@/auth"; |
1 | 2 | import S3, { r2_bucket_name } from "@/lib/cloudflareR2Connect";
|
2 | 3 | import { ApiResponse } from "@/types/ApiResponse";
|
3 | 4 | import { randomUUID } from "crypto";
|
| 5 | +import { Session } from "next-auth"; |
4 | 6 | import { NextRequest, NextResponse } from "next/server";
|
5 | 7 |
|
6 |
| -export const GET = async (req: NextRequest) => { |
7 |
| - const fileType = req.nextUrl.searchParams.get("fileType") as string; |
8 |
| - const ext = fileType.split("/")[1]; |
9 |
| - const Key = `${randomUUID()}.${ext}`; |
10 |
| - const params = { |
11 |
| - Bucket: r2_bucket_name, |
12 |
| - Key, |
13 |
| - ContentType: fileType, |
14 |
| - }; |
15 |
| - const signedUrl = await S3.getSignedUrl("putObject", params); |
| 8 | +export const GET = auth(async (req: NextRequest & { auth: Session | null }) => { |
16 | 9 | const res: ApiResponse = {
|
17 |
| - success: true, |
18 |
| - message: "OK", |
19 |
| - signedUrl, |
20 |
| - Key, |
| 10 | + success: false, |
| 11 | + message: "", |
21 | 12 | };
|
22 |
| - return NextResponse.json(res); |
23 |
| -}; |
| 13 | + if (req.auth) { |
| 14 | + const fileType = req.nextUrl.searchParams.get("fileType") as string; |
| 15 | + const existingKey = req.nextUrl.searchParams.get("key") as string; |
| 16 | + const action = req.nextUrl.searchParams.get("action") as string; |
| 17 | + switch (action) { |
| 18 | + case "put": |
| 19 | + try { |
| 20 | + const ext = fileType.split("/")[1]; |
| 21 | + if (!ext) { |
| 22 | + res.message = "Invalid File Type"; |
| 23 | + throw new Error("Invalid File Type"); |
| 24 | + } |
| 25 | + const Key = existingKey ? existingKey : `${randomUUID()}.${ext}`; |
| 26 | + const params = { |
| 27 | + Bucket: r2_bucket_name, |
| 28 | + Key, |
| 29 | + ContentType: fileType, |
| 30 | + }; |
| 31 | + const signedUrl = await S3.getSignedUrl("putObject", params); |
| 32 | + res.success = true; |
| 33 | + res.message = "OK"; |
| 34 | + res.signedUrl = signedUrl; |
| 35 | + res.Key = Key; |
| 36 | + return NextResponse.json(res, { status: 200 }); |
| 37 | + } catch (error) { |
| 38 | + res.success = false; |
| 39 | + console.error("PresignedURL Error", error); |
| 40 | + res.message = |
| 41 | + res.message == "" |
| 42 | + ? "Something Went Wrong, Check Server Logs for More Details" |
| 43 | + : res.message; |
| 44 | + return NextResponse.json(res, { status: 500 }); |
| 45 | + } |
| 46 | + case "delete": |
| 47 | + try { |
| 48 | + if (!existingKey) { |
| 49 | + res.message = "Invalid Key"; |
| 50 | + throw new Error("Invalid Key"); |
| 51 | + } |
| 52 | + const params = { |
| 53 | + Bucket: r2_bucket_name, |
| 54 | + Key: existingKey, |
| 55 | + }; |
| 56 | + const signedUrl = await S3.getSignedUrl("deleteObject", params); |
| 57 | + res.success = true; |
| 58 | + res.message = "OK"; |
| 59 | + res.signedUrl = signedUrl; |
| 60 | + return NextResponse.json(res, { status: 200 }); |
| 61 | + } catch (error) { |
| 62 | + res.success = false; |
| 63 | + console.error("PresignedURL Error", error); |
| 64 | + res.message = |
| 65 | + res.message == "" |
| 66 | + ? "Something Went Wrong, Check Server Logs for More Details" |
| 67 | + : res.message; |
| 68 | + return NextResponse.json(res, { status: 500 }); |
| 69 | + } |
| 70 | + default: |
| 71 | + res.success = false; |
| 72 | + res.message = res.message == "" ? "Invalid Request" : res.message; |
| 73 | + return NextResponse.json(res, { status: 500 }); |
| 74 | + } |
| 75 | + } else { |
| 76 | + res.success = false; |
| 77 | + res.message = " Unauthorized"; |
| 78 | + return NextResponse.json(res, { status: 401 }); |
| 79 | + } |
| 80 | +}); |
0 commit comments