express-form-post
Advanced tools
Comparing version 1.2.1 to 1.2.2
@@ -136,3 +136,3 @@ "use strict"; | ||
return new Promise((resolve, reject) => { | ||
// promiseCallback rejects if called as this.handleError(err) and resolves if called as this.finished(); | ||
// promiseCallback rejects if called as req.efp.handleError(err) and resolves if called as req.efp.finished(); | ||
let promiseCallback = (err) => { | ||
@@ -139,0 +139,0 @@ if(err) { |
const hasha = require("hasha"); | ||
const path = require("path"); | ||
const createHandlePromise = require("./promise").create; | ||
@@ -12,5 +13,3 @@ module.exports = function(busboy, req) { | ||
// Busboy emits limit event before my file listeners can reach it | ||
file.on("limit", () => { | ||
req.efp.handleError(new Error("File limit reached on file")); | ||
}); | ||
file.on("limit", () => req.efp.handleError(new Error("File limit reached on file"))); | ||
@@ -20,11 +19,3 @@ // validate file | ||
// handlePromise is cb in function declaration | ||
const handlePromise = (validFlag) => { | ||
if(validFlag == false) { | ||
reject(new Error("Validation error by custom validateFile function")); | ||
} else if(validFlag instanceof Error) { | ||
reject(validFlag); | ||
} else { | ||
resolve(); | ||
} | ||
}; | ||
const handlePromise = createHandlePromise(resolve, reject); | ||
this.opts.validateFile(fieldname, mimetype, handlePromise); | ||
@@ -41,9 +32,12 @@ }) | ||
this.opts.filename(req, fileInfo).then((save_filename) => { | ||
typeof save_filename == "string" && save_filename.length > 0 ? "" : save_filename = hasha(Date.now() + originalname); | ||
save_filename.includes("/") ? ( | ||
this.opts.directory = path.join(this.opts.directory, save_filename, ".."), | ||
save_filename = path.basename(path.resolve(...(save_filename.split("/")))) | ||
): ""; | ||
if(typeof(save_filename) == "string" && save_filename.length == 0 || save_filename == "/") { | ||
save_filename = hasha(Date.now() + originalname); | ||
} | ||
// user input may include filename with trailing slash | ||
let save_directory = path.join(this.opts.directory, save_filename, ".."); | ||
save_filename = path.basename(save_filename); | ||
let uploadInfo = { | ||
directory: this.opts.directory, | ||
directory: save_directory, | ||
filename: save_filename, | ||
@@ -95,11 +89,3 @@ mimetype: mimetype, | ||
new Promise((resolve, reject) => { | ||
const handlePromise = (validFlag) => { | ||
if(validFlag == false) { | ||
reject(new Error("Validation failed on validateBody function")); | ||
} else if(validFlag instanceof Error) { | ||
reject(validFlag); | ||
} else { | ||
resolve(); | ||
} | ||
}; | ||
const handlePromise = createHandlePromise(resolve, reject); | ||
this.opts.validateBody(req.body, handlePromise); | ||
@@ -112,4 +98,4 @@ }) | ||
if(this.opts.minfileSize > req.efp._data[key].size) { | ||
return req.efp.handleError(new Error("Uploaded file was smaller than minfileSize." | ||
+ " the file was of size " + req.efp._data[key].size)); | ||
let err_msg = "Uploaded file was smaller than minfileSize." + " the file was of size " + req.efp._data[key].size; | ||
return req.efp.handleError(new Error(err_msg)); | ||
} | ||
@@ -116,0 +102,0 @@ } |
@@ -18,6 +18,6 @@ const Busboy = require("busboy"); | ||
/* | ||
* In middleware, this.finished passes on to next middleware | ||
* Validation checking in this.finished because of upload function cb not next param in middleware | ||
* In upload function, this.finished will be the callback with an err parameter. (err be undefined) | ||
* this.finished will be called when finished with parsing the request to pass on to the cb action | ||
* In middleware, req.efp.finished passes on to next middleware | ||
* Validation checking in req.efp.finished because of upload function cb not next param in middleware | ||
* In upload function, req.efp.finished will be the callback with an err parameter. (err be undefined) | ||
* req.efp.finished will be called when finished with parsing the request to pass on to the cb action | ||
* buffers array holds file contents that should be sent to the store if the body is valid | ||
@@ -43,3 +43,3 @@ */ | ||
/* | ||
* A call to this.handleError will nullify any subsequent calls to this.finished and this.handleError | ||
* A call to req.efp.handleError will nullify any subsequent calls to req.efp.finished and req.efp.handleError | ||
* 1st expr resolves to middleware and 2nd to upload | ||
@@ -78,3 +78,3 @@ */ | ||
} catch(err) { | ||
this.handleError(err); | ||
req.efp.handleError(err); | ||
} | ||
@@ -81,0 +81,0 @@ } else { |
{ | ||
"name": "express-form-post", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"description": "Simple, reliable and memory efficient http file parse and upload api", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
15
124139
3362