Skip to content

Commit d0ad423

Browse files
committed
feat: implement S3 file deletion API endpoint
1 parent b4c1501 commit d0ad423

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import { DeleteObjectCommand } from "@aws-sdk/client-s3";
2+
import { NextResponse } from "next/server";
3+
4+
import { env } from "@/env";
5+
import { z } from "zod";
6+
import { authID } from "@/backend/services/session.actions";
7+
import { s3Client } from "@/backend/s3.client";
8+
9+
const bodySchema = z.object({
10+
keys: z.array(z.string()),
11+
});
12+
13+
export const POST = async (request: Request) => {
14+
const userID = await authID();
15+
if (!userID) {
16+
return new Response("Unauthorized", {
17+
status: 401,
18+
headers: {
19+
"Content-Type": "application/json",
20+
},
21+
});
22+
}
23+
const body = await bodySchema.safeParseAsync(await request.json());
24+
25+
if (!body.success) {
26+
return new Response(JSON.stringify(body.error), {
27+
status: 400,
28+
headers: {
29+
"Content-Type": "application/json",
30+
},
31+
});
32+
}
33+
34+
for (const key of body.data.keys) {
35+
const command = new DeleteObjectCommand({
36+
Bucket: env.S3_BUCKET,
37+
Key: key,
38+
});
39+
await s3Client.send(command);
40+
}
41+
42+
return NextResponse.json({
43+
success: true,
44+
message: "Files deleted successfully",
45+
});
46+
};

0 commit comments

Comments
 (0)