Skip to content

Commit

Permalink
Updated lib/index.js to be lint-happy. Added promise-based API for `.…
Browse files Browse the repository at this point in the history
…mv` method. This fixes #42
  • Loading branch information
richardgirges committed Aug 28, 2017
1 parent 07989d2 commit 9bf6e61
Showing 1 changed file with 55 additions and 22 deletions.
77 changes: 55 additions & 22 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ module.exports = function(options) {
options = options || {};

return function(req, res, next) {
if (!hasBody(req) || !hasAcceptableMethod(req) || !hasAcceptableMime(req))
if (!hasBody(req) || !hasAcceptableMethod(req) || !hasAcceptableMime(req)) {
return next();
}

processMultipart(options, req, res, next);
};
Expand Down Expand Up @@ -56,11 +57,13 @@ function processMultipart(options, req, res, next) {

let prev = req.body[fieldname];

if (!prev)
if (!prev) {
return req.body[fieldname] = val;
}

if (Array.isArray(prev))
if (Array.isArray(prev)) {
return prev.push(val);
}

req.body[fieldname] = [prev, val];
});
Expand All @@ -73,34 +76,39 @@ function processMultipart(options, req, res, next) {
file.on('data', function(data) {
buffers.push(data);

if (options.debug)
if (options.debug) {
return console.log('Uploading %s -> %s', fieldname, filename);
}
});

file.on('end', function() {
if (!req.files)
if (!req.files) {
req.files = {};
}

const buf = Buffer.concat(buffers);
// see: https://github.com/richardgirges/express-fileupload/issues/14
// firefox uploads empty file in case of cache miss when f5ing page.
// resulting in unexpected behavior. if there is no file data, the file is invalid.
if (!buf.length)
if (!buf.length) {
return;
}

if (options.safeFileNames) {
let maxExtensionLength = 3;
let extension = '';

if (typeof options.safeFileNames === 'object')
if (typeof options.safeFileNames === 'object') {
safeFileNameRegex = options.safeFileNames;
}

maxExtensionLength = parseInt(options.preserveExtension);
if (options.preserveExtension || maxExtensionLength === 0) {
if (isNaN(maxExtensionLength))
if (isNaN(maxExtensionLength)) {
maxExtensionLength = 3;
else
} else {
maxExtensionLength = Math.abs(maxExtensionLength);
}

let filenameParts = filename.split('.');
let filenamePartsLen = filenameParts.length;
Expand All @@ -126,20 +134,44 @@ function processMultipart(options, req, res, next) {
data: buf,
encoding: encoding,
mimetype: mime,
mv: function(path, callback) {
let fstream = fs.createWriteStream(path);
mv: function(path, callback = null) {
// Callback is passed in, use the callback API
if (callback) {
doMove(
() => {
callback(null);
},
(error) => {
callback(error);
}
);

// Otherwise, return a promise
} else {
return new Promise((resolve, reject) => {
doMove(resolve, reject);
});
}

streamifier.createReadStream(buf).pipe(fstream);
/**
* Local function that moves the file to a different location on the filesystem
* Takes two function arguments to make it compatible w/ Promise or Callback APIs
* @param {Function} successFunc
* @param {Function} errorFunc
*/
function doMove(successFunc, errorFunc) {
const fstream = fs.createWriteStream(path);

fstream.on('error', function(error) {
if (callback)
callback(error);
});
streamifier.createReadStream(buf).pipe(fstream);

fstream.on('close', function() {
if (callback)
callback(null);
});
fstream.on('error', function(error) {
errorFunc(error);
});

fstream.on('close', function() {
successFunc();
});
}
}
};

Expand All @@ -148,10 +180,11 @@ function processMultipart(options, req, res, next) {
req.files[fieldname] = newFile;
} else {
// Array fields
if (req.files[fieldname] instanceof Array)
if (req.files[fieldname] instanceof Array) {
req.files[fieldname].push(newFile);
else
} else {
req.files[fieldname] = [req.files[fieldname], newFile];
}
}
});

Expand Down

0 comments on commit 9bf6e61

Please sign in to comment.