From 10c57f0d3da5842ad5c40b2174117ec6d05cf8df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Geisend=C3=B6rfer?= Date: Mon, 14 Jun 2010 16:39:30 +0200 Subject: [PATCH 1/2] New feature: form.keepExtensions This is useful if you process uploaded files using comannd line tools that determine the type of a file using its extension. (I will hunt down the authors of these tools so we won't have to rely on file extensions any more in the future) --- Readme.md | 4 +++ lib/formidable/incoming_form.js | 9 +++-- test/simple/test-incoming-form.js | 55 ++++++++++++++++++++++--------- 3 files changed, 51 insertions(+), 17 deletions(-) diff --git a/Readme.md b/Readme.md index d68addc2..171c0d5c 100644 --- a/Readme.md +++ b/Readme.md @@ -78,6 +78,10 @@ The encoding to use for incoming form fields. The directory for placing file uploads in. You can later on move them using `fs.rename()`. +#### incomingForm.keepExtensions = false + +If you want the files written to `incomingForm.uploadDir` to include the extensions of the original files, set this property to `true`. + #### incomingForm.type Either 'multipart' or 'urlencoded' depending on the incoming request. diff --git a/lib/formidable/incoming_form.js b/lib/formidable/incoming_form.js index 758fe516..58e1ae8d 100644 --- a/lib/formidable/incoming_form.js +++ b/lib/formidable/incoming_form.js @@ -14,6 +14,7 @@ function IncomingForm() { this.error = null; this.ended = false; + this.keepExtensions = false; this.uploadDir = '/tmp'; this.encoding = 'utf-8'; this.headers = null; @@ -150,7 +151,7 @@ IncomingForm.prototype.handlePart = function(part) { this._flushing++; - var file = new WriteStream(this._uploadPath()); + var file = new WriteStream(this._uploadPath(part.filename)); part.addListener('data', function(buffer) { self.pause(); file.write(buffer, function() { @@ -308,12 +309,16 @@ IncomingForm.prototype._initUrlencoded = function() { this._parser = parser; }; -IncomingForm.prototype._uploadPath = function() { +IncomingForm.prototype._uploadPath = function(filename) { var name = ''; for (i = 0; i < 32; i++) { name += Math.floor(Math.random() * 16).toString(16); } + if (this.keepExtensions) { + name += path.extname(filename); + } + return path.join(this.uploadDir, name); }; diff --git a/test/simple/test-incoming-form.js b/test/simple/test-incoming-form.js index a006b84d..9f0d9159 100644 --- a/test/simple/test-incoming-form.js +++ b/test/simple/test-incoming-form.js @@ -26,6 +26,7 @@ test(function constructor() { assert.strictEqual(form.ended, false); assert.strictEqual(form.type, null); assert.strictEqual(form.headers, null); + assert.strictEqual(form.keepExtensions, false); assert.strictEqual(form.uploadDir, '/tmp'); assert.strictEqual(form.encoding, 'utf-8'); assert.strictEqual(form.bytesReceived, null); @@ -493,16 +494,20 @@ test(function handlePart() { (function testFilePart() { var PART = new events.EventEmitter() - , FILE = new events.EventEmitter(); + , FILE = new events.EventEmitter() + , PATH = '/foo/bar'; PART.name = 'my_file'; PART.filename = 'sweet.txt'; PART.mime = 'sweet.txt'; - FILE.path = form._uploadPath(); + gently.expect(form, '_uploadPath', function(filename) { + assert.equal(filename, PART.filename); + return PATH; + }); - gently.expect(WriteStreamStub, 'new', function(file) { - assert.equal(path.dirname(file), form.uploadDir); + gently.expect(WriteStreamStub, 'new', function(path) { + assert.equal(path, PATH); FILE = this; }); @@ -543,19 +548,39 @@ test(function handlePart() { }); test(function _uploadPath() { - var UUID_A, UUID_B; - gently.expect(GENTLY.hijacked.path, 'join', function(uploadDir, uuid) { - assert.equal(uploadDir, form.uploadDir); - UUID_A = uuid; - }); - form._uploadPath(); + (function testUniqueId() { + var UUID_A, UUID_B; + gently.expect(GENTLY.hijacked.path, 'join', function(uploadDir, uuid) { + assert.equal(uploadDir, form.uploadDir); + UUID_A = uuid; + }); + form._uploadPath(); - gently.expect(GENTLY.hijacked.path, 'join', function(uploadDir, uuid) { - UUID_B = uuid; - }); - form._uploadPath(); + gently.expect(GENTLY.hijacked.path, 'join', function(uploadDir, uuid) { + UUID_B = uuid; + }); + form._uploadPath(); + + assert.notEqual(UUID_A, UUID_B); + })(); - assert.notEqual(UUID_A, UUID_B); + (function testFileExtension() { + form.keepExtensions = true; + var FILENAME = 'foo.jpg' + , EXT = '.bar'; + + gently.expect(GENTLY.hijacked.path, 'extname', function(filename) { + assert.equal(filename, FILENAME); + gently.restore(path, 'extname'); + + return EXT; + }); + + gently.expect(GENTLY.hijacked.path, 'join', function(uploadDir, name) { + assert.equal(path.extname(name), EXT); + }); + form._uploadPath(FILENAME); + })(); }); test(function _maybeEnd() { From af57e615f944abcaedad160a544c41801a72bbbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Geisend=C3=B6rfer?= Date: Mon, 14 Jun 2010 16:41:17 +0200 Subject: [PATCH 2/2] Bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 9dd1d5e8..9d4b9999 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { "name" : "formidable" -, "version": "0.8.0" +, "version": "0.9.0" , "dependencies": {"gently": ">=0.7.0"} , "directories" : { "lib" : "./lib/formidable" } , "main" : "./lib/formidable/index"