-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds the refactored forms in seperate file. They were removed from th…
…e route file, they had to much logic. #85 Adds new functions to select the file headers, from default or from a line in file
- Loading branch information
Showing
6 changed files
with
358 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
var fs=require('fs') | ||
var path=require('path') | ||
var formidable=require('formidable') | ||
var detect=require('detect-file-type') | ||
var formidable = require('formidable') | ||
var processTargetsFile=require('./../miRNADB/targets/psRNAtargetFile') | ||
|
||
function uploadFile(req,uploadDir){ | ||
return new Promise((res,rej)=>{ | ||
|
||
// create an incoming form object | ||
var form = new formidable.IncomingForm(); | ||
|
||
// specify that we want to allow the user to upload multiple files in a single request | ||
form.multiples = false; | ||
//Calculate file hash | ||
form.hash = 'md5'; | ||
// store all uploads in the /uploads directory | ||
form.uploadDir = path.join(uploadDir,"/de_matrices"); | ||
|
||
// every time a file has been uploaded successfully, | ||
// rename it to it's original name | ||
form.on('file', function(field, file) { | ||
detect.fromFile(file.path,function(err,result){ | ||
if (err) rej(err) | ||
if (result===null){ | ||
fs.rename(file.path, path.join(form.uploadDir, file.name), (err)=>{ | ||
if(err) rej(err) | ||
file={hash:form.openedFiles[0].hash, name:form.openedFiles[0].name} | ||
res(file); | ||
}); | ||
}else{ | ||
fs.unlink(file.path, (err)=>{ | ||
err ? rej(err) : res({hash:'',name:"UnsupportedFile"}) | ||
}) | ||
} | ||
}) | ||
}); | ||
|
||
// log any errors that occur | ||
form.on('error', function(err) { | ||
rej(err); | ||
}); | ||
|
||
// once all the files have been uploaded, send a response to the client | ||
form.on('end', function() { | ||
//Not necessary for single file | ||
}); | ||
|
||
// parse the incoming request containing the form data | ||
form.parse(req); | ||
|
||
}) | ||
} | ||
|
||
|
||
function uploadTargets(req,uploadDir,){ | ||
return new Promise((res,rej)=>{ | ||
// create an incoming form object | ||
var form = new formidable.IncomingForm(); | ||
|
||
// specify that we want to allow the user to upload multiple files in a single request | ||
form.multiples = false; | ||
//Calculate file hash | ||
form.hash = 'md5'; | ||
// store all uploads in the /uploads directory | ||
form.uploadDir = uploadDir; | ||
|
||
// every time a file has been uploaded successfully, | ||
// rename it to it's original name | ||
form.on('file', function(field, file) { | ||
detect.fromFile(file.path,function(err,result){ | ||
if (err) rej(err); | ||
if (result===null){ | ||
let destinationDir=path.join(uploadDir,`/${req.params.studyid}/targets`) | ||
let destinationFile=path.join(destinationDir, file.name) | ||
fs.exists(destinationDir, (exists)=>{ | ||
if(exists){ | ||
rename(file.path, destinationFile) | ||
}else{ | ||
fs.mkdir(destinationDir, { recursive: true }, (err)=>{ | ||
if (err){ | ||
rej(err); | ||
}else{ | ||
rename(file.path, destinationFile) | ||
} | ||
}) | ||
} | ||
function rename(inFile,outFile){ | ||
fs.rename(inFile,outFile, (err)=>{ | ||
if(err){ | ||
rej(err); | ||
}else{ | ||
file={hash:form.openedFiles[0].hash, name:form.openedFiles[0].name} | ||
processTargetsFile.getPreview(outFile,0).then(result=>{ | ||
result instanceof Error ? rej(result) : res({filePreview:result,file}); | ||
}) | ||
} | ||
}); | ||
} | ||
}) | ||
}else{ | ||
fs.unlink(file.path, (err)=>{ | ||
err ? rej(err) : res({hash:'',name:"UnsupportedFile"}) | ||
}) | ||
} | ||
}) | ||
}); | ||
|
||
// log any errors that occur | ||
form.on('error', function(err) { | ||
console.log('An error has occured: \n' + err); | ||
rej(err); | ||
}); | ||
|
||
// once all the files have been uploaded, send a response to the client | ||
form.on('end', function() { | ||
//Not necessary for single file | ||
}); | ||
|
||
// parse the incoming request containing the form data | ||
form.parse(req); | ||
|
||
}) | ||
} | ||
|
||
|
||
|
||
module.exports={uploadFile,uploadTargets} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.