Skip to content

Commit

Permalink
Add "preserverExtension" option handler
Browse files Browse the repository at this point in the history
Misc reformatting.
  • Loading branch information
pronein committed Apr 28, 2017
1 parent 63c759a commit 26e009b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ Pass in non-Busboy options directly to the middleware. These are express-fileupl
Option | Acceptable Values | Details
--- | --- | ---
safeFileNames | <ul><li><code>false</code>&nbsp;**(default)**</li><li><code>true</code></li><li>regex</li></ul> | Strips characters from the upload's filename. You can use custom regex to determine what to strip. If set to `true`, non-alphanumeric characters _except_ dashes and underscores will be stripped. This option is off by default.<br /><br />**Example #1 (strip slashes from file names):** `app.use(fileUpload({ safeFileNames: /\\/g }))`<br />**Example #2:** `app.use(fileUpload({ safeFileNames: true }))`
preserveExtension | <ul><li><code>false</code>&nbsp;**(default)**</li><li><code>true</code></li><li><code>*Number*</code></li></ul> | Preserves filename extension when using <code>safeFileNames</code> option. If set to <code>true</code>, will default to an extension length of 3. If set to <code>*Number*</code>, this will be the max allowable extension length. If an extension is smaller than the extension length, it remains untouched. If the extension is longer, it is shifted.<br /><br />**Example #1 (true):**<br /><code>app.use(fileUpload({ safeFileNames: true, preserveExtension: true }));</code><br />*myFileName.ext* --> *myFileName.ext*<br /><br />**Example #2 (max extension length 2, extension truncated):**<br /><code>app.use(fileUpload({ safeFileNames: true, preserveExtension: 2 }));</code><br />*myFileName.ext* --> *myFileNamee.xt*

# Help Wanted
Pull Requests are welcomed!
Expand Down
33 changes: 28 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module.exports = function(options) {

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

processMultipart(options, req, res, next);
};
Expand Down Expand Up @@ -85,14 +85,37 @@ function processMultipart(options, req, res, next) {
// 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 extensionLength = 3;
let extension = '';

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

filename = filename.replace(safeFileNameRegex, '');
if (options.preserveExtension) {
if (typeof options.preserveExtension === 'number')
extensionLength = options.preserveExtension;

let filenameParts = filename.split('.');
let filenamePartsLen = filenameParts.length;
if (filenamePartsLen > 1) {
extension = filenameParts.pop();

if (extension.length > extensionLength) {
filenameParts[filenameParts.length - 1] +=
'.' + extension.substr(0, extension.length - extensionLength);
extension = extension.substr(-extensionLength);
}

extension = '.' + extension.replace(safeFileNameRegex, '');
filename = filenameParts.join('.');
}
}

filename = filename.replace(safeFileNameRegex, '').concat(extension);
}

let newFile = {
Expand Down Expand Up @@ -123,9 +146,9 @@ function processMultipart(options, req, res, next) {
} else {
// Array fields
if (req.files[fieldname] instanceof Array)
req.files[fieldname].push(newFile);
req.files[fieldname].push(newFile);
else
req.files[fieldname] = [req.files[fieldname], newFile];
req.files[fieldname] = [req.files[fieldname], newFile];
}
});
});
Expand Down

0 comments on commit 26e009b

Please sign in to comment.