Permalink
Browse files

Merge #148 - hash support

Closes #148
  • Loading branch information...
2 parents 3cf2817 + c331714 commit c6ad572559771997d70a835285c8bb2158eb13cb @felixge committed Jun 4, 2012
Showing with 27 additions and 2 deletions.
  1. +8 −0 Readme.md
  2. +14 −2 lib/file.js
  3. +2 −0 lib/incoming_form.js
  4. +3 −0 test/legacy/system/test-multi-video-upload.js
View
@@ -199,6 +199,10 @@ Limits the amount of memory a field (not file) can allocate in bytes.
If this value is exceeded, an `'error'` event is emitted. The default
size is 2MB.
+__incomingForm.hash = false__
+
+If you want checksums calculated for incoming files, set this to either `'sha1'` or `'md5'`.
+
__incomingForm.bytesReceived__
The amount of bytes received for this form so far.
@@ -290,6 +294,10 @@ __file.lastModifiedDate = null__
A date object (or `null`) containing the time this file was last written to. Mostly
here for compatibility with the [W3C File API Draft](http://dev.w3.org/2006/webapi/FileAPI/).
+__file.hash = null__
+
+If hash calculation was set, you can read the hex digest out of this var.
+
## License
Formidable is licensed under the MIT license.
View
@@ -2,7 +2,8 @@ if (global.GENTLY) require = GENTLY.hijack(require);
var util = require('./util'),
WriteStream = require('fs').WriteStream,
- EventEmitter = require('events').EventEmitter;
+ EventEmitter = require('events').EventEmitter,
+ crypto = require('crypto');
function File(properties) {
EventEmitter.call(this);
@@ -11,14 +12,19 @@ function File(properties) {
this.path = null;
this.name = null;
this.type = null;
+ this.hash = null;
this.lastModifiedDate = null;
this._writeStream = null;
-
+
for (var key in properties) {
this[key] = properties[key];
}
+ if(typeof this.hash === 'string') {
+ this.hash = crypto.createHash(properties.hash);
+ }
+
this._backwardsCompatibility();
}
module.exports = File;
@@ -45,6 +51,9 @@ File.prototype.open = function() {
File.prototype.write = function(buffer, cb) {
var self = this;
this._writeStream.write(buffer, function() {
+ if(self.hash) {
+ self.hash.update(buffer);
+ }
self.lastModifiedDate = new Date();
self.size += buffer.length;
self.emit('progress', self.size);
@@ -55,6 +64,9 @@ File.prototype.write = function(buffer, cb) {
File.prototype.end = function(cb) {
var self = this;
this._writeStream.end(function() {
+ if(self.hash) {
+ self.hash = self.hash.digest('hex');
+ }
self.emit('end');
cb();
});
View
@@ -25,6 +25,7 @@ function IncomingForm(opts) {
this.encoding = opts.encoding || 'utf-8';
this.headers = null;
this.type = null;
+ this.hash = false;
this.bytesReceived = null;
this.bytesExpected = null;
@@ -190,6 +191,7 @@ IncomingForm.prototype.handlePart = function(part) {
path: this._uploadPath(part.filename),
name: part.filename,
type: part.mime,
+ hash: self.hash
});
this.emit('fileBegin', part.name, file);
@@ -12,6 +12,7 @@ server.on('request', function(req, res) {
uploads = {};
form.uploadDir = TEST_TMP;
+ form.hash = 'sha1';
form.parse(req);
form
@@ -41,10 +42,12 @@ server.on('request', function(req, res) {
assert.ok(uploads['shortest_video.flv']);
assert.ok(uploads['shortest_video.flv'].ended);
assert.ok(uploads['shortest_video.flv'].progress.length > 3);
+ assert.equal(uploads['shortest_video.flv'].file.hash, 'd6a17616c7143d1b1438ceeef6836d1a09186b3a');
assert.equal(uploads['shortest_video.flv'].progress.slice(-1), uploads['shortest_video.flv'].file.length);
assert.ok(uploads['shortest_video.mp4']);
assert.ok(uploads['shortest_video.mp4'].ended);
assert.ok(uploads['shortest_video.mp4'].progress.length > 3);
+ assert.equal(uploads['shortest_video.mp4'].file.hash, '937dfd4db263f4887ceae19341dcc8d63bcd557f');
server.close();
res.writeHead(200);

0 comments on commit c6ad572

Please sign in to comment.