Skip to content

Commit 704a88e

Browse files
committed
feat(GridFS): add option to disable md5 in file upload
Fixes NODE-1306
1 parent eb68074 commit 704a88e

File tree

4 files changed

+22
-7
lines changed

4 files changed

+22
-7
lines changed

lib/gridfs-stream/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ util.inherits(GridFSBucket, Emitter);
7979
* @param {object} [options.metadata=null] Optional object to store in the file document's `metadata` field
8080
* @param {string} [options.contentType=null] Optional string to store in the file document's `contentType` field
8181
* @param {array} [options.aliases=null] Optional array of strings to store in the file document's `aliases` field
82+
* @param {boolean} [options.disableMD5=false] If true, disables adding an md5 field to file data
8283
* @return {GridFSBucketWriteStream}
8384
*/
8485

@@ -106,6 +107,7 @@ GridFSBucket.prototype.openUploadStream = function(filename, options) {
106107
* @param {object} [options.metadata=null] Optional object to store in the file document's `metadata` field
107108
* @param {string} [options.contentType=null] Optional string to store in the file document's `contentType` field
108109
* @param {array} [options.aliases=null] Optional array of strings to store in the file document's `aliases` field
110+
* @param {boolean} [options.disableMD5=false] If true, disables adding an md5 field to file data
109111
* @return {GridFSBucketWriteStream}
110112
*/
111113

lib/gridfs-stream/upload.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module.exports = GridFSBucketWriteStream;
2323
* @param {number} [options.w=null] The write concern
2424
* @param {number} [options.wtimeout=null] The write concern timeout
2525
* @param {number} [options.j=null] The journal write concern
26+
* @param {boolean} [options.disableMD5=false] If true, disables adding an md5 field to file data
2627
* @fires GridFSBucketWriteStream#error
2728
* @fires GridFSBucketWriteStream#finish
2829
* @return {GridFSBucketWriteStream} a GridFSBucketWriteStream instance.
@@ -42,7 +43,7 @@ function GridFSBucketWriteStream(bucket, filename, options) {
4243
this.chunkSizeBytes = this.options.chunkSizeBytes;
4344
this.bufToStore = new Buffer(this.chunkSizeBytes);
4445
this.length = 0;
45-
this.md5 = crypto.createHash('md5');
46+
this.md5 = !options.disableMD5 && crypto.createHash('md5');
4647
this.n = 0;
4748
this.pos = 0;
4849
this.state = {
@@ -264,7 +265,7 @@ function checkDone(_this, callback) {
264265
_this.id,
265266
_this.length,
266267
_this.chunkSizeBytes,
267-
_this.md5.digest('hex'),
268+
_this.md5 && _this.md5.digest('hex'),
268269
_this.filename,
269270
_this.options.contentType,
270271
_this.options.aliases,
@@ -357,10 +358,13 @@ function createFilesDoc(_id, length, chunkSize, md5, filename, contentType, alia
357358
length: length,
358359
chunkSize: chunkSize,
359360
uploadDate: new Date(),
360-
md5: md5,
361361
filename: filename
362362
};
363363

364+
if (md5) {
365+
ret.md5 = md5;
366+
}
367+
364368
if (contentType) {
365369
ret.contentType = contentType;
366370
}
@@ -414,7 +418,9 @@ function doWrite(_this, chunk, encoding, callback) {
414418
_this.pos += numToCopy;
415419
spaceRemaining -= numToCopy;
416420
if (spaceRemaining === 0) {
417-
_this.md5.update(_this.bufToStore);
421+
if (_this.md5) {
422+
_this.md5.update(_this.bufToStore);
423+
}
418424
var doc = createChunkDoc(_this.id, _this.n, _this.bufToStore);
419425
++_this.state.outstandingRequests;
420426
++outstandingRequests;
@@ -497,7 +503,9 @@ function writeRemnant(_this, callback) {
497503
// to be.
498504
var remnant = new Buffer(_this.pos);
499505
_this.bufToStore.copy(remnant, 0, 0, _this.pos);
500-
_this.md5.update(remnant);
506+
if (_this.md5) {
507+
_this.md5.update(remnant);
508+
}
501509
var doc = createChunkDoc(_this.id, _this.n, remnant);
502510

503511
// If the stream was aborted, do not write remnant

lib/gridfs/grid_store.js

+5
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,14 @@ const inherits = util.inherits;
4747
const Duplex = require('stream').Duplex;
4848
const shallowClone = require('../utils').shallowClone;
4949
const executeOperation = require('../utils').executeOperation;
50+
const deprecate = require('util').deprecate;
5051

5152
var REFERENCE_BY_FILENAME = 0,
5253
REFERENCE_BY_ID = 1;
5354

55+
const deprecationFn = deprecate(() => {},
56+
'GridStore is deprecated, and will be removed in a future version. Please use GridFSBucket instead');
57+
5458
/**
5559
* Namespace provided by the mongodb-core and node.js
5660
* @external Duplex
@@ -86,6 +90,7 @@ var REFERENCE_BY_FILENAME = 0,
8690
* @deprecated Use GridFSBucket API instead
8791
*/
8892
var GridStore = function GridStore(db, id, filename, mode, options) {
93+
deprecationFn();
8994
if (!(this instanceof GridStore)) return new GridStore(db, id, filename, mode, options);
9095
this.db = db;
9196

test/functional/gridfs_stream_tests.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1199,8 +1199,8 @@ describe('GridFS Stream', function() {
11991199
});
12001200

12011201
function testResultDoc(test, specDoc, resDoc, result) {
1202-
var specKeys = Object.keys(specDoc);
1203-
var resKeys = Object.keys(resDoc);
1202+
var specKeys = Object.keys(specDoc).sort();
1203+
var resKeys = Object.keys(resDoc).sort();
12041204

12051205
test.ok(specKeys.length === resKeys.length);
12061206

0 commit comments

Comments
 (0)