index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. 'use strict';
  2. var Emitter = require('events').EventEmitter;
  3. var GridFSBucketReadStream = require('./download');
  4. var GridFSBucketWriteStream = require('./upload');
  5. var shallowClone = require('../utils').shallowClone;
  6. var toError = require('../utils').toError;
  7. var util = require('util');
  8. var executeLegacyOperation = require('../utils').executeLegacyOperation;
  9. const deprecateOptions = require('../utils').deprecateOptions;
  10. var DEFAULT_GRIDFS_BUCKET_OPTIONS = {
  11. bucketName: 'fs',
  12. chunkSizeBytes: 255 * 1024
  13. };
  14. module.exports = GridFSBucket;
  15. /**
  16. * Constructor for a streaming GridFS interface
  17. * @class
  18. * @extends external:EventEmitter
  19. * @param {Db} db A db handle
  20. * @param {object} [options] Optional settings.
  21. * @param {string} [options.bucketName="fs"] The 'files' and 'chunks' collections will be prefixed with the bucket name followed by a dot.
  22. * @param {number} [options.chunkSizeBytes=255 * 1024] Number of bytes stored in each chunk. Defaults to 255KB
  23. * @param {object} [options.writeConcern] Optional write concern to be passed to write operations, for instance `{ w: 1 }`
  24. * @param {object} [options.readPreference] Optional read preference to be passed to read operations
  25. * @fires GridFSBucketWriteStream#index
  26. */
  27. function GridFSBucket(db, options) {
  28. Emitter.apply(this);
  29. this.setMaxListeners(0);
  30. if (options && typeof options === 'object') {
  31. options = shallowClone(options);
  32. var keys = Object.keys(DEFAULT_GRIDFS_BUCKET_OPTIONS);
  33. for (var i = 0; i < keys.length; ++i) {
  34. if (!options[keys[i]]) {
  35. options[keys[i]] = DEFAULT_GRIDFS_BUCKET_OPTIONS[keys[i]];
  36. }
  37. }
  38. } else {
  39. options = DEFAULT_GRIDFS_BUCKET_OPTIONS;
  40. }
  41. this.s = {
  42. db: db,
  43. options: options,
  44. _chunksCollection: db.collection(options.bucketName + '.chunks'),
  45. _filesCollection: db.collection(options.bucketName + '.files'),
  46. checkedIndexes: false,
  47. calledOpenUploadStream: false,
  48. promiseLibrary: db.s.promiseLibrary || Promise
  49. };
  50. }
  51. util.inherits(GridFSBucket, Emitter);
  52. /**
  53. * When the first call to openUploadStream is made, the upload stream will
  54. * check to see if it needs to create the proper indexes on the chunks and
  55. * files collections. This event is fired either when 1) it determines that
  56. * no index creation is necessary, 2) when it successfully creates the
  57. * necessary indexes.
  58. *
  59. * @event GridFSBucket#index
  60. * @type {Error}
  61. */
  62. /**
  63. * Returns a writable stream (GridFSBucketWriteStream) for writing
  64. * buffers to GridFS. The stream's 'id' property contains the resulting
  65. * file's id.
  66. * @method
  67. * @param {string} filename The value of the 'filename' key in the files doc
  68. * @param {object} [options] Optional settings.
  69. * @param {number} [options.chunkSizeBytes] Optional overwrite this bucket's chunkSizeBytes for this file
  70. * @param {object} [options.metadata] Optional object to store in the file document's `metadata` field
  71. * @param {string} [options.contentType] Optional string to store in the file document's `contentType` field
  72. * @param {array} [options.aliases] Optional array of strings to store in the file document's `aliases` field
  73. * @param {boolean} [options.disableMD5=false] **Deprecated** If true, disables adding an md5 field to file data
  74. * @return {GridFSBucketWriteStream}
  75. */
  76. GridFSBucket.prototype.openUploadStream = deprecateOptions(
  77. {
  78. name: 'GridFSBucket.openUploadStream',
  79. deprecatedOptions: ['disableMD5'],
  80. optionsIndex: 1
  81. },
  82. function(filename, options) {
  83. if (options) {
  84. options = shallowClone(options);
  85. } else {
  86. options = {};
  87. }
  88. if (!options.chunkSizeBytes) {
  89. options.chunkSizeBytes = this.s.options.chunkSizeBytes;
  90. }
  91. return new GridFSBucketWriteStream(this, filename, options);
  92. }
  93. );
  94. /**
  95. * Returns a writable stream (GridFSBucketWriteStream) for writing
  96. * buffers to GridFS for a custom file id. The stream's 'id' property contains the resulting
  97. * file's id.
  98. * @method
  99. * @param {string|number|object} id A custom id used to identify the file
  100. * @param {string} filename The value of the 'filename' key in the files doc
  101. * @param {object} [options] Optional settings.
  102. * @param {number} [options.chunkSizeBytes] Optional overwrite this bucket's chunkSizeBytes for this file
  103. * @param {object} [options.metadata] Optional object to store in the file document's `metadata` field
  104. * @param {string} [options.contentType] Optional string to store in the file document's `contentType` field
  105. * @param {array} [options.aliases] Optional array of strings to store in the file document's `aliases` field
  106. * @param {boolean} [options.disableMD5=false] **Deprecated** If true, disables adding an md5 field to file data
  107. * @return {GridFSBucketWriteStream}
  108. */
  109. GridFSBucket.prototype.openUploadStreamWithId = deprecateOptions(
  110. {
  111. name: 'GridFSBucket.openUploadStreamWithId',
  112. deprecatedOptions: ['disableMD5'],
  113. optionsIndex: 2
  114. },
  115. function(id, filename, options) {
  116. if (options) {
  117. options = shallowClone(options);
  118. } else {
  119. options = {};
  120. }
  121. if (!options.chunkSizeBytes) {
  122. options.chunkSizeBytes = this.s.options.chunkSizeBytes;
  123. }
  124. options.id = id;
  125. return new GridFSBucketWriteStream(this, filename, options);
  126. }
  127. );
  128. /**
  129. * Returns a readable stream (GridFSBucketReadStream) for streaming file
  130. * data from GridFS.
  131. * @method
  132. * @param {ObjectId} id The id of the file doc
  133. * @param {Object} [options] Optional settings.
  134. * @param {Number} [options.start] Optional 0-based offset in bytes to start streaming from
  135. * @param {Number} [options.end] Optional 0-based offset in bytes to stop streaming before
  136. * @return {GridFSBucketReadStream}
  137. */
  138. GridFSBucket.prototype.openDownloadStream = function(id, options) {
  139. var filter = { _id: id };
  140. options = {
  141. start: options && options.start,
  142. end: options && options.end
  143. };
  144. return new GridFSBucketReadStream(
  145. this.s._chunksCollection,
  146. this.s._filesCollection,
  147. this.s.options.readPreference,
  148. filter,
  149. options
  150. );
  151. };
  152. /**
  153. * Deletes a file with the given id
  154. * @method
  155. * @param {ObjectId} id The id of the file doc
  156. * @param {GridFSBucket~errorCallback} [callback]
  157. */
  158. GridFSBucket.prototype.delete = function(id, callback) {
  159. return executeLegacyOperation(this.s.db.s.topology, _delete, [this, id, callback], {
  160. skipSessions: true
  161. });
  162. };
  163. /**
  164. * @ignore
  165. */
  166. function _delete(_this, id, callback) {
  167. _this.s._filesCollection.deleteOne({ _id: id }, function(error, res) {
  168. if (error) {
  169. return callback(error);
  170. }
  171. _this.s._chunksCollection.deleteMany({ files_id: id }, function(error) {
  172. if (error) {
  173. return callback(error);
  174. }
  175. // Delete orphaned chunks before returning FileNotFound
  176. if (!res.result.n) {
  177. var errmsg = 'FileNotFound: no file with id ' + id + ' found';
  178. return callback(new Error(errmsg));
  179. }
  180. callback();
  181. });
  182. });
  183. }
  184. /**
  185. * Convenience wrapper around find on the files collection
  186. * @method
  187. * @param {Object} filter
  188. * @param {Object} [options] Optional settings for cursor
  189. * @param {number} [options.batchSize=1000] The number of documents to return per batch. See {@link https://docs.mongodb.com/manual/reference/command/find|find command documentation}.
  190. * @param {number} [options.limit] Optional limit for cursor
  191. * @param {number} [options.maxTimeMS] Optional maxTimeMS for cursor
  192. * @param {boolean} [options.noCursorTimeout] Optionally set cursor's `noCursorTimeout` flag
  193. * @param {number} [options.skip] Optional skip for cursor
  194. * @param {object} [options.sort] Optional sort for cursor
  195. * @return {Cursor}
  196. */
  197. GridFSBucket.prototype.find = function(filter, options) {
  198. filter = filter || {};
  199. options = options || {};
  200. var cursor = this.s._filesCollection.find(filter);
  201. if (options.batchSize != null) {
  202. cursor.batchSize(options.batchSize);
  203. }
  204. if (options.limit != null) {
  205. cursor.limit(options.limit);
  206. }
  207. if (options.maxTimeMS != null) {
  208. cursor.maxTimeMS(options.maxTimeMS);
  209. }
  210. if (options.noCursorTimeout != null) {
  211. cursor.addCursorFlag('noCursorTimeout', options.noCursorTimeout);
  212. }
  213. if (options.skip != null) {
  214. cursor.skip(options.skip);
  215. }
  216. if (options.sort != null) {
  217. cursor.sort(options.sort);
  218. }
  219. return cursor;
  220. };
  221. /**
  222. * Returns a readable stream (GridFSBucketReadStream) for streaming the
  223. * file with the given name from GridFS. If there are multiple files with
  224. * the same name, this will stream the most recent file with the given name
  225. * (as determined by the `uploadDate` field). You can set the `revision`
  226. * option to change this behavior.
  227. * @method
  228. * @param {String} filename The name of the file to stream
  229. * @param {Object} [options] Optional settings
  230. * @param {number} [options.revision=-1] The revision number relative to the oldest file with the given filename. 0 gets you the oldest file, 1 gets you the 2nd oldest, -1 gets you the newest.
  231. * @param {Number} [options.start] Optional 0-based offset in bytes to start streaming from
  232. * @param {Number} [options.end] Optional 0-based offset in bytes to stop streaming before
  233. * @return {GridFSBucketReadStream}
  234. */
  235. GridFSBucket.prototype.openDownloadStreamByName = function(filename, options) {
  236. var sort = { uploadDate: -1 };
  237. var skip = null;
  238. if (options && options.revision != null) {
  239. if (options.revision >= 0) {
  240. sort = { uploadDate: 1 };
  241. skip = options.revision;
  242. } else {
  243. skip = -options.revision - 1;
  244. }
  245. }
  246. var filter = { filename: filename };
  247. options = {
  248. sort: sort,
  249. skip: skip,
  250. start: options && options.start,
  251. end: options && options.end
  252. };
  253. return new GridFSBucketReadStream(
  254. this.s._chunksCollection,
  255. this.s._filesCollection,
  256. this.s.options.readPreference,
  257. filter,
  258. options
  259. );
  260. };
  261. /**
  262. * Renames the file with the given _id to the given string
  263. * @method
  264. * @param {ObjectId} id the id of the file to rename
  265. * @param {String} filename new name for the file
  266. * @param {GridFSBucket~errorCallback} [callback]
  267. */
  268. GridFSBucket.prototype.rename = function(id, filename, callback) {
  269. return executeLegacyOperation(this.s.db.s.topology, _rename, [this, id, filename, callback], {
  270. skipSessions: true
  271. });
  272. };
  273. /**
  274. * @ignore
  275. */
  276. function _rename(_this, id, filename, callback) {
  277. var filter = { _id: id };
  278. var update = { $set: { filename: filename } };
  279. _this.s._filesCollection.updateOne(filter, update, function(error, res) {
  280. if (error) {
  281. return callback(error);
  282. }
  283. if (!res.result.n) {
  284. return callback(toError('File with id ' + id + ' not found'));
  285. }
  286. callback();
  287. });
  288. }
  289. /**
  290. * Removes this bucket's files collection, followed by its chunks collection.
  291. * @method
  292. * @param {GridFSBucket~errorCallback} [callback]
  293. */
  294. GridFSBucket.prototype.drop = function(callback) {
  295. return executeLegacyOperation(this.s.db.s.topology, _drop, [this, callback], {
  296. skipSessions: true
  297. });
  298. };
  299. /**
  300. * Return the db logger
  301. * @method
  302. * @return {Logger} return the db logger
  303. * @ignore
  304. */
  305. GridFSBucket.prototype.getLogger = function() {
  306. return this.s.db.s.logger;
  307. };
  308. /**
  309. * @ignore
  310. */
  311. function _drop(_this, callback) {
  312. _this.s._filesCollection.drop(function(error) {
  313. if (error) {
  314. return callback(error);
  315. }
  316. _this.s._chunksCollection.drop(function(error) {
  317. if (error) {
  318. return callback(error);
  319. }
  320. return callback();
  321. });
  322. });
  323. }
  324. /**
  325. * Callback format for all GridFSBucket methods that can accept a callback.
  326. * @callback GridFSBucket~errorCallback
  327. * @param {MongoError|undefined} error If present, an error instance representing any errors that occurred
  328. * @param {*} result If present, a returned result for the method
  329. */