Feature Request
Use Case
By adding the multipartFileHandler or multipartHandler to the bodyParser options, the ordinary process of formidable does not continue (the form.handlePart(part) would not be called anymore). So, we must write our handling function to save the incoming file for example.
server.use(restify.plugins.bodyParser({
// other options
// ...
multipartFileHandler: function (part) {
const {mime} = part;
const fileType: string = mime.split('/').pop();
if (fileType === 'jpg' || fileType === 'png' || fileType === 'jpeg') {
part.on('data', buffer => {
fs.writeFile(filePath, buffer, (err) => {
if (err) {
console.log(err);
} else {
console.log("The file was saved:", filename);
// form.emit('file', part.name, theSavedFile);
}
});
});
} else {
console.log('incorrect file type:', fileType);
}
},
// ...
});
Besides, this method leaves the request.files to be an empty object {}. As we can not call the form.emit('file', part.name, theSavedFile) to ask the formidable: "the {[part.name]: theSavedFile} was successfully received, please add it the the files object".
Example API
We can easily run this snippet and save the file like a piece of cake.
server.use(restify.plugins.bodyParser({
multiples: true,
uploadDir,
multipartFileHandler: function (part, req, form) { // <-- the third argument can be added
const {mime} = part;
const fileType: string = mime.split('/').pop();
if (fileType === 'jpg' || fileType === 'png' || fileType === 'jpeg') {
form.handlePart(part); // <--- this line can be used.
} else {
console.log('incorrect file type:', fileType);
}
}
})
Are you willing and able to implement this?
It's not so hard. However, I wrote a snippet to inject "form" into the functions.
Feature Request
Use Case
By adding the
multipartFileHandlerormultipartHandlerto thebodyParseroptions, the ordinary process of formidable does not continue (theform.handlePart(part)would not be called anymore). So, we must write our handling function to save the incoming file for example.Besides, this method leaves the
request.filesto be an empty object{}. As we can not call theform.emit('file', part.name, theSavedFile)to ask the formidable: "the{[part.name]: theSavedFile}was successfully received, please add it the thefilesobject".Example API
We can easily run this snippet and save the file like a piece of cake.
Are you willing and able to implement this?
It's not so hard. However, I wrote a snippet to inject "form" into the functions.