@@ -5,68 +5,101 @@ import { customAlphabet } from "nanoid";
55import path from "path" ;
66import { writeFile , mkdir } from "fs/promises" ;
77import { getUploadDir , getUploadPublicBasePath } from "@/lib/upload-path" ;
8+ import { detectMimeType } from "@/lib/file-upload-validation" ;
89
910const UPLOAD_DIR = getUploadDir ( ) ;
1011const UPLOAD_PUBLIC_BASE = getUploadPublicBasePath ( ) ;
1112
1213export default async function POST ( req : Request ) {
13- const formData = await req . formData ( ) ;
14- const data = parseFormData ( formData )
15-
16- const paperID = await generateSubmissionId ( ) ;
14+ try {
15+ const formData = await req . formData ( ) ;
16+ const data = parseFormData ( formData ) ;
17+ const fileBuffer = Buffer . from ( await data . file . arrayBuffer ( ) ) ;
18+ const detectedMimeType = detectMimeType ( fileBuffer ) ;
19+
20+ if ( detectedMimeType !== "application/pdf" ) {
21+ return Response . json (
22+ {
23+ success : false ,
24+ data : "Invalid file type. Please upload a valid PDF file." ,
25+ } ,
26+ {
27+ status : 400 ,
28+ }
29+ ) ;
30+ }
1731
18- // Upload file
19- const uploadDir = path . join ( process . cwd ( ) , UPLOAD_DIR , "papers" ) ;
20- const filePath = path . join ( uploadDir , `${ paperID } .pdf` ) ;
32+ const paperID = await generateSubmissionId ( ) ;
33+
34+ // Upload file
35+ const uploadDir = path . join ( process . cwd ( ) , UPLOAD_DIR , "papers" ) ;
36+ const filePath = path . join ( uploadDir , `${ paperID } .pdf` ) ;
37+
38+ // Ensure the upload directory exists
39+ await mkdir ( uploadDir , { recursive : true } ) ;
40+ await writeFile ( filePath , fileBuffer ) ;
41+
42+ const authorFromDb = await db . select ( { id : users . id } ) . from ( users ) . where (
43+ eq ( users . email , data . coAuthors ?. [ 0 ] ?. email )
44+ ) ;
45+ if ( authorFromDb . length === 0 ) {
46+ return Response . json (
47+ {
48+ success : false ,
49+ data : "First author not registered! Please make sure the email matches with your GitHub's primary email." ,
50+ } ,
51+ {
52+ status : 400 ,
53+ }
54+ ) ;
55+ }
2156
22- // Ensure the upload directory exists
23- await mkdir ( uploadDir , { recursive : true } ) ;
24- await writeFile ( filePath , Buffer . from ( await data . file . arrayBuffer ( ) ) ) ;
57+ const paperReturned = await db
58+ . insert ( papers )
59+ . values ( {
60+ title : data . title ,
61+ abstract : data . abstract ,
62+ keywords : data . keywords ,
63+ fileUrl : `${ UPLOAD_PUBLIC_BASE } /papers/${ paperID } .pdf` ,
64+ themeId : data . theme ,
65+ trackType : data . trackType ,
66+ authorId : authorFromDb [ 0 ] . id ,
67+ submissionId : paperID ,
68+ } )
69+ . returning ( { id : papers . id , submissionId : papers . submissionId } ) ;
70+
71+ await Promise . all (
72+ data . coAuthors . map ( async ( coauthor , index ) => {
73+ if ( index >= 1 ) {
74+ await db . insert ( coAuthors ) . values ( {
75+ name : coauthor . name ,
76+ email : coauthor . email ,
77+ paperId : paperReturned [ 0 ] . id ,
78+ orcid : coauthor ?. orcid ,
79+ affiliation : coauthor ?. affiliation ,
80+ } ) ;
81+ }
82+ } )
83+ ) ;
2584
26- const authorFromDb = await db . select ( { id : users . id } ) . from ( users ) . where (
27- eq ( users . email , data . coAuthors ?. [ 0 ] ?. email )
28- ) ;
29- if ( authorFromDb . length === 0 ) {
3085 return Response . json ( {
31- success : false ,
32- data : "First author not registered! Please make sure the email matches with your GitHub's primary email."
33- } , {
34- status : 400
35- } )
36- }
37-
38- const paperReturned = await db
39- . insert ( papers )
40- . values ( {
41- title : data . title ,
42- abstract : data . abstract ,
43- keywords : data . keywords ,
44- fileUrl : `${ UPLOAD_PUBLIC_BASE } /papers/${ paperID } .pdf` ,
45- themeId : data . theme ,
46- trackType : data . trackType ,
47- authorId : authorFromDb [ 0 ] . id ,
48- submissionId : paperID
49- } ) . returning ( { id : papers . id , submissionId : papers . submissionId } )
50-
51- await Promise . all (
52- data . coAuthors . map ( async ( coauthor , index ) => {
53- if ( index >= 1 ) { // Neglecting the first co-author
54- await db . insert ( coAuthors ) . values ( {
55- name : coauthor . name ,
56- email : coauthor . email ,
57- paperId : paperReturned [ 0 ] . id ,
58- orcid : coauthor ?. orcid ,
59- affiliation : coauthor ?. affiliation
60- } )
86+ success : true ,
87+ data : {
88+ submissionId : paperReturned [ 0 ] . submissionId ,
89+ } ,
90+ } ) ;
91+ } catch ( error ) {
92+ console . error ( "Paper submission error:" , error ) ;
93+ return Response . json (
94+ {
95+ success : false ,
96+ data : "Failed to process paper submission." ,
97+ } ,
98+ {
99+ status : 500 ,
61100 }
62- } )
63- )
64-
65- return Response . json ( {
66- success : true , data : {
67- submissionId : paperReturned [ 0 ] . submissionId
68- }
69- } )
101+ ) ;
102+ }
70103}
71104
72105async function generateSubmissionId ( ) {
0 commit comments