Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

add File#toJSON() #184

Merged
merged 2 commits into from

3 participants

@tj
tj commented

void of ._writeStream

@tj tj add File#toJSON()
void of ._writeStream
1dd3767
@svnlto
Collaborator

cool. would you be able to add a quick test for this before we merge this?

@tj
tj commented

yup, dont have time ATM but I can after work

@svnlto
Collaborator

@visionmedia i suppose you haven't had time to look at this yet?

@felixge felixge commented on the diff
lib/file.js
@@ -32,6 +32,20 @@ File.prototype.open = function() {
this._writeStream = new WriteStream(this.path);
};
+File.prototype.toJSON = function() {
+ return {
+ size: this.size,
+ path: this.path,
+ name: this.name,
+ type: this.type,
+ hash: this.hash,
+ mtime: this.lastModifiedDate,
+ length: this.length,
+ filename: this.filename,
+ mime: this.mime
+ }
@felixge Owner
felixge added a note

nitpick: missing semicolon ; )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@felixge
Owner

I'm ok with merging this as is. But in order for it to become official API it needs to have:

  • one test
  • mention in the docs
@tj
tj commented

done! let me know if you want other changes

@svnlto
Collaborator

i think this is sufficient enough. thanks!

@svnlto svnlto closed this
@svnlto svnlto reopened this
@svnlto svnlto merged commit 799c7a4 into from
@felixge
Owner

Sweet, thanks @visionmedia !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 17, 2012
  1. @tj

    add File#toJSON()

    tj authored
    void of ._writeStream
Commits on Oct 31, 2012
  1. @tj

    add File#toJSON() tests and docs

    tj authored
This page is out of date. Refresh to see the latest.
Showing with 51 additions and 0 deletions.
  1. +5 −0 Readme.md
  2. +13 −0 lib/file.js
  3. +33 −0 test/unit/test-file.js
View
5 Readme.md
@@ -182,6 +182,11 @@ here for compatibility with the [W3C File API Draft](http://dev.w3.org/2006/weba
If hash calculation was set, you can read the hex digest out of this var.
+#### Formidable.File#toJSON()
+
+ This method returns a JSON-representation of the file, allowing you to
+ `JSON.stringify()` the file which is useful for logging and responding
+ to requests.
### Events
View
13 lib/file.js
@@ -32,6 +32,19 @@ File.prototype.open = function() {
this._writeStream = new WriteStream(this.path);
};
+File.prototype.toJSON = function() {
+ return {
+ size: this.size,
+ path: this.path,
+ name: this.name,
+ type: this.type,
+ mtime: this.lastModifiedDate,
+ length: this.length,
+ filename: this.filename,
+ mime: this.mime
+ };
@felixge Owner
felixge added a note

nitpick: missing semicolon ; )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+};
+
File.prototype.write = function(buffer, cb) {
var self = this;
this._writeStream.write(buffer, function() {
View
33 test/unit/test-file.js
@@ -0,0 +1,33 @@
+var common = require('../common');
+var test = require('utest');
+var assert = common.assert;
+var File = common.require('file');
+
+var file;
+var now = new Date;
+test('IncomingForm', {
+ before: function() {
+ file = new File({
+ size: 1024,
+ path: '/tmp/cat.png',
+ name: 'cat.png',
+ type: 'image/png',
+ lastModifiedDate: now,
+ filename: 'cat.png',
+ mime: 'image/png'
+ })
+ },
+
+ '#toJSON()': function() {
+ var obj = file.toJSON();
+ var len = Object.keys(obj).length;
+ assert.equal(1024, obj.size);
+ assert.equal('/tmp/cat.png', obj.path);
+ assert.equal('cat.png', obj.name);
+ assert.equal('image/png', obj.type);
+ assert.equal('image/png', obj.mime);
+ assert.equal('cat.png', obj.filename);
+ assert.equal(now, obj.mtime);
+ assert.equal(len, 8);
+ }
+});
Something went wrong with that request. Please try again.