|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358 |
- 'use strict';
-
- var Emitter = require('events').EventEmitter;
- var GridFSBucketReadStream = require('./download');
- var GridFSBucketWriteStream = require('./upload');
- var shallowClone = require('../utils').shallowClone;
- var toError = require('../utils').toError;
- var util = require('util');
- var executeOperation = require('../utils').executeOperation;
-
- var DEFAULT_GRIDFS_BUCKET_OPTIONS = {
- bucketName: 'fs',
- chunkSizeBytes: 255 * 1024
- };
-
- module.exports = GridFSBucket;
-
-
-
- function GridFSBucket(db, options) {
- Emitter.apply(this);
- this.setMaxListeners(0);
-
- if (options && typeof options === 'object') {
- options = shallowClone(options);
- var keys = Object.keys(DEFAULT_GRIDFS_BUCKET_OPTIONS);
- for (var i = 0; i < keys.length; ++i) {
- if (!options[keys[i]]) {
- options[keys[i]] = DEFAULT_GRIDFS_BUCKET_OPTIONS[keys[i]];
- }
- }
- } else {
- options = DEFAULT_GRIDFS_BUCKET_OPTIONS;
- }
-
- this.s = {
- db: db,
- options: options,
- _chunksCollection: db.collection(options.bucketName + '.chunks'),
- _filesCollection: db.collection(options.bucketName + '.files'),
- checkedIndexes: false,
- calledOpenUploadStream: false,
- promiseLibrary: db.s.promiseLibrary || Promise
- };
- }
-
- util.inherits(GridFSBucket, Emitter);
-
-
-
-
-
- GridFSBucket.prototype.openUploadStream = function(filename, options) {
- if (options) {
- options = shallowClone(options);
- } else {
- options = {};
- }
- if (!options.chunkSizeBytes) {
- options.chunkSizeBytes = this.s.options.chunkSizeBytes;
- }
- return new GridFSBucketWriteStream(this, filename, options);
- };
-
-
-
- GridFSBucket.prototype.openUploadStreamWithId = function(id, filename, options) {
- if (options) {
- options = shallowClone(options);
- } else {
- options = {};
- }
-
- if (!options.chunkSizeBytes) {
- options.chunkSizeBytes = this.s.options.chunkSizeBytes;
- }
-
- options.id = id;
-
- return new GridFSBucketWriteStream(this, filename, options);
- };
-
-
-
- GridFSBucket.prototype.openDownloadStream = function(id, options) {
- var filter = { _id: id };
- options = {
- start: options && options.start,
- end: options && options.end
- };
-
- return new GridFSBucketReadStream(
- this.s._chunksCollection,
- this.s._filesCollection,
- this.s.options.readPreference,
- filter,
- options
- );
- };
-
-
-
- GridFSBucket.prototype.delete = function(id, callback) {
- return executeOperation(this.s.db.s.topology, _delete, [this, id, callback], {
- skipSessions: true
- });
- };
-
-
-
- function _delete(_this, id, callback) {
- _this.s._filesCollection.deleteOne({ _id: id }, function(error, res) {
- if (error) {
- return callback(error);
- }
-
- _this.s._chunksCollection.deleteMany({ files_id: id }, function(error) {
- if (error) {
- return callback(error);
- }
-
-
- if (!res.result.n) {
- var errmsg = 'FileNotFound: no file with id ' + id + ' found';
- return callback(new Error(errmsg));
- }
-
- callback();
- });
- });
- }
-
-
-
- GridFSBucket.prototype.find = function(filter, options) {
- filter = filter || {};
- options = options || {};
-
- var cursor = this.s._filesCollection.find(filter);
-
- if (options.batchSize != null) {
- cursor.batchSize(options.batchSize);
- }
- if (options.limit != null) {
- cursor.limit(options.limit);
- }
- if (options.maxTimeMS != null) {
- cursor.maxTimeMS(options.maxTimeMS);
- }
- if (options.noCursorTimeout != null) {
- cursor.addCursorFlag('noCursorTimeout', options.noCursorTimeout);
- }
- if (options.skip != null) {
- cursor.skip(options.skip);
- }
- if (options.sort != null) {
- cursor.sort(options.sort);
- }
-
- return cursor;
- };
-
-
-
- GridFSBucket.prototype.openDownloadStreamByName = function(filename, options) {
- var sort = { uploadDate: -1 };
- var skip = null;
- if (options && options.revision != null) {
- if (options.revision >= 0) {
- sort = { uploadDate: 1 };
- skip = options.revision;
- } else {
- skip = -options.revision - 1;
- }
- }
-
- var filter = { filename: filename };
- options = {
- sort: sort,
- skip: skip,
- start: options && options.start,
- end: options && options.end
- };
- return new GridFSBucketReadStream(
- this.s._chunksCollection,
- this.s._filesCollection,
- this.s.options.readPreference,
- filter,
- options
- );
- };
-
-
-
- GridFSBucket.prototype.rename = function(id, filename, callback) {
- return executeOperation(this.s.db.s.topology, _rename, [this, id, filename, callback], {
- skipSessions: true
- });
- };
-
-
-
- function _rename(_this, id, filename, callback) {
- var filter = { _id: id };
- var update = { $set: { filename: filename } };
- _this.s._filesCollection.updateOne(filter, update, function(error, res) {
- if (error) {
- return callback(error);
- }
- if (!res.result.n) {
- return callback(toError('File with id ' + id + ' not found'));
- }
- callback();
- });
- }
-
-
-
- GridFSBucket.prototype.drop = function(callback) {
- return executeOperation(this.s.db.s.topology, _drop, [this, callback], {
- skipSessions: true
- });
- };
-
-
- GridFSBucket.prototype.getLogger = function() {
- return this.s.db.s.logger;
- };
-
-
-
- function _drop(_this, callback) {
- _this.s._filesCollection.drop(function(error) {
- if (error) {
- return callback(error);
- }
- _this.s._chunksCollection.drop(function(error) {
- if (error) {
- return callback(error);
- }
-
- return callback();
- });
- });
- }
-
-
|