@@ -51,33 +51,44 @@ async function GetAllContributions(req, res, next) {
5151async function HandleFileUpload ( req , res , next ) {
5252 console . log ( "Handling File Upload" ) ;
5353 const contributionId = req . headers [ "contribution-id" ] ;
54- // console.log(req.headers.username);
55- const file = req . file ;
54+ const files = req . files ; // Changed from req.file to req.files for array handling
5655
57- // Files names
58- let initialPath = file . path ;
59- let newFilename = file . filename ;
60- let originalFilename = file . originalname ;
56+ // Check if files were uploaded
57+ if ( ! files || files . length === 0 ) {
58+ return res . status ( 400 ) . json ( { error : "No files were uploaded" } ) ;
59+ }
6160
62- let wordArr = originalFilename . split ( "." ) ;
63- let fileExtension = wordArr [ wordArr . length - 1 ] ;
64- let finalFileName = "" ;
61+ // Handle multiple files
62+ const uploadedFiles = [ ] ;
6563
66- for ( let i = 0 ; i < wordArr . length - 1 ; i ++ ) {
67- finalFileName += wordArr [ i ] ;
68- }
69- finalFileName += "~" + req . headers . username ;
70- finalFileName += "." + fileExtension ;
64+ for ( const file of files ) {
65+ // Files names
66+ let initialPath = file . path ;
67+ let newFilename = file . filename ;
68+ let originalFilename = file . originalname ;
7169
72- const finalPath = initialPath . slice ( 0 , initialPath . indexOf ( newFilename ) ) ;
70+ let wordArr = originalFilename . split ( "." ) ;
71+ let fileExtension = wordArr [ wordArr . length - 1 ] ;
72+ let finalFileName = "" ;
7373
74- await fs . promises . rename ( finalPath + newFilename , finalPath + finalFileName ) ;
75- const fileId = await UploadFile ( contributionId , finalPath , finalFileName ) ;
76- if ( fileId ) {
77- await HandleFileToDB ( contributionId , fileId ) ;
74+ for ( let i = 0 ; i < wordArr . length - 1 ; i ++ ) {
75+ finalFileName += wordArr [ i ] ;
76+ }
77+ finalFileName += "~" + req . headers . username ;
78+ finalFileName += "." + fileExtension ;
79+
80+ const finalPath = initialPath . slice ( 0 , initialPath . indexOf ( newFilename ) ) ;
81+
82+ await fs . promises . rename ( finalPath + newFilename , finalPath + finalFileName ) ;
83+ const fileId = await UploadFile ( contributionId , finalPath , finalFileName ) ;
84+ if ( fileId ) {
85+ await HandleFileToDB ( contributionId , fileId ) ;
86+ uploadedFiles . push ( { fileId, originalName : originalFilename } ) ;
87+ }
88+ await fs . promises . unlink ( finalPath + finalFileName ) ;
7889 }
79- await fs . promises . unlink ( finalPath + finalFileName ) ;
80- return res . json ( { file } ) ;
90+
91+ return res . json ( { files : uploadedFiles , count : uploadedFiles . length } ) ;
8192}
8293
8394async function CreateNewContribution ( req , res , next ) {
@@ -105,7 +116,9 @@ async function CreateNewContribution(req, res, next) {
105116}
106117
107118async function GetMyContributions ( req , res , next ) {
108- const myContributions = await Contribution . find ( { uploadedBy : req . user . _id } ) . populate ( { path : 'files' } ) ;
119+ const myContributions = await Contribution . find ( { uploadedBy : req . user . _id } ) . populate ( {
120+ path : "files" ,
121+ } ) ;
109122 res . json ( myContributions ) ;
110123}
111124
0 commit comments