upload.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. var core = require('mongodb-core');
  2. var crypto = require('crypto');
  3. var stream = require('stream');
  4. var util = require('util');
  5. var ERROR_NAMESPACE_NOT_FOUND = 26;
  6. module.exports = GridFSBucketWriteStream;
  7. /**
  8. * A writable stream that enables you to write buffers to GridFS.
  9. *
  10. * Do not instantiate this class directly. Use `openUploadStream()` instead.
  11. *
  12. * @class
  13. * @param {GridFSBucket} bucket Handle for this stream's corresponding bucket
  14. * @param {string} filename The value of the 'filename' key in the files doc
  15. * @param {object} [options=null] Optional settings.
  16. * @param {string|number|object} [options.id=null] Custom file id for the GridFS file.
  17. * @param {number} [options.chunkSizeBytes=null] The chunk size to use, in bytes
  18. * @param {number} [options.w=null] The write concern
  19. * @param {number} [options.wtimeout=null] The write concern timeout
  20. * @param {number} [options.j=null] The journal write concern
  21. * @fires GridFSBucketWriteStream#error
  22. * @fires GridFSBucketWriteStream#finish
  23. * @return {GridFSBucketWriteStream} a GridFSBucketWriteStream instance.
  24. */
  25. function GridFSBucketWriteStream(bucket, filename, options) {
  26. options = options || {};
  27. this.bucket = bucket;
  28. this.chunks = bucket.s._chunksCollection;
  29. this.filename = filename;
  30. this.files = bucket.s._filesCollection;
  31. this.options = options;
  32. this.id = options.id ? options.id : core.BSON.ObjectId();
  33. this.chunkSizeBytes = this.options.chunkSizeBytes;
  34. this.bufToStore = new Buffer(this.chunkSizeBytes);
  35. this.length = 0;
  36. this.md5 = crypto.createHash('md5');
  37. this.n = 0;
  38. this.pos = 0;
  39. this.state = {
  40. streamEnd: false,
  41. outstandingRequests: 0,
  42. errored: false,
  43. aborted: false,
  44. promiseLibrary: this.bucket.s.promiseLibrary
  45. };
  46. if (!this.bucket.s.calledOpenUploadStream) {
  47. this.bucket.s.calledOpenUploadStream = true;
  48. var _this = this;
  49. checkIndexes(this, function() {
  50. _this.bucket.s.checkedIndexes = true;
  51. _this.bucket.emit('index');
  52. });
  53. }
  54. }
  55. util.inherits(GridFSBucketWriteStream, stream.Writable);
  56. /**
  57. * An error occurred
  58. *
  59. * @event GridFSBucketWriteStream#error
  60. * @type {Error}
  61. */
  62. /**
  63. * `end()` was called and the write stream successfully wrote the file
  64. * metadata and all the chunks to MongoDB.
  65. *
  66. * @event GridFSBucketWriteStream#finish
  67. * @type {object}
  68. */
  69. /**
  70. * Write a buffer to the stream.
  71. *
  72. * @method
  73. * @param {Buffer} chunk Buffer to write
  74. * @param {String} encoding Optional encoding for the buffer
  75. * @param {Function} callback Function to call when the chunk was added to the buffer, or if the entire chunk was persisted to MongoDB if this chunk caused a flush.
  76. * @return {Boolean} False if this write required flushing a chunk to MongoDB. True otherwise.
  77. */
  78. GridFSBucketWriteStream.prototype.write = function(chunk, encoding, callback) {
  79. var _this = this;
  80. return waitForIndexes(this, function() {
  81. return doWrite(_this, chunk, encoding, callback);
  82. });
  83. };
  84. /**
  85. * Places this write stream into an aborted state (all future writes fail)
  86. * and deletes all chunks that have already been written.
  87. *
  88. * @method
  89. * @param {GridFSBucket~errorCallback} callback called when chunks are successfully removed or error occurred
  90. * @return {Promise} if no callback specified
  91. */
  92. GridFSBucketWriteStream.prototype.abort = function(callback) {
  93. if (this.state.streamEnd) {
  94. var error = new Error('Cannot abort a stream that has already completed');
  95. if (typeof callback == 'function') {
  96. return callback(error);
  97. }
  98. return this.state.promiseLibrary.reject(error);
  99. }
  100. if (this.state.aborted) {
  101. error = new Error('Cannot call abort() on a stream twice');
  102. if (typeof callback == 'function') {
  103. return callback(error);
  104. }
  105. return this.state.promiseLibrary.reject(error);
  106. }
  107. this.state.aborted = true;
  108. this.chunks.deleteMany({ files_id: this.id }, function(error) {
  109. if(typeof callback == 'function') callback(error);
  110. });
  111. };
  112. /**
  113. * Tells the stream that no more data will be coming in. The stream will
  114. * persist the remaining data to MongoDB, write the files document, and
  115. * then emit a 'finish' event.
  116. *
  117. * @method
  118. * @param {Buffer} chunk Buffer to write
  119. * @param {String} encoding Optional encoding for the buffer
  120. * @param {Function} callback Function to call when all files and chunks have been persisted to MongoDB
  121. */
  122. GridFSBucketWriteStream.prototype.end = function(chunk, encoding, callback) {
  123. var _this = this;
  124. if(typeof chunk == 'function') {
  125. callback = chunk, chunk = null, encoding = null;
  126. } else if(typeof encoding == 'function') {
  127. callback = encoding, encoding = null;
  128. }
  129. if (checkAborted(this, callback)) {
  130. return;
  131. }
  132. this.state.streamEnd = true;
  133. if (callback) {
  134. this.once('finish', function(result) {
  135. callback(null, result);
  136. });
  137. }
  138. if (!chunk) {
  139. waitForIndexes(this, function() {
  140. writeRemnant(_this);
  141. });
  142. return;
  143. }
  144. this.write(chunk, encoding, function() {
  145. writeRemnant(_this);
  146. });
  147. };
  148. /**
  149. * @ignore
  150. */
  151. function __handleError(_this, error, callback) {
  152. if (_this.state.errored) {
  153. return;
  154. }
  155. _this.state.errored = true;
  156. if (callback) {
  157. return callback(error);
  158. }
  159. _this.emit('error', error);
  160. }
  161. /**
  162. * @ignore
  163. */
  164. function createChunkDoc(filesId, n, data) {
  165. return {
  166. _id: core.BSON.ObjectId(),
  167. files_id: filesId,
  168. n: n,
  169. data: data
  170. };
  171. }
  172. /**
  173. * @ignore
  174. */
  175. function checkChunksIndex(_this, callback) {
  176. _this.chunks.listIndexes().toArray(function(error, indexes) {
  177. if (error) {
  178. // Collection doesn't exist so create index
  179. if (error.code === ERROR_NAMESPACE_NOT_FOUND) {
  180. var index = { files_id: 1, n: 1 };
  181. _this.chunks.createIndex(index, { background: false, unique: true }, function(error) {
  182. if (error) {
  183. return callback(error);
  184. }
  185. callback();
  186. });
  187. return;
  188. }
  189. return callback(error);
  190. }
  191. var hasChunksIndex = false;
  192. indexes.forEach(function(index) {
  193. if (index.key) {
  194. var keys = Object.keys(index.key);
  195. if (keys.length === 2 && index.key.files_id === 1 &&
  196. index.key.n === 1) {
  197. hasChunksIndex = true;
  198. }
  199. }
  200. });
  201. if (hasChunksIndex) {
  202. callback();
  203. } else {
  204. index = { files_id: 1, n: 1 };
  205. var indexOptions = getWriteOptions(_this);
  206. indexOptions.background = false;
  207. indexOptions.unique = true;
  208. _this.chunks.createIndex(index, indexOptions, function(error) {
  209. if (error) {
  210. return callback(error);
  211. }
  212. callback();
  213. });
  214. }
  215. });
  216. }
  217. /**
  218. * @ignore
  219. */
  220. function checkDone(_this, callback) {
  221. if (_this.state.streamEnd &&
  222. _this.state.outstandingRequests === 0 &&
  223. !_this.state.errored) {
  224. var filesDoc = createFilesDoc(_this.id, _this.length, _this.chunkSizeBytes,
  225. _this.md5.digest('hex'), _this.filename, _this.options.contentType,
  226. _this.options.aliases, _this.options.metadata);
  227. if (checkAborted(_this, callback)) {
  228. return false;
  229. }
  230. _this.files.insert(filesDoc, getWriteOptions(_this), function(error) {
  231. if (error) {
  232. return __handleError(_this, error, callback);
  233. }
  234. _this.emit('finish', filesDoc);
  235. });
  236. return true;
  237. }
  238. return false;
  239. }
  240. /**
  241. * @ignore
  242. */
  243. function checkIndexes(_this, callback) {
  244. _this.files.findOne({}, { _id: 1 }, function(error, doc) {
  245. if (error) {
  246. return callback(error);
  247. }
  248. if (doc) {
  249. return callback();
  250. }
  251. _this.files.listIndexes().toArray(function(error, indexes) {
  252. if (error) {
  253. // Collection doesn't exist so create index
  254. if (error.code === ERROR_NAMESPACE_NOT_FOUND) {
  255. var index = { filename: 1, uploadDate: 1 };
  256. _this.files.createIndex(index, { background: false }, function(error) {
  257. if (error) {
  258. return callback(error);
  259. }
  260. checkChunksIndex(_this, callback);
  261. });
  262. return;
  263. }
  264. return callback(error);
  265. }
  266. var hasFileIndex = false;
  267. indexes.forEach(function(index) {
  268. var keys = Object.keys(index.key);
  269. if (keys.length === 2 && index.key.filename === 1 &&
  270. index.key.uploadDate === 1) {
  271. hasFileIndex = true;
  272. }
  273. });
  274. if (hasFileIndex) {
  275. checkChunksIndex(_this, callback);
  276. } else {
  277. index = { filename: 1, uploadDate: 1 };
  278. var indexOptions = getWriteOptions(_this);
  279. indexOptions.background = false;
  280. _this.files.createIndex(index, indexOptions, function(error) {
  281. if (error) {
  282. return callback(error);
  283. }
  284. checkChunksIndex(_this, callback);
  285. });
  286. }
  287. });
  288. });
  289. }
  290. /**
  291. * @ignore
  292. */
  293. function createFilesDoc(_id, length, chunkSize, md5, filename, contentType,
  294. aliases, metadata) {
  295. var ret = {
  296. _id: _id,
  297. length: length,
  298. chunkSize: chunkSize,
  299. uploadDate: new Date(),
  300. md5: md5,
  301. filename: filename
  302. };
  303. if (contentType) {
  304. ret.contentType = contentType;
  305. }
  306. if (aliases) {
  307. ret.aliases = aliases;
  308. }
  309. if (metadata) {
  310. ret.metadata = metadata;
  311. }
  312. return ret;
  313. }
  314. /**
  315. * @ignore
  316. */
  317. function doWrite(_this, chunk, encoding, callback) {
  318. if (checkAborted(_this, callback)) {
  319. return false;
  320. }
  321. var inputBuf = (Buffer.isBuffer(chunk)) ?
  322. chunk : new Buffer(chunk, encoding);
  323. _this.length += inputBuf.length;
  324. // Input is small enough to fit in our buffer
  325. if (_this.pos + inputBuf.length < _this.chunkSizeBytes) {
  326. inputBuf.copy(_this.bufToStore, _this.pos);
  327. _this.pos += inputBuf.length;
  328. callback && callback();
  329. // Note that we reverse the typical semantics of write's return value
  330. // to be compatible with node's `.pipe()` function.
  331. // True means client can keep writing.
  332. return true;
  333. }
  334. // Otherwise, buffer is too big for current chunk, so we need to flush
  335. // to MongoDB.
  336. var inputBufRemaining = inputBuf.length;
  337. var spaceRemaining = _this.chunkSizeBytes - _this.pos;
  338. var numToCopy = Math.min(spaceRemaining, inputBuf.length);
  339. var outstandingRequests = 0;
  340. while (inputBufRemaining > 0) {
  341. var inputBufPos = inputBuf.length - inputBufRemaining;
  342. inputBuf.copy(_this.bufToStore, _this.pos,
  343. inputBufPos, inputBufPos + numToCopy);
  344. _this.pos += numToCopy;
  345. spaceRemaining -= numToCopy;
  346. if (spaceRemaining === 0) {
  347. _this.md5.update(_this.bufToStore);
  348. var doc = createChunkDoc(_this.id, _this.n, _this.bufToStore);
  349. ++_this.state.outstandingRequests;
  350. ++outstandingRequests;
  351. if (checkAborted(_this, callback)) {
  352. return false;
  353. }
  354. _this.chunks.insert(doc, getWriteOptions(_this), function(error) {
  355. if (error) {
  356. return __handleError(_this, error);
  357. }
  358. --_this.state.outstandingRequests;
  359. --outstandingRequests;
  360. if (!outstandingRequests) {
  361. _this.emit('drain', doc);
  362. callback && callback();
  363. checkDone(_this);
  364. }
  365. });
  366. spaceRemaining = _this.chunkSizeBytes;
  367. _this.pos = 0;
  368. ++_this.n;
  369. }
  370. inputBufRemaining -= numToCopy;
  371. numToCopy = Math.min(spaceRemaining, inputBufRemaining);
  372. }
  373. // Note that we reverse the typical semantics of write's return value
  374. // to be compatible with node's `.pipe()` function.
  375. // False means the client should wait for the 'drain' event.
  376. return false;
  377. }
  378. /**
  379. * @ignore
  380. */
  381. function getWriteOptions(_this) {
  382. var obj = {};
  383. if (_this.options.writeConcern) {
  384. obj.w = _this.options.writeConcern.w;
  385. obj.wtimeout = _this.options.writeConcern.wtimeout;
  386. obj.j = _this.options.writeConcern.j;
  387. }
  388. return obj;
  389. }
  390. /**
  391. * @ignore
  392. */
  393. function waitForIndexes(_this, callback) {
  394. if (_this.bucket.s.checkedIndexes) {
  395. return callback(false);
  396. }
  397. _this.bucket.once('index', function() {
  398. callback(true);
  399. });
  400. return true;
  401. }
  402. /**
  403. * @ignore
  404. */
  405. function writeRemnant(_this, callback) {
  406. // Buffer is empty, so don't bother to insert
  407. if (_this.pos === 0) {
  408. return checkDone(_this, callback);
  409. }
  410. ++_this.state.outstandingRequests;
  411. // Create a new buffer to make sure the buffer isn't bigger than it needs
  412. // to be.
  413. var remnant = new Buffer(_this.pos);
  414. _this.bufToStore.copy(remnant, 0, 0, _this.pos);
  415. _this.md5.update(remnant);
  416. var doc = createChunkDoc(_this.id, _this.n, remnant);
  417. // If the stream was aborted, do not write remnant
  418. if (checkAborted(_this, callback)) {
  419. return false;
  420. }
  421. _this.chunks.insert(doc, getWriteOptions(_this), function(error) {
  422. if (error) {
  423. return __handleError(_this, error);
  424. }
  425. --_this.state.outstandingRequests;
  426. checkDone(_this);
  427. });
  428. }
  429. /**
  430. * @ignore
  431. */
  432. function checkAborted(_this, callback) {
  433. if (_this.state.aborted) {
  434. if(typeof callback == 'function') {
  435. callback(new Error('this stream has been aborted'));
  436. }
  437. return true;
  438. }
  439. return false;
  440. }