1
1
import { NextResponse } from "next/server" ;
2
2
import { PDFDocument } from "pdf-lib" ;
3
- import { campuses , exams , semesters , slots , years } from "@/components/select_options" ;
3
+ import {
4
+ campuses ,
5
+ exams ,
6
+ semesters ,
7
+ slots ,
8
+ years ,
9
+ } from "@/components/select_options" ;
4
10
import { connectToDatabase } from "@/lib/mongoose" ;
5
11
import cloudinary from "cloudinary" ;
6
12
import { type ICourses , type CloudinaryUploadResult } from "@/interface" ;
7
13
import { PaperAdmin } from "@/db/papers" ;
8
14
import axios from "axios" ;
9
15
import processAndAnalyze from "@/util/mistral" ;
10
16
import { examMap } from "./map" ;
17
+ import Fuse from "fuse.js" ;
11
18
// import processAndAnalyze from "./mistral";
12
19
// TODO: REMOVE THUMBNAIL FROM admin-buffer DB
13
20
cloudinary . v2 . config ( {
@@ -26,57 +33,54 @@ export async function POST(req: Request) {
26
33
const files : File [ ] = formData . getAll ( "files" ) as File [ ] ;
27
34
const isPdf = formData . get ( "isPdf" ) === "true" ; // Convert string to boolean
28
35
29
- let imageURL = ""
30
- if ( isPdf )
31
- {
32
- imageURL = formData . get ( "image" ) as string
33
- }
34
- else
35
- {
36
+ let imageURL = "" ;
37
+ if ( isPdf ) {
38
+ imageURL = formData . get ( "image" ) as string ;
39
+ } else {
36
40
const bytes = await files [ 0 ] ?. arrayBuffer ( ) ;
37
41
if ( bytes ) {
38
42
const buffer = await Buffer . from ( bytes ) ;
39
43
imageURL = `data:${ "image/png" } ;base64,${ buffer . toString ( "base64" ) } ` ;
40
-
41
- }
42
- }
43
- const tags = await processAndAnalyze ( { imageURL} )
44
- let subject = ""
45
- let slot = ""
46
- let exam = ""
47
- let year = ""
48
- let campus = ""
49
- let semester = ""
50
-
51
- if ( ! tags )
52
- {
53
- console . log ( "Anaylsis failed, inputing blank strings as fields" )
54
- }
55
- else {
56
- subject = tags [ "course-name" ]
57
- slot = tags . slot
58
- if ( "exam-type" in tags && tags [ "exam-type" ] in examMap )
59
- {
60
- const examType = tags [ "exam-type" ] as keyof typeof examMap ;
61
- exam = examMap [ examType ] ;
62
-
63
- }
64
- year = formData . get ( "year" ) as string ;
65
- campus = formData . get ( "campus" ) as string ;
66
- semester = formData . get ( "semester" ) as string ;
67
-
44
+ }
68
45
}
69
- console . log ( exam , slot , subject )
70
-
71
- const { data } = await axios . get < ICourses [ ] > ( `${ process . env . SERVER_URL } /api/course-list` ) ;
46
+ const tags = await processAndAnalyze ( { imageURL } ) ;
47
+ let subject = "" ;
48
+ let slot = "" ;
49
+ let exam = "" ;
50
+ let year = "" ;
51
+ let campus = "" ;
52
+ let semester = "" ;
53
+ const { data } = await axios . get < ICourses [ ] > (
54
+ `${ process . env . SERVER_URL } /api/course-list` ,
55
+ ) ;
72
56
const courses = data . map ( ( course : { name : string } ) => course . name ) ;
57
+ const coursesFuzy = new Fuse ( courses ) ;
58
+ if ( ! tags ) {
59
+ console . log ( "Anaylsis failed, inputing blank strings as fields" ) ;
60
+ } else {
61
+ const subjectSearch = coursesFuzy . search ( tags [ "course-name" ] ) [ 0 ] ;
62
+ if ( subjectSearch ) {
63
+ subject = subjectSearch . item ;
64
+ }
65
+ const slotPattern = new RegExp ( `[${ tags . slot } ]` , "i" ) ;
66
+ const slotSearchResult = slots . find ( ( s ) => slotPattern . test ( s ) ) ;
67
+ slot = slotSearchResult ? slotSearchResult : tags . slot ;
68
+ if ( "exam-type" in tags && tags [ "exam-type" ] in examMap ) {
69
+ const examType = tags [ "exam-type" ] as keyof typeof examMap ;
70
+ exam = examMap [ examType ] ;
71
+ }
72
+ year = formData . get ( "year" ) as string ;
73
+ campus = formData . get ( "campus" ) as string ;
74
+ semester = formData . get ( "semester" ) as string ;
75
+ }
76
+ console . log ( exam , slot , subject ) ;
73
77
74
78
if (
75
79
! (
76
80
exam . includes ( exam ) &&
77
81
years . includes ( year ) &&
78
82
campuses . includes ( campus ) &&
79
- semesters . includes ( semester )
83
+ semesters . includes ( semester )
80
84
)
81
85
) {
82
86
return NextResponse . json ( { message : "Bad request" } , { status : 400 } ) ;
@@ -93,9 +97,8 @@ export async function POST(req: Request) {
93
97
{ status : 400 } ,
94
98
) ;
95
99
}
96
-
97
- if ( ! isPdf ) {
98
100
101
+ if ( ! isPdf ) {
99
102
try {
100
103
if ( ! process . env . NEXT_PUBLIC_CLOUDINARY_UPLOAD_PRESET ) {
101
104
return ;
@@ -135,7 +138,7 @@ export async function POST(req: Request) {
135
138
year,
136
139
exam,
137
140
campus,
138
- semester
141
+ semester,
139
142
} ) ;
140
143
await paper . save ( ) ;
141
144
return NextResponse . json (
@@ -167,7 +170,6 @@ async function uploadFile(
167
170
fileType : string ,
168
171
) {
169
172
try {
170
-
171
173
const buffer = Buffer . from ( bytes ) ;
172
174
const dataUrl = `data:${ fileType } ;base64,${ buffer . toString ( "base64" ) } ` ;
173
175
const uploadResult = ( await cloudinary . v2 . uploader . unsigned_upload (
@@ -182,7 +184,7 @@ async function uploadFile(
182
184
183
185
async function CreatePDF ( files : File [ ] ) {
184
186
const pdfDoc = await PDFDocument . create ( ) ;
185
- //sort files using name. Later remove to see if u can without names
187
+ //sort files using name. Later remove to see if u can without names
186
188
const orderedFiles = Array . from ( files ) . sort ( ( a , b ) => {
187
189
return a . name . localeCompare ( b . name ) ;
188
190
} ) ;
0 commit comments