Skip to content

Commit d2497a4

Browse files
sreuterfelixge
authored andcommitted
Added option to calculate checksums for incoming files
1 parent bcaa95d commit d2497a4

File tree

3 files changed

+23
-4
lines changed

3 files changed

+23
-4
lines changed

Readme.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,10 @@ Limits the amount of memory a field (not file) can allocate in bytes.
199199
If this value is exceeded, an `'error'` event is emitted. The default
200200
size is 2MB.
201201

202+
__incomingForm.hash = false__
203+
204+
If you want checksums calculated for incoming files, set this to either `'sha1'` or `'md5'`.
205+
202206
__incomingForm.bytesReceived__
203207

204208
The amount of bytes received for this form so far.

lib/file.js

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ if (global.GENTLY) require = GENTLY.hijack(require);
22

33
var util = require('./util'),
44
WriteStream = require('fs').WriteStream,
5-
EventEmitter = require('events').EventEmitter;
5+
EventEmitter = require('events').EventEmitter,
6+
crypto = require('crypto')
67

78
function File(properties) {
89
EventEmitter.call(this);
@@ -14,9 +15,15 @@ function File(properties) {
1415
this.lastModifiedDate = null;
1516

1617
this._writeStream = null;
17-
18-
for (var key in properties) {
19-
this[key] = properties[key];
18+
19+
if(typeof properties === 'object') {
20+
if(typeof properties.hash === 'string') {
21+
this.hash = crypto.createHash(properties.hash);
22+
}
23+
delete(properties.hash);
24+
for (var key in properties) {
25+
this[key] = properties[key];
26+
}
2027
}
2128

2229
this._backwardsCompatibility();
@@ -45,6 +52,9 @@ File.prototype.open = function() {
4552
File.prototype.write = function(buffer, cb) {
4653
var self = this;
4754
this._writeStream.write(buffer, function() {
55+
if(self.hash) {
56+
self.hash.update(buffer);
57+
}
4858
self.lastModifiedDate = new Date();
4959
self.size += buffer.length;
5060
self.emit('progress', self.size);
@@ -55,6 +65,9 @@ File.prototype.write = function(buffer, cb) {
5565
File.prototype.end = function(cb) {
5666
var self = this;
5767
this._writeStream.end(function() {
68+
if(self.hash) {
69+
self.hash = self.hash.digest('hex');
70+
}
5871
self.emit('end');
5972
cb();
6073
});

lib/incoming_form.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ function IncomingForm() {
2323
this.encoding = 'utf-8';
2424
this.headers = null;
2525
this.type = null;
26+
this.hash = false;
2627

2728
this.bytesReceived = null;
2829
this.bytesExpected = null;
@@ -188,6 +189,7 @@ IncomingForm.prototype.handlePart = function(part) {
188189
path: this._uploadPath(part.filename),
189190
name: part.filename,
190191
type: part.mime,
192+
hash: self.hash
191193
});
192194

193195
this.emit('fileBegin', part.name, file);

0 commit comments

Comments
 (0)