diff --git a/.gitignore b/.gitignore index 45032a13..5deae025 100644 --- a/.gitignore +++ b/.gitignore @@ -2,3 +2,5 @@ *.upload *.un~ /node_modules/* +.idea +npm-debug.log diff --git a/lib/incoming_form.js b/lib/incoming_form.js index 060eac29..e20f1b21 100644 --- a/lib/incoming_form.js +++ b/lib/incoming_form.js @@ -10,12 +10,12 @@ var util = require('./util'), EventEmitter = require('events').EventEmitter, Stream = require('stream').Stream; -function IncomingForm(opts) { +function IncomingForm(opts) { if (!(this instanceof IncomingForm)) return new IncomingForm; EventEmitter.call(this); opts=opts||{}; - + this.error = null; this.ended = false; @@ -27,19 +27,21 @@ function IncomingForm(opts) { this.type = null; this.hash = false; + this.isAutoRename = opts.isAutoRename || true; + this.bytesReceived = null; this.bytesExpected = null; this._parser = null; this._flushing = 0; this._fieldsSize = 0; -}; +} util.inherits(IncomingForm, EventEmitter); exports.IncomingForm = IncomingForm; IncomingForm.UPLOAD_DIR = (function() { var dirs = [process.env.TMP, '/tmp', process.cwd()]; - for (var i = 0; i < dirs.length; i++) { + for (var i = 0, l = dirs.length; i < l; i++) { var dir = dirs[i]; var isDirectory = false; @@ -360,19 +362,29 @@ IncomingForm.prototype._initUrlencoded = function() { }; IncomingForm.prototype._uploadPath = function(filename) { - var name = ''; - for (var i = 0; i < 32; i++) { - name += Math.floor(Math.random() * 16).toString(16); + //if isAutoRename is false + //return filename without modification + var _path = ''; + if(this.isAutoRename === false) { + var execRes = /(?:^.*\/)?(.*)$/.exec(filename); + var fileName = execRes && execRes[1] || filename; + _path = path.join(this.uploadDir, fileName); + } else { + var name = ''; + for (var i = 0; i < 32; i++) { + name += Math.floor(Math.random() * 16).toString(16); + } + + if (this.keepExtensions) { + var ext = path.extname(filename); + ext = ext.replace(/(\.[a-z0-9]+).*/, '$1'); + + name += ext; + } + + _path = path.join(this.uploadDir, name); } - - if (this.keepExtensions) { - var ext = path.extname(filename); - ext = ext.replace(/(\.[a-z0-9]+).*/, '$1') - - name += ext; - } - - return path.join(this.uploadDir, name); + return (_path); }; IncomingForm.prototype._maybeEnd = function() { diff --git a/test/unit/test-incoming-form.js b/test/unit/test-incoming-form.js index fe2ac1c6..11c12373 100644 --- a/test/unit/test-incoming-form.js +++ b/test/unit/test-incoming-form.js @@ -56,6 +56,29 @@ test('IncomingForm', { var ext = path.extname(form._uploadPath('super.bar')); assert.equal(ext, '.bar'); }, + '#_uploadPath with disable rename files (isAutoRename)': function() { + var fileName = "sample.txt"; + + form.isAutoRename = true; + var _path = form._uploadPath(fileName); + assert.notEqual(_path, '/tmp/' + fileName); + + form.isAutoRename = false; + _path = form._uploadPath(fileName); + assert.equal(_path, '/tmp/' + fileName); + + fileName = "my-photo.jpeg"; + + form.isAutoRename = true; + var _path = form._uploadPath('../../' + fileName); + assert.notEqual(_path, '/tmp/' + fileName); + assert.ok(/\/tmp\//.test(_path)); + + form.isAutoRename = false; + _path = form._uploadPath('../../' + fileName); + assert.ok(new RegExp(fileName).test(_path)); + assert.equal(_path, '/tmp/' + fileName); + } }); function makeHeader(filename) {