1
1
import { NextResponse } from "next/server" ;
2
2
import { PDFDocument } from "pdf-lib" ;
3
- import {
4
- campuses ,
5
- exams ,
6
- semesters ,
7
- slots ,
8
- years ,
9
- } from "@/components/select_options" ;
10
3
import { connectToDatabase } from "@/lib/mongoose" ;
11
4
import cloudinary from "cloudinary" ;
12
- import type {
13
- ICourses ,
14
- CloudinaryUploadResult ,
15
- ExamDetail ,
16
- IAdminPaper ,
17
- } from "@/interface" ;
5
+ import type { CloudinaryUploadResult } from "@/interface" ;
18
6
import { PaperAdmin } from "@/db/papers" ;
19
- import axios from "axios" ;
20
- import processAndAnalyze from "@/util/gemini" ;
21
- import Fuse from "fuse.js" ;
22
7
23
8
cloudinary . v2 . config ( {
24
9
cloud_name : process . env . NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME ,
25
10
api_key : process . env . CLOUDINARY_API_KEY ,
26
11
api_secret : process . env . CLOUDINARY_SECRET ,
27
12
} ) ;
28
- type SemesterType = IAdminPaper [ "semester" ] ;
29
13
30
14
const config1 = {
31
15
cloud_name : process . env . NEXT_PUBLIC_CLOUDINARY_CLOUD_NAME_1 ,
@@ -38,6 +22,7 @@ const config2 = {
38
22
api_key : process . env . CLOUDINARY_API_KEY_2 ,
39
23
api_secret : process . env . CLOUDINARY_SECRET_2 ,
40
24
} ;
25
+
41
26
const cloudinaryConfigs = [ config1 , config2 ] ;
42
27
43
28
export async function POST ( req : Request ) {
@@ -68,66 +53,6 @@ export async function POST(req: Request) {
68
53
const pdfBuffer = Buffer . from ( pdfBytes ) ;
69
54
pdfData = pdfBuffer . toString ( "base64" ) ;
70
55
}
71
- const tags = await processAndAnalyze ( { pdfData } ) ;
72
-
73
- console . log ( "tags generated:" , tags ) ;
74
-
75
- const { data } = await axios . get < ICourses [ ] > (
76
- `${ process . env . SERVER_URL } /api/course-list` ,
77
- ) ;
78
- const courses = data . map ( ( course : { name : string } ) => course . name ) ;
79
-
80
- const finalTags = await setTagsFromCurrentLists ( tags , courses ) ;
81
- console . log ( " tags final:" , finalTags ) ;
82
-
83
- const subject = finalTags . subject ;
84
- const slot = finalTags . slot ;
85
- const exam = finalTags . exam ;
86
- const year = finalTags . year ;
87
- const campus = formData . get ( "campus" ) as string ;
88
- const semester = finalTags . semester ;
89
- const answerKeyIncluded = finalTags . answerKeyIncluded ;
90
- if ( ! courses . includes ( subject ) ) {
91
- return NextResponse . json (
92
- { message : "The course subject is invalid." } ,
93
- { status : 400 } ,
94
- ) ;
95
- }
96
-
97
- if ( ! slots . includes ( slot ) ) {
98
- return NextResponse . json (
99
- { message : "The slot is invalid." } ,
100
- { status : 400 } ,
101
- ) ;
102
- }
103
-
104
- if ( ! exam . includes ( exam ) ) {
105
- return NextResponse . json (
106
- { message : "The exam type is invalid." } ,
107
- { status : 400 } ,
108
- ) ;
109
- }
110
-
111
- if ( ! years . includes ( year ) ) {
112
- return NextResponse . json (
113
- { message : "The year is invalid." } ,
114
- { status : 400 } ,
115
- ) ;
116
- }
117
-
118
- if ( ! campuses . includes ( campus ) ) {
119
- return NextResponse . json (
120
- { message : `The ${ campus } is invalid.` } ,
121
- { status : 400 } ,
122
- ) ;
123
- }
124
-
125
- if ( ! semesters . includes ( semester ) ) {
126
- return NextResponse . json (
127
- { message : "The semester is invalid." } ,
128
- { status : 400 } ,
129
- ) ;
130
- }
131
56
132
57
let finalUrl : string | undefined = "" ;
133
58
let public_id_cloudinary : string | undefined = "" ;
@@ -146,11 +71,15 @@ export async function POST(req: Request) {
146
71
return ;
147
72
}
148
73
74
+ console . log ( "this is happening 1" ) ;
75
+
149
76
const mergedPdfBytes = await CreatePDF ( files ) ;
150
77
[ public_id_cloudinary , finalUrl ] = await uploadPDFFile (
151
78
mergedPdfBytes ,
152
79
uploadPreset ,
153
80
) ;
81
+
82
+ console . log ( "this is happening 2" ) ;
154
83
} catch ( error ) {
155
84
console . error ( "Error creating PDF:" , error ) ;
156
85
return NextResponse . json (
@@ -159,33 +88,39 @@ export async function POST(req: Request) {
159
88
) ;
160
89
}
161
90
} else {
91
+ console . log ( "this is happening 3" ) ;
162
92
[ public_id_cloudinary , finalUrl ] = await uploadPDFFile (
163
93
files [ 0 ] ! ,
164
94
uploadPreset ,
165
95
) ;
166
96
}
167
- console . log ( finalUrl ) ;
97
+
168
98
const thumbnailResponse = cloudinary . v2 . image ( finalUrl ! , {
169
99
format : "jpg" ,
170
100
} ) ;
171
101
thumbnailUrl = thumbnailResponse
172
102
. replace ( "pdf" , "jpg" )
173
103
. replace ( "upload" , "upload/w_400,h_400,c_fill" )
174
104
. replace ( / < i m g s r c = ' | ' \s * \/ > / g, "" ) ;
105
+
106
+ console . log ( "this is happening 4" ) ;
107
+
175
108
const paper = new PaperAdmin ( {
176
109
cloudinary_index : configIndex ,
177
110
public_id_cloudinary,
178
- answerKeyIncluded,
179
111
finalUrl,
180
112
thumbnailUrl,
181
- subject,
182
- slot,
183
- year,
184
- exam,
185
- campus ,
186
- semester ,
113
+ subject : null ,
114
+ slot : null ,
115
+ year : null ,
116
+ exam : null ,
117
+ semester : null ,
118
+ campus : null ,
187
119
} ) ;
120
+
121
+ console . log ( "this is happening 5" ) ;
188
122
await paper . save ( ) ;
123
+ console . log ( "this is happening 6" ) ;
189
124
return NextResponse . json ( { status : "success" } , { status : 201 } ) ;
190
125
} catch ( error ) {
191
126
console . error ( error ) ;
@@ -197,6 +132,7 @@ export async function POST(req: Request) {
197
132
}
198
133
199
134
async function uploadPDFFile ( file : File | ArrayBuffer , uploadPreset : string ) {
135
+ console . log ( "this is happening 7" ) ;
200
136
let bytes ;
201
137
if ( file instanceof File ) {
202
138
bytes = await file . arrayBuffer ( ) ;
@@ -212,12 +148,16 @@ async function uploadFile(
212
148
fileType : string ,
213
149
) {
214
150
try {
151
+ console . log ( "this is happening 8" ) ;
215
152
const buffer = Buffer . from ( bytes ) ;
153
+ console . log ( "this is happening 9" ) ;
216
154
const dataUrl = `data:${ fileType } ;base64,${ buffer . toString ( "base64" ) } ` ;
155
+ console . log ( "this is happening 10" ) ;
217
156
const uploadResult = ( await cloudinary . v2 . uploader . unsigned_upload (
218
157
dataUrl ,
219
158
uploadPreset ,
220
159
) ) as CloudinaryUploadResult ;
160
+ console . log ( "this is happening 11" ) ;
221
161
return [ uploadResult . public_id , uploadResult . secure_url ] ;
222
162
} catch ( e ) {
223
163
throw e ;
@@ -252,62 +192,3 @@ async function CreatePDF(orderedFiles: File[]) {
252
192
const mergedPdfBytes = await pdfDoc . save ( ) ;
253
193
return mergedPdfBytes ;
254
194
}
255
-
256
- // Sets course-name to corresponding course name from our api
257
- async function setTagsFromCurrentLists (
258
- tags : ExamDetail | undefined ,
259
- courses : string [ ] ,
260
- ) : Promise < ExamDetail > {
261
- if ( ! courses [ 0 ] || ! slots [ 0 ] || ! exams [ 0 ] || ! semesters [ 0 ] || ! years [ 0 ] ) {
262
- throw Error ( "Cannot fetch default value for courses/slot/exam/sem/year!" ) ;
263
- }
264
-
265
- const newTags : ExamDetail = {
266
- subject : courses [ 0 ] ,
267
- slot : slots [ 0 ] ,
268
- "course-code" : "notInUse" ,
269
- exam : exams [ 0 ] ,
270
- semester : semesters [ 0 ] as SemesterType ,
271
- year : years [ 0 ] ,
272
- answerKeyIncluded : false ,
273
- } ;
274
-
275
- const coursesFuzy = new Fuse ( courses ) ;
276
- if ( ! tags ) {
277
- console . log ( "Anaylsis failed setting random courses as fields" ) ;
278
- return newTags ;
279
- } else {
280
- const subjectSearch = coursesFuzy . search ( tags . subject ) [ 0 ] ;
281
- if ( subjectSearch ) {
282
- newTags . subject = subjectSearch . item ;
283
- }
284
- const slotSearchResult = findMatch ( slots , tags . slot ) ;
285
- if ( slotSearchResult ) {
286
- newTags . slot = slotSearchResult ;
287
- }
288
- const examSearchResult = findMatch ( exams , tags . exam ) ;
289
- if ( examSearchResult ) {
290
- newTags . exam = examSearchResult ;
291
- }
292
- const semesterSearchResult = findMatch ( semesters , tags . semester ) ;
293
- if ( semesterSearchResult ) {
294
- newTags . semester = semesterSearchResult as SemesterType ;
295
- }
296
- const yearSearchResult = findMatch ( years , tags . year ) ;
297
-
298
- if ( yearSearchResult ) {
299
- newTags . year = yearSearchResult ;
300
- }
301
- const answerkeySearchResults = tags . answerKeyIncluded ?? false ;
302
-
303
- if ( yearSearchResult ) {
304
- newTags . answerKeyIncluded = answerkeySearchResults ;
305
- }
306
- }
307
- return newTags ;
308
- }
309
- function findMatch < T > ( arr : T [ ] , value : string | undefined ) : T | undefined {
310
- if ( ! value ) return undefined ;
311
- const pattern = new RegExp ( value , "i" ) ;
312
- return arr . find ( ( item ) => pattern . test ( String ( item ) ) ) ;
313
- }
0 commit comments