express-fileupload
Advanced tools
Comparing version 1.1.7-alpha.2 to 1.1.7-alpha.3
@@ -62,4 +62,4 @@ const Busboy = require('busboy'); | ||
const uploadTimer = new UploadTimer(options.uploadTimeout, () => { | ||
debugLog(options, `Upload timeout ${field}->${filename}, bytes:${getFileSize()}`); | ||
cleanup(); | ||
// After destroy an error event will be emitted and file clean up will be done. | ||
file.destroy(new Error(`Upload timeout ${field}->${filename}, bytes:${getFileSize()}`)); | ||
}); | ||
@@ -87,7 +87,10 @@ | ||
file.on('end', () => { | ||
// Debug logging for a new file upload. | ||
debugLog(options, `Upload finished ${field}->${filename}, bytes:${getFileSize()}`); | ||
const size = getFileSize(); | ||
// Debug logging for file upload ending. | ||
debugLog(options, `Upload finished ${field}->${filename}, bytes:${size}`); | ||
// Reset upload timer in case of end event. | ||
uploadTimer.clear(); | ||
// Add file instance to the req.files | ||
// Do not add file instance to the req.files if original name and size are empty. | ||
// Empty name and zero size indicates empty file field in the posted form. | ||
if (!name && size === 0) return; | ||
req.files = buildFields(req.files, field, fileFactory({ | ||
@@ -97,4 +100,4 @@ buffer: complete(), | ||
tempFilePath: getFilePath(), | ||
size: getFileSize(), | ||
hash: getHash(), | ||
size, | ||
encoding, | ||
@@ -112,5 +115,4 @@ truncated: file.truncated, | ||
file.on('error', (err) => { | ||
// Reset upload timer in case of errors. | ||
uploadTimer.clear(); | ||
debugLog(options, `Error ${field}->${filename}, bytes:${getFileSize()}, error:${err}`); | ||
uploadTimer.clear(); // Reset upload timer in case of errors. | ||
debugLog(options, err); | ||
cleanup(); | ||
@@ -117,0 +119,0 @@ next(); |
@@ -19,15 +19,21 @@ const fs = require('fs'); | ||
const hash = crypto.createHash('md5'); | ||
const writeStream = fs.createWriteStream(tempFilePath); | ||
let fileSize = 0; | ||
let completed = false; | ||
const writePromise = new Promise((resolve, reject) => { | ||
writeStream.on('finish', () => { | ||
resolve(); | ||
let writeStream = false; | ||
let writePromise = Promise.resolve(); | ||
const createWriteStream = () => { | ||
debugLog(options, `Opening write stream for ${fieldname}->${filename}...`); | ||
writeStream = fs.createWriteStream(tempFilePath); | ||
writePromise = new Promise((resolve, reject) => { | ||
writeStream.on('finish', () => { | ||
resolve(); | ||
}); | ||
writeStream.on('error', (err) => { | ||
debugLog(options, `Error write temp file: ${err}`); | ||
reject(err); | ||
}); | ||
}); | ||
writeStream.on('error', (err) => { | ||
debugLog(options, `Error write temp file: ${err}`); | ||
reject(err); | ||
}); | ||
}); | ||
}; | ||
@@ -40,2 +46,3 @@ return { | ||
} | ||
if (writeStream === false) createWriteStream(); | ||
writeStream.write(data); | ||
@@ -50,5 +57,5 @@ hash.update(data); | ||
complete: () => { | ||
completed = true; | ||
debugLog(options, `Upload ${fieldname}->${filename} completed, bytes:${fileSize}.`); | ||
writeStream.end(); | ||
completed = true; | ||
if (writeStream !== false) writeStream.end(); | ||
// Return empty buff since data was uploaded into a temp file. | ||
@@ -58,6 +65,11 @@ return Buffer.concat([]); | ||
cleanup: () => { | ||
debugLog(options, `Cleaning up temporary file ${tempFilePath}...`); | ||
writeStream.end(); | ||
completed = true; | ||
deleteFile(tempFilePath, (err) => { if (err) throw err; }); | ||
if (writeStream !== false) { | ||
debugLog(options, `Cleaning up temporary file ${tempFilePath}...`); | ||
writeStream.end(); | ||
deleteFile(tempFilePath, err => (err | ||
? debugLog(options, `Cleaning up temporary file ${tempFilePath} failed: ${err}`) | ||
: debugLog(options, `Cleaning up temporary file ${tempFilePath} done.`) | ||
)); | ||
} | ||
}, | ||
@@ -64,0 +76,0 @@ getWritePromise: () => writePromise |
@@ -11,3 +11,3 @@ 'use strict'; | ||
// Parameters which used to generate unique temporary file names: | ||
// Parameters to generate unique temporary file names: | ||
const TEMP_COUNTER_MAX = 65536; | ||
@@ -20,9 +20,9 @@ const TEMP_PREFIX = 'tmp'; | ||
* @param {Object} options - options object. | ||
* @param {String} msg - message to log. | ||
* @returns {Boolean} | ||
* @param {string} msg - message to log. | ||
* @returns {boolean} - false if debug is off. | ||
*/ | ||
const debugLog = (options, msg) => { | ||
options = options || {}; | ||
if (!options.debug) return false; | ||
console.log(msg); // eslint-disable-line | ||
const opts = options || {}; | ||
if (!opts.debug) return false; | ||
console.log(`Express-file-upload: ${msg}`); // eslint-disable-line | ||
return true; | ||
@@ -33,14 +33,13 @@ }; | ||
* Generates unique temporary file name like: tmp-5000-156788789789. | ||
* @param prefix {String} - a prefix for generated unique file name. | ||
* @returns {String} | ||
* @param {string} prefix - a prefix for generated unique file name. | ||
* @returns {string} | ||
*/ | ||
const getTempFilename = (prefix) => { | ||
prefix = prefix || TEMP_PREFIX; | ||
tempCounter = tempCounter >= TEMP_COUNTER_MAX ? 1 : tempCounter + 1; | ||
return `${prefix}-${tempCounter}-${Date.now()}`; | ||
return `${prefix || TEMP_PREFIX}-${tempCounter}-${Date.now()}`; | ||
}; | ||
/** | ||
* Returns true if argument is a function. | ||
* @returns {Boolean} | ||
* isFunc- check if argument is a function. | ||
* @returns {boolean} - Returns true if argument is a function. | ||
*/ | ||
@@ -71,3 +70,3 @@ const isFunc = func => func && func.constructor && func.call && func.apply ? true: false; | ||
if (!options || typeof options !== 'object') return; | ||
Object.keys(options).forEach(key => result[key] = options[key]); | ||
Object.keys(options).forEach(i => result[i] = options[i]); | ||
}); | ||
@@ -80,4 +79,4 @@ return result; | ||
* @param {Object} instance - request object. | ||
* @param {String} field - field name. | ||
* @param value - field value. | ||
* @param {string} field - field name. | ||
* @param {any} value - field value. | ||
* @returns {Object} | ||
@@ -92,9 +91,9 @@ */ | ||
instance[field] = value; | ||
return instance; | ||
} | ||
// Array fields | ||
if (instance[field] instanceof Array) { | ||
instance[field].push(value); | ||
} else { | ||
// Array fields | ||
if (instance[field] instanceof Array) { | ||
instance[field].push(value); | ||
} else { | ||
instance[field] = [instance[field], value]; | ||
} | ||
instance[field] = [instance[field], value]; | ||
} | ||
@@ -107,4 +106,4 @@ return instance; | ||
* @param {Object} fileUploadOptions | ||
* @param {String} filePath | ||
* @returns {Boolean} | ||
* @param {string} filePath | ||
* @returns {boolean} | ||
*/ | ||
@@ -126,3 +125,3 @@ const checkAndMakeDir = (fileUploadOptions, filePath) => { | ||
* Delete file. | ||
* @param {String} file - Path to the file to delete. | ||
* @param {string} file - Path to the file to delete. | ||
*/ | ||
@@ -133,4 +132,4 @@ const deleteFile = (file, callback) => fs.unlink(file, err => err ? callback(err) : callback()); | ||
* Copy file via streams | ||
* @param {String} src - Path to the source file | ||
* @param {String} dst - Path to the destination file. | ||
* @param {string} src - Path to the source file | ||
* @param {string} dst - Path to the destination file. | ||
*/ | ||
@@ -162,4 +161,4 @@ const copyFile = (src, dst, callback) => { | ||
* Firstly trying to rename the file if no luck copying it to dst and then deleteing src. | ||
* @param {String} src - Path to the source file | ||
* @param {String} dst - Path to the destination file. | ||
* @param {string} src - Path to the source file | ||
* @param {string} dst - Path to the destination file. | ||
* @param {Function} callback - A callback function. | ||
@@ -175,3 +174,3 @@ */ | ||
* @param {Buffer} buffer - buffer to save to a file. | ||
* @param {String} filePath - path to a file. | ||
* @param {string} filePath - path to a file. | ||
*/ | ||
@@ -208,5 +207,5 @@ const saveBufferToFile = (buffer, filePath, callback) => { | ||
* Parses filename and extension and returns object {name, extension}. | ||
* @param preserveExtension {Boolean, Integer} - true/false or number of characters for extension. | ||
* @param fileName {String} - file name to parse. | ||
* @returns {Object} - {name, extension}. | ||
* @param {boolean|integer} preserveExtension - true/false or number of characters for extension. | ||
* @param {string} fileName - file name to parse. | ||
* @returns {Object} - { name, extension }. | ||
*/ | ||
@@ -213,0 +212,0 @@ const parseFileNameExtension = (preserveExtension, fileName) => { |
{ | ||
"name": "express-fileupload", | ||
"version": "1.1.7-alpha.2", | ||
"version": "1.1.7-alpha.3", | ||
"author": "Richard Girges <richardgirges@gmail.com>", | ||
@@ -32,3 +32,3 @@ "description": "Simple express file upload middleware that wraps around Busboy", | ||
"body-parser": "^1.19.0", | ||
"coveralls": "^3.0.9", | ||
"coveralls": "^3.0.11", | ||
"eslint": "^6.8.0", | ||
@@ -38,3 +38,3 @@ "express": "^4.17.1", | ||
"md5": "^2.2.1", | ||
"mocha": "^7.0.1", | ||
"mocha": "^7.1.1", | ||
"rimraf": "^3.0.2", | ||
@@ -41,0 +41,0 @@ "supertest": "^4.0.2" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1212046
2238