express-form-post
Advanced tools
Comparing version
20
index.js
@@ -14,3 +14,3 @@ "use strict"; | ||
} else { | ||
user_options.validateFile = (handlePromise) => handlePromise(); | ||
user_options.validateFile = (fieldname, mimetype, handlePromise) => handlePromise(); | ||
} | ||
@@ -26,3 +26,3 @@ | ||
} else { | ||
user_options.validateBody = (handlePromise) => handlePromise(); | ||
user_options.validateBody = (body, handlePromise) => handlePromise(); | ||
} | ||
@@ -53,9 +53,5 @@ | ||
let customFileMethod = user_options.filename; | ||
user_options.filename = function(originalname, fieldname, mimetype) { | ||
let customName = customFileMethod(originalname, fieldname, mimetype); | ||
if(customName == undefined || customName == "") { | ||
return originalname; // returning the original name that is being uploaded | ||
} | ||
return customName; | ||
}; | ||
user_options.filename = (fileInfo) => { | ||
return new Promise((resolve, reject) => customFileMethod(fileInfo, resolve)); | ||
} | ||
} else { | ||
@@ -65,7 +61,9 @@ switch(user_options.filename) { | ||
case "": | ||
user_options.filename = (originalname) => { return hasha(Date.now() + originalname); }; | ||
user_options.filename = (originalname) => { return Promise.resolve(hasha(Date.now() + originalname)); }; | ||
break; | ||
default: | ||
var user_input = user_options.filename; // Closures are awesome | ||
user_options.filename = () => { return user_input; }; | ||
user_options.filename = (fileInfo) => { | ||
return new Promise((resolve, reject) => user_input(fileInfo, resolve)); | ||
} | ||
} | ||
@@ -72,0 +70,0 @@ } |
@@ -19,44 +19,53 @@ const hasha = require("hasha"); | ||
// handlePromise is cb in function declaration | ||
const handlePromise = (valid) => { | ||
valid == false ? reject((new Error("Validation error by custom validateFile function"))) : ( | ||
resolve() | ||
); | ||
const handlePromise = (validFlag) => { | ||
if(validFlag == false) { | ||
reject(new Error("Validation error by custom validateFile function")); | ||
} else if(validFlag instanceof Error) { | ||
reject(validFlag); | ||
} else { | ||
resolve(); | ||
} | ||
}; | ||
this.opts.validateFile(handlePromise, fieldname, mimetype); | ||
this.opts.validateFile(fieldname, mimetype, handlePromise); | ||
}) | ||
.then(() => { | ||
let fileInfo = { | ||
originalname: originalname, | ||
fieldname: fieldname, | ||
mimetype: mimetype, | ||
encoding: encoding | ||
} | ||
// user may use filename function but incorrectly return nothing. no warning supplied. defaults to hash | ||
let save_filename = this.opts.filename(originalname, fieldname, mimetype); | ||
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("/")))) | ||
): ""; | ||
this.opts.filename(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("/")))) | ||
): ""; | ||
let uploadInfo = { | ||
directory: this.opts.directory, | ||
filename: save_filename, | ||
mimetype: mimetype, | ||
fieldname: fieldname, | ||
file: file, | ||
api: this.opts.api | ||
}; | ||
let uploadInfo = { | ||
directory: this.opts.directory, | ||
filename: save_filename, | ||
mimetype: mimetype, | ||
fieldname: fieldname, | ||
file: file, | ||
encoding: encoding, | ||
api: this.opts.api | ||
}; | ||
// init duplex stream (read/writable) or concat-stream depending on store method | ||
this.storeMethod(uploadInfo, req) | ||
.then((file_contents) => { | ||
req.efp._data[fieldname].stream = file_contents; | ||
// do not end through pipe, since streams should only end through efp finished method | ||
file.pipe(file_contents, { end: false }); | ||
file.on("data", (data) => { | ||
if(!req.efp._finished) req.efp._data[fieldname].size += data.length; | ||
// init duplex stream (read/writable) or concat-stream depending on store method | ||
this.storeMethod(uploadInfo, req) | ||
.then((file_contents) => { | ||
req.efp._data[fieldname].stream = file_contents; | ||
// do not end through pipe, since streams should only end through efp finished method | ||
file.pipe(file_contents, { end: false }); | ||
file.on("data", (data) => { | ||
if(!req.efp._finished) req.efp._data[fieldname].size += data.length; | ||
}); | ||
file.on("error", (err) => req.efp.handleError(err)); | ||
}) | ||
.catch((err) => { | ||
req.efp.handleError(err); | ||
}); | ||
file.on("error", (err) => req.efp.handleError(err)); | ||
}) | ||
.catch((err) => { | ||
req.efp.handleError(err); | ||
}).catch((err) => { | ||
req.efp.handleError(err); // this probably will not trigger | ||
}); | ||
}) | ||
@@ -84,6 +93,12 @@ .catch((err) => { | ||
new Promise((resolve, reject) => { | ||
const handlePromise = (flag) => { | ||
flag == false ? reject(new Error("Validation failed on validateBody function")) : resolve(); | ||
const handlePromise = (validFlag) => { | ||
if(validFlag == false) { | ||
reject(new Error("Validation failed on validateBody function")); | ||
} else if(validFlag instanceof Error) { | ||
reject(validFlag); | ||
} else { | ||
resolve(); | ||
} | ||
}; | ||
this.opts.validateBody(handlePromise, req.body); | ||
this.opts.validateBody(req.body, handlePromise); | ||
}) | ||
@@ -90,0 +105,0 @@ .then(() => { |
{ | ||
"name": "express-form-post", | ||
"version": "1.1.5", | ||
"version": "1.2.0", | ||
"description": "Simple, reliable and memory efficient http file parse and upload api", | ||
@@ -27,3 +27,4 @@ "main": "index.js", | ||
"request", | ||
"body" | ||
"body", | ||
"validate body" | ||
], | ||
@@ -30,0 +31,0 @@ "author": "Danny Cho (dannycho7)", |
@@ -8,5 +8,3 @@ # Express Form Post | ||
Many bugs were addressed in v1.1.4, so if you have an older version please consider switching to the latest. | ||
## Why use this module? | ||
@@ -96,6 +94,6 @@ * Easy setup | ||
maxfileSize: 10000, | ||
filename: function(originalname, fieldname, mimetype) { | ||
return Date.now() + "-" + originalname; | ||
filename: function(file, cb) { | ||
cb(Date.now() + "-" + "this is the file name"); | ||
}, | ||
validateFile: function(cb, fieldname, mimetype) { | ||
validateFile: function(fieldname, mimetype, cb) { | ||
console.log(mimetype); | ||
@@ -107,3 +105,3 @@ if(mimetype != "application/pdf") { | ||
}, | ||
validateBody: function(cb, body) { | ||
validateBody: function(body, cb) { | ||
// validates password length before uploading file | ||
@@ -136,4 +134,4 @@ if(body.password.length > 7) { | ||
maxfileSize: 100000, | ||
filename: function(originalname, fieldname, mimetype) { | ||
return originalname; | ||
filename: function(file, cb) { | ||
cb(file.originalname); | ||
}, | ||
@@ -166,4 +164,4 @@ api: { | ||
store: "dropbox", | ||
filename: function(originalname, fieldname, mimetype) { | ||
return originalname; | ||
filename: function(file, cb) { | ||
cb(file.originalname); | ||
}, | ||
@@ -197,4 +195,4 @@ api: { | ||
`minfileSize` | Minimum Size of the uploaded file in bytes | defaults to 0 | ||
`validateFile` | function to validate uploaded file | takes two optional parameters: fieldname and mimetype | ||
`validateBody` | function to validate the body of the request before storing the validated file | request body is the parameter | ||
`validateFile` | function to validate uploaded file | takes params: fieldname, mimetype, callback | ||
`validateBody` | function to validate the body of the request before storing the validated file | takes params: body, callback | ||
`api` | api configuration information (api keys) | read further documentation for specifications | ||
@@ -210,3 +208,3 @@ | ||
#### validateBody(callback, body) | ||
#### validateBody(body, callback) | ||
The validateBody method validates the request's body before sending off your file to the specified store. This is especially helpful for handling signups that require uploading some type of file (e.g a resume). For example, if the user signs up without filling in the proper fields, you can cancel the file upload (saves api requests and creates faster responses for errors). Here is an example with validating that a field called 'username' was sent. | ||
@@ -216,3 +214,3 @@ | ||
const formPost = efp({ | ||
validateBody: function(cb, body) { | ||
validateBody: function(body, cb) { | ||
if(body.username == undefined) { | ||
@@ -226,3 +224,3 @@ return cb(false); | ||
#### validateFile(fieldname, mimetype) | ||
#### validateFile(fieldname, mimetype, callback) | ||
The validateFile method validates the file data itself. An example use case would be checking if the file is a pdf. This particular example is listed below. | ||
@@ -232,3 +230,3 @@ | ||
const formPost = efp({ | ||
validateFile: function(fieldname, mimetype) { | ||
validateFile: function(fieldname, mimetype, cb) { | ||
if(mimetype != "application/pdf") { | ||
@@ -268,3 +266,3 @@ return cb(false); | ||
--- | --- | --- | ||
`accessKeyId` | AWS access key id | **Required** You can find it here : [aws console](https://aws.amazon.com/console/) | ||
`accessKeyId` | AWS access key id | Optional if already set through aws.config.update. You can find it here : [aws console](https://aws.amazon.com/console/) | ||
`secretAccessKey` | secret key for aws | Optional based on your s3 settings | ||
@@ -271,0 +269,0 @@ `bucketName` | The name of your bucket. | **Required** |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
124122
0.19%3368
0.48%0
-100%273
-0.73%