@@ -34,6 +34,7 @@ export async function POST(req: Request) {
34
34
35
35
await connectToDatabase ( ) ;
36
36
let finalUrl : string | undefined = "" ;
37
+ let public_id_cloudinary : string | undefined = "" ;
37
38
let thumbnailUrl : string | undefined = "" ;
38
39
const existingPaper = await Paper . findOne ( {
39
40
subject,
@@ -60,15 +61,15 @@ export async function POST(req: Request) {
60
61
}
61
62
62
63
const mergedPdfBytes = await CreatePDF ( files ) ;
63
- finalUrl = await uploadPDFFile ( mergedPdfBytes , uploadPreset ) ;
64
+ [ public_id_cloudinary , finalUrl ] = await uploadPDFFile ( mergedPdfBytes , uploadPreset ) ;
64
65
} catch ( error ) {
65
66
return NextResponse . json (
66
67
{ error : "Failed to process PDF" } ,
67
68
{ status : 500 } ,
68
69
) ;
69
70
}
70
71
} else {
71
- finalUrl = await uploadPDFFile ( files [ 0 ] as File , uploadPreset ) ;
72
+ [ public_id_cloudinary , finalUrl ] = await uploadPDFFile ( files [ 0 ] as File , uploadPreset ) ;
72
73
}
73
74
const thumbnailResponse = cloudinary . v2 . image ( finalUrl ! , {
74
75
format : "jpg" ,
@@ -77,8 +78,9 @@ export async function POST(req: Request) {
77
78
. replace ( "pdf" , "jpg" )
78
79
. replace ( "upload" , "upload/w_400,h_400,c_fill" )
79
80
. replace ( / < i m g s r c = ' | ' \s * \/ > / g, "" ) ;
80
-
81
81
const paper = new Paper ( {
82
+
83
+ public_id_cloudinary,
82
84
finalUrl,
83
85
thumbnailUrl,
84
86
subject,
@@ -101,40 +103,38 @@ export async function POST(req: Request) {
101
103
}
102
104
}
103
105
104
- export async function DELETE ( req : Request ) {
105
- try {
106
- const url = new URL ( req . url ) ;
107
- const public_id = url . searchParams . get ( "public_id" ) ;
108
- const type = url . searchParams . get ( "type" ) ;
106
+ // export async function DELETE(req: Request) {
107
+ // try {
108
+ // const url = new URL(req.url);
109
+ // const public_id = url.searchParams.get("public_id");
110
+ // const type = url.searchParams.get("type");
109
111
110
- if ( ! public_id || ! type ) {
111
- return NextResponse . json (
112
- { message : "Missing parameters: public_id or type" } ,
113
- { status : 400 } ,
114
- ) ;
115
- }
116
- await cloudinary . v2 . uploader . destroy ( public_id , {
117
- type : type ,
118
- } ) ;
112
+ // if (!public_id || !type) {
113
+ // return NextResponse.json(
114
+ // { message: "Missing parameters: public_id or type" },
115
+ // { status: 400 },
116
+ // );
117
+ // }
118
+ // await cloudinary.v2.uploader.destroy(public_id, {
119
+ // type: type,
120
+ // });
119
121
120
- return NextResponse . json ( { message : "Asset deleted successfully" } ) ;
121
- } catch ( error ) {
122
- return NextResponse . json (
123
- { message : "Failed to delete asset" , error } ,
124
- { status : 500 } ,
125
- ) ;
126
- }
127
- }
122
+ // return NextResponse.json({ message: "Asset deleted successfully" });
123
+ // } catch (error) {
124
+ // return NextResponse.json(
125
+ // { message: "Failed to delete asset", error },
126
+ // { status: 500 },
127
+ // );
128
+ // }
129
+ // }
128
130
async function uploadPDFFile ( file : File | ArrayBuffer , uploadPreset : string ) {
129
131
let bytes ;
130
- if ( file instanceof File )
132
+ if ( file instanceof File ) //for pdf
131
133
{
132
- console . log ( "PDF file" ) ;
133
134
bytes = await file . arrayBuffer ( ) ;
134
135
}
135
- else
136
+ else // for images that are pdf
136
137
{
137
- console . log ( "Non PDF file" ) ;
138
138
bytes = file as ArrayBuffer ;
139
139
}
140
140
return uploadFile ( bytes , uploadPreset , "application/pdf" )
@@ -146,8 +146,7 @@ async function uploadPDFFile(file: File | ArrayBuffer, uploadPreset: string) {
146
146
147
147
const uploadResult : CloudinaryUploadResult =
148
148
await cloudinary . v2 . uploader . unsigned_upload ( dataUrl , uploadPreset ) ;
149
-
150
- return uploadResult . secure_url ;
149
+ return [ uploadResult . public_id , uploadResult . secure_url ] ;
151
150
} catch ( e ) {
152
151
throw ( e ) ;
153
152
}
0 commit comments