File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed
src/app/api/storage/delete Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change 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+ } ;
You can’t perform that action at this time.
0 commit comments