Skip to content

Commit

Permalink
Adding safeFileName option. This closes #13
Browse files Browse the repository at this point in the history
  • Loading branch information
richardgirges committed Jan 14, 2017
1 parent 35c34e0 commit 06da130
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
var busboy = require('connect-busboy'),
fs = require('fs-extra'),
streamifier = require('streamifier');
var busboy = require('connect-busboy');
var fs = require('fs-extra');
var streamifier = require('streamifier');

module.exports = function(options) {
options = options || {};
Expand All @@ -21,9 +21,11 @@ module.exports = function(options) {

req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
var buf = new Buffer(0);
var safeFileNameRegex = /[^\w-]/g;

file.on('data', function(data) {
buf = Buffer.concat([buf, data]);

if (options.debug) {
return console.log('Uploading %s -> %s', fieldname, filename);
}
Expand All @@ -33,14 +35,21 @@ module.exports = function(options) {
if (!req.files)
req.files = {};


// 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)
return;

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

filename = filename.replace(safeFileNameRegex, '');

console.log('filename yo', filename);
}

return req.files[fieldname] = {
name: filename,
data: buf,
Expand All @@ -58,8 +67,6 @@ module.exports = function(options) {
});
}
};


});
});

Expand Down

0 comments on commit 06da130

Please sign in to comment.