Skip to content

Commit

Permalink
feat(GridFS): add option to disable md5 in file upload
Browse files Browse the repository at this point in the history
Fixes NODE-1306
  • Loading branch information
daprahamian committed May 23, 2018
1 parent eb68074 commit 704a88e
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
2 changes: 2 additions & 0 deletions lib/gridfs-stream/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ util.inherits(GridFSBucket, Emitter);
* @param {object} [options.metadata=null] Optional object to store in the file document's `metadata` field
* @param {string} [options.contentType=null] Optional string to store in the file document's `contentType` field
* @param {array} [options.aliases=null] Optional array of strings to store in the file document's `aliases` field
* @param {boolean} [options.disableMD5=false] If true, disables adding an md5 field to file data
* @return {GridFSBucketWriteStream}
*/

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

Expand Down
18 changes: 13 additions & 5 deletions lib/gridfs-stream/upload.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ module.exports = GridFSBucketWriteStream;
* @param {number} [options.w=null] The write concern
* @param {number} [options.wtimeout=null] The write concern timeout
* @param {number} [options.j=null] The journal write concern
* @param {boolean} [options.disableMD5=false] If true, disables adding an md5 field to file data
* @fires GridFSBucketWriteStream#error
* @fires GridFSBucketWriteStream#finish
* @return {GridFSBucketWriteStream} a GridFSBucketWriteStream instance.
Expand All @@ -42,7 +43,7 @@ function GridFSBucketWriteStream(bucket, filename, options) {
this.chunkSizeBytes = this.options.chunkSizeBytes;
this.bufToStore = new Buffer(this.chunkSizeBytes);
this.length = 0;
this.md5 = crypto.createHash('md5');
this.md5 = !options.disableMD5 && crypto.createHash('md5');
this.n = 0;
this.pos = 0;
this.state = {
Expand Down Expand Up @@ -264,7 +265,7 @@ function checkDone(_this, callback) {
_this.id,
_this.length,
_this.chunkSizeBytes,
_this.md5.digest('hex'),
_this.md5 && _this.md5.digest('hex'),
_this.filename,
_this.options.contentType,
_this.options.aliases,
Expand Down Expand Up @@ -357,10 +358,13 @@ function createFilesDoc(_id, length, chunkSize, md5, filename, contentType, alia
length: length,
chunkSize: chunkSize,
uploadDate: new Date(),
md5: md5,
filename: filename
};

if (md5) {
ret.md5 = md5;
}

if (contentType) {
ret.contentType = contentType;
}
Expand Down Expand Up @@ -414,7 +418,9 @@ function doWrite(_this, chunk, encoding, callback) {
_this.pos += numToCopy;
spaceRemaining -= numToCopy;
if (spaceRemaining === 0) {
_this.md5.update(_this.bufToStore);
if (_this.md5) {
_this.md5.update(_this.bufToStore);
}
var doc = createChunkDoc(_this.id, _this.n, _this.bufToStore);
++_this.state.outstandingRequests;
++outstandingRequests;
Expand Down Expand Up @@ -497,7 +503,9 @@ function writeRemnant(_this, callback) {
// to be.
var remnant = new Buffer(_this.pos);
_this.bufToStore.copy(remnant, 0, 0, _this.pos);
_this.md5.update(remnant);
if (_this.md5) {
_this.md5.update(remnant);
}
var doc = createChunkDoc(_this.id, _this.n, remnant);

// If the stream was aborted, do not write remnant
Expand Down
5 changes: 5 additions & 0 deletions lib/gridfs/grid_store.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,14 @@ const inherits = util.inherits;
const Duplex = require('stream').Duplex;
const shallowClone = require('../utils').shallowClone;
const executeOperation = require('../utils').executeOperation;
const deprecate = require('util').deprecate;

var REFERENCE_BY_FILENAME = 0,
REFERENCE_BY_ID = 1;

const deprecationFn = deprecate(() => {},
'GridStore is deprecated, and will be removed in a future version. Please use GridFSBucket instead');

/**
* Namespace provided by the mongodb-core and node.js
* @external Duplex
Expand Down Expand Up @@ -86,6 +90,7 @@ var REFERENCE_BY_FILENAME = 0,
* @deprecated Use GridFSBucket API instead
*/
var GridStore = function GridStore(db, id, filename, mode, options) {
deprecationFn();
if (!(this instanceof GridStore)) return new GridStore(db, id, filename, mode, options);
this.db = db;

Expand Down
4 changes: 2 additions & 2 deletions test/functional/gridfs_stream_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -1199,8 +1199,8 @@ describe('GridFS Stream', function() {
});

function testResultDoc(test, specDoc, resDoc, result) {
var specKeys = Object.keys(specDoc);
var resKeys = Object.keys(resDoc);
var specKeys = Object.keys(specDoc).sort();
var resKeys = Object.keys(resDoc).sort();

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

Expand Down

0 comments on commit 704a88e

Please sign in to comment.