Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Use HTML5 FileAPI properties for File class

This does not break backwards compatiblity yet, but future
versions will remove the old property alias.

See: https://github.com/felixge/node-formidable/issues#issue/32
  • Loading branch information...
commit 29716c1ccd8a415742e0a01c2f26e767909b284c 1 parent 62dc04b
@felixge authored
View
26 lib/formidable/file.js
@@ -7,20 +7,36 @@ var util = require('./util'),
function File(properties) {
EventEmitter.call(this);
- this.length = 0;
+ this.size = 0;
this.path = null;
- this.filename = null;
- this.mime = null;
+ this.name = null;
+ this.type = null;
this._writeStream = null;
for (var key in properties) {
this[key] = properties[key];
}
+
+ this._backwardsCompatibility();
}
module.exports = File;
util.inherits(File, EventEmitter);
+// @todo Next release: Show error messages when accessing these
+File.prototype._backwardsCompatibility = function() {
+ var self = this;
+ this.__defineGetter__('length', function() {
+ return self.size;
+ });
+ this.__defineGetter__('filename', function() {
+ return self.name;
+ });
+ this.__defineGetter__('mime', function() {
+ return self.type;
+ });
+};
+
File.prototype.open = function() {
this._writeStream = new WriteStream(this.path);
};
@@ -28,8 +44,8 @@ File.prototype.open = function() {
File.prototype.write = function(buffer, cb) {
var self = this;
this._writeStream.write(buffer, function() {
- self.length += buffer.length;
- self.emit('progress', self.length);
+ self.size += buffer.length;
+ self.emit('progress', self.size);
cb();
});
};
View
4 lib/formidable/incoming_form.js
@@ -167,8 +167,8 @@ IncomingForm.prototype.handlePart = function(part) {
var file = new File({
path: this._uploadPath(part.filename),
- filename: part.filename,
- mime: part.mime,
+ name: part.filename,
+ type: part.mime,
});
this.emit('fileBegin', part.name, file);
View
14 test/simple/test-file.js
@@ -15,10 +15,10 @@ function test(test) {
test(function constructor() {
assert.ok(file instanceof EventEmitter);
- assert.strictEqual(file.length, 0);
+ assert.strictEqual(file.size, 0);
assert.strictEqual(file.path, null);
- assert.strictEqual(file.filename, null);
- assert.strictEqual(file.mime, null);
+ assert.strictEqual(file.name, null);
+ assert.strictEqual(file.type, null);
assert.strictEqual(file._writeStream, null);
@@ -55,22 +55,22 @@ test(function write() {
gently.expect(file, 'emit', function (event, bytesWritten) {
assert.equal(event, 'progress');
- assert.equal(bytesWritten, file.length);
+ assert.equal(bytesWritten, file.size);
});
CB_STUB = gently.expect(function writeCb() {
- assert.equal(file.length, 10);
+ assert.equal(file.size, 10);
});
cb();
gently.expect(file, 'emit', function (event, bytesWritten) {
assert.equal(event, 'progress');
- assert.equal(bytesWritten, file.length);
+ assert.equal(bytesWritten, file.size);
});
CB_STUB = gently.expect(function writeCb() {
- assert.equal(file.length, 20);
+ assert.equal(file.size, 20);
});
cb();
View
4 test/simple/test-incoming-form.js
@@ -595,8 +595,8 @@ test(function handlePart() {
gently.expect(FileStub, 'new', function(properties) {
assert.equal(properties.path, PATH);
- assert.equal(properties.filename, PART.filename);
- assert.equal(properties.mime, PART.mime);
+ assert.equal(properties.name, PART.filename);
+ assert.equal(properties.type, PART.mime);
FILE = this;
gently.expect(form, 'emit', function (event, field, file) {
Please sign in to comment.
Something went wrong with that request. Please try again.