Skip to content

Commit

Permalink
Use HTML5 FileAPI properties for File class
Browse files Browse the repository at this point in the history
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
felixge committed Jan 18, 2011
1 parent 62dc04b commit 29716c1
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
26 changes: 21 additions & 5 deletions lib/formidable/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,45 @@ 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);
};

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();
});
};
Expand Down
4 changes: 2 additions & 2 deletions lib/formidable/incoming_form.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions test/simple/test-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions test/simple/test-incoming-form.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 29716c1

Please sign in to comment.