Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
* @author lally elias
* @license MIT
* @since 1.0.0
* @version 0.1.0
* @instance
* @example
*
* const bucket = createBucket();
* const _id = new ObjectId();
* const filename = 'filename.txt';
* const writeStream = fs.createWriteStream(filename);
* const readStream = bucket.createReadStream({_id, filename});
* readStream.pipe(writeStream);
*
*/
GridFSBucket.prototype.createReadStream = function createReadStream(optns) {
// ensure options
const options = _.merge({}, optns);
const { _id, filename } = options;
// ensure filename or _id
if (_.isEmpty(filename) && !_id) {
let error = Error('Missing filename or file id');
error.status = 400;
throw error;
}
// initalize file read stream
let readstream;
// open download stream by file id
if (_id) {
* @version 0.1.0
* @instance
* @example
*
* // large file
* const bucket = createBucket();
* const filename = 'filename.txt';
* const readStream = bucket.readFile({ filename });
*
* // small file
* const bucket = createBucket();
* const filename = 'filename.txt';
* bucket.readFile({ filename }, (error, buffer) => { ... });
*
*/
GridFSBucket.prototype.readFile = function readFile(optns, done) {
// ensure options
const _optns = _.merge({}, optns);
//create file read stream
const readstream = this.createReadStream(_optns);
//pipe the whole stream into buffer if callback provided
if (done && _.isFunction(done)) {
return read(readstream, done);
}
//return stream
else {
return readstream;
}
* @description find an existing file with given objectid
* @param {ObjectId} _id valid objectid of the existing file
* @param {Function} done a callback to invoke on success or error
* @return {Object} existing file details
* @author lally elias
* @license MIT
* @since 0.1.0
* @version 0.7.0
* @instance
* @example
*
* const bucket = createBucket();
* bucket.findById(_id, (error, file) => { ... });
*
*/
GridFSBucket.prototype.findById = function findById(_id, done) {
return this.findOne({ _id }, done);
};
/* multer */
/**
* @function _handleFile
* @name _handleFile
* @description write file to the bucket and return information on how to
* access the file in the future
* @param {Object} request injected request from multer
* @param {Object} file injected file object from multer
* @param {Function} done a callback to invoke on success or error
* @author lally elias
* @see {@link https://github.com/expressjs/multer/blob/master/StorageEngine.md}
* @param {Object} optns valid find criteria
* @param {Function} done a callback to invoke on success or error
* @return {Object} existing file details
* @author lally elias
* @license MIT
* @since 0.1.0
* @version 0.7.0
* @instance
* @example
*
* const bucket = createBucket();
* bucket.findOne({ _id }, (error, file) => { ... });
* bucket.findOne({ filename }, (error, file) => { ... });
*
*/
GridFSBucket.prototype.findOne = function findOne(optns, done) {
// ensure file find criteria
const options = _.merge({}, optns);
// find one existing file
try {
const cursor = this.find(options, { limit: 1 });
if (!cursor) {
const error = new Error('Collection not found');
error.status = 400;
return done(error);
}
return cursor.next(done);
}
// catch find errors
catch (error) {
* @author lally elias
* @license MIT
* @since 1.0.0
* @version 0.1.0
* @instance
* @example
*
* const bucket = createBucket();
* const _id = new ObjectId();
* const filename = 'filename.txt';
* const readStream = fs.createReadStream(filename);
* const writeStream = bucket.createWriteStream({_id, filename});
* readStream.pipe(writeStream);
*
*/
GridFSBucket.prototype.createWriteStream = function createWriteStream(optns) {
// ensure options
const defaults = { _id: new ObjectId() };
const options = _.merge({}, defaults, optns);
const { _id, filename } = options;
// ensure filename
if (_.isEmpty(filename)) {
let error = new Error('Missing filename');
error.status = 400;
throw error;
}
// open write stream
const writeStream =
this.openUploadStreamWithId(_id, filename, options);
* @example
*
* const express = require('express');
* const multer = require('multer');
* const { createBucket } = require('mongoose-gridfs');
* const app = express();
* const storage = createBucket(); // createBucket(optns)
* const upload = multer({ storage });
*
* app.post('/profile', upload.single('avatar'), (req, res, next) => {
* // req.file is the `avatar` file
* // req.body contains the text fields
* });
*
*/
GridFSBucket.prototype._removeFile = function _removeFile(request, file, done) {
// remove file
if (file._id) {
return this.deleteFile(file._id, done);
}
// no operation
else {
return done(null, null);
}
};
/* statics */
/**
* @function createBucket
* @name createBucket
* @description Remove an existing file and its chunks.
* @param {ObjectId} _id The id of the file doc
* @param {Function} done a callback to invoke on success or error
* @author lally elias
* @license MIT
* @since 1.0.0
* @version 0.1.0
* @instance
* @example
*
* const bucket = createBucket();
* bucket.deleteFile(_id, (error, results) => { ... });
*
*/
GridFSBucket.prototype.deleteFile =
GridFSBucket.prototype.unlink = function deleteFile(_id, done) {
this.delete(_id, function afterDelete(error) {
return done(error, _id);
});
};
/* finders */
/**
* @function findOne
* @name findOne
* @description find an existing file using options provided
* @param {Object} optns valid find criteria
* @param {Function} done a callback to invoke on success or error
* @return {Object} existing file details
* @author lally elias
* @example
*
* // large file
* const bucket = createBucket();
* const filename = 'filename.txt';
* const readStream = fs.createReadStream(filename);
* const writeStream = bucket.writeFile({ filename }, readStream);
*
* // small file
* const bucket = createBucket();
* const filename = 'filename.txt';
* const readStream = fs.createReadStream(filename);
* bucket.writeFile({ filename }, readStream, (error, file) => { ... });
*
*/
GridFSBucket.prototype.writeFile = function writeFile(file, readstream, done) {
// ensure file details
const _file = _.merge({}, { _id: new ObjectId() }, file);
// create file write stream
const writestream = this.createWriteStream(_file);
// stream file into mongodb gridfs bucket
readstream.pipe(writestream);
// work on the stream
if (done && _.isFunction(done)) {
// handle errors
writestream.on('error', function onWriteFileError(error) {
return done(error);
});
* @alias unlink
* @description Remove an existing file and its chunks.
* @param {ObjectId} _id The id of the file doc
* @param {Function} done a callback to invoke on success or error
* @author lally elias
* @license MIT
* @since 1.0.0
* @version 0.1.0
* @instance
* @example
*
* const bucket = createBucket();
* bucket.deleteFile(_id, (error, results) => { ... });
*
*/
GridFSBucket.prototype.deleteFile =
GridFSBucket.prototype.unlink = function deleteFile(_id, done) {
this.delete(_id, function afterDelete(error) {
return done(error, _id);
});
};
/* finders */
/**
* @function findOne
* @name findOne
* @description find an existing file using options provided
* @param {Object} optns valid find criteria
* @param {Function} done a callback to invoke on success or error
* @return {Object} existing file details
* @name bucketName
* @description Bucket name used to prefix 'files' and 'chunks' collections.
* @return {String}
* @author lally elias
* @license MIT
* @since 1.0.0
* @version 0.1.0
* @instance
* @example
*
* const bucket = createBucket();
* const bucketName = bucket.bucketName;
* //=> fs
*
*/
Object.defineProperty(GridFSBucket.prototype, 'bucketName', {
get: function getBucketName() {
return this.s.options.bucketName;
}
});
/**
* @name collection
* @description Collection used to store file data.
* @return {Collection}
* @author lally elias
* @license MIT
* @since 0.1.0
* @version 0.6.0
* @instance
* @example