How to use the mongodb.GridFSBucket function in mongodb

To help you get started, we’ve selected a few mongodb examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github open-rpa / openflow / OpenFlow / src / Messages / Message.ts View on Github external
return new Promise(async (resolve, reject) => {
            try {
                var bucket = new GridFSBucket(Config.db.db);
                let downloadStream = bucket.openDownloadStream(safeObjectID(id));
                var bufs = [];
                downloadStream.on('data', (chunk) => {
                    bufs.push(chunk);
                });
                downloadStream.on('error', (error) => {
                    reject(error);
                });
                downloadStream.on('end', () => {

                    // var contentLength = bufs.reduce(function(sum, buf){
                    //     return sum + buf.length;
                    //   }, 0);
                    var buffer = Buffer.concat(bufs);
                    //writeFileSync('/home/allan/Documents/data.png', result.body);
                    //result.body = Buffer.from(result.body).toString('base64');
github Innovic-io / angular-nestjs-rendering / src / server / modules / pets / services / owner.service.ts View on Github external
return new Promise((resolve) => {

      const bucket = new GridFSBucket(this.dbService, {
        bucketName: 'avatars',
      });

      const stream = bucket.openDownloadStream(createObjectID(id));
      
      stream.on('data', (data) => {
        resolve(data.toString());
      });

    });
github open-rpa / openflow / OpenFlow / src / Messages / Message.ts View on Github external
private async UpdateFile(cli: WebSocketClient): Promise {
        this.Reply();
        var msg: UpdateFileMessage
        try {
            msg = UpdateFileMessage.assign(this.data);
            if (Util.IsNullEmpty(msg.jwt)) { msg.jwt = cli.jwt; }

            var bucket = new GridFSBucket(Config.db.db);
            var q = { $or: [{ _id: msg.id }, { _id: safeObjectID(msg.id) }] };
            var files = bucket.find(q);
            var count = await this.filescount(files);
            if (count == 0) { throw new Error("Not found"); }
            var file = await this.filesnext(files);
            msg.metadata._createdby = file.metadata._createdby;
            msg.metadata._createdbyid = file.metadata._createdbyid;
            msg.metadata._created = file.metadata._created;
            msg.metadata.name = file.metadata.name;
            (msg.metadata as any).filename = file.metadata.filename;
            (msg.metadata as any).path = file.metadata.path;

            var user: TokenUser = Crypt.verityToken(msg.jwt);
            msg.metadata._modifiedby = user.name;
            msg.metadata._modifiedbyid = user._id;
            msg.metadata._modified = new Date(new Date().toISOString());;
github jembi / hearth / lib / fhir / resources / binary.js View on Github external
mongo.getDB((err, db) => {
      if (err) {
        return done(err)
      }
      const bucket = new mongodb.GridFSBucket(db)

      if (!resource || !resource.content) {
        return done(null, fhirCommon.buildHTTPOutcome(400, 'error', 'invalid', 'No content in binary resource'))
      }

      const stream = bucket.openUploadStream()

      stream.on('error', (err) => {
        return done(err)
      })
      .on('finish', (doc) => {
        if (!doc) {
          return done(new Error('GridFS create failed'))
        }
        if (!resource._transforms) {
          resource._transforms = {}
github opporty-com / Plasma-Cash / plasma-core / src / child-chain / lib / db / mongo.js View on Github external
return new Promise((resolve, reject) => {
      const bucket = new GridFSBucket(db);
      let result = "";
      const downloadStream = bucket.openDownloadStreamByName(key);

      downloadStream
        .once('error', (error) => {
          reject(error);
        })
        .on('data', (chunk) => {
          result += chunk;
        })
        .once('end', () => {
          downloadStream.destroy();
          resolve(result);
        });
    })
  }
github hanpama / girin / packages / objectstorage-gridfs / src / index.ts View on Github external
protected async createGridFSBucket(bucket: string): Promise {
    const { gridFSBucketMap, configs, client } = this;
    const options = configs.gridFSOptions && configs.gridFSOptions[bucket];

    const gridFSBucket = new GridFSBucket(client.db(), { bucketName: bucket, ...options });
    await this.client.db().collection(`${bucket}.files`).createIndex({ filename: 1 }, { unique: true });
    gridFSBucketMap.set(bucket, gridFSBucket);
    return gridFSBucket;
  }
}
github open-rpa / openflow / OpenFlow / src / DatabaseConnection.ts View on Github external
return new Promise(async (resolve, reject) => {
            try {
                var _id = new ObjectID(id);
                var bucket = new GridFSBucket(this.db);
                bucket.delete(_id, (error) => {
                    if (error) return reject(error);
                    resolve();
                })
            } catch (err) {
                reject(err);
            }
        });
    }
github jade-press / jade-press / lib / file.js View on Github external
_ = require('lodash')
,local = require('../app/local')
,escRegStr = require('escape-string-regexp')
,setting = require('../app/setting')
,tools = require('./tools')
,log = tools.log
,err = tools.err
,db = require('./db').db
,validater = require('s-validater').validatePromise
,types = require('s-validater').types
,docs = require('../doc/db').file
,newId = require('./meta').createDigitId
,cid = require('shortid').generate
,multiparty = require('multiparty')
,GridFSBucket = require('mongodb').GridFSBucket
,bucket = new GridFSBucket(db, {
	bucketName: 'fs'
})

exports.handleUpload = function (req) {


	return new Promise(function(resolve, reject) {

		let form = new multiparty.Form()
		let fid = cid()
		let gstream = bucket.openUploadStream('')
		gstream.id = fid

		let res = {
			_id: fid
		}
github opporty-com / Plasma-Cash / plasma-core / src / child-chain / lib / db / mongo.js View on Github external
return new Promise((resolve, reject) => {
      const bucket = new GridFSBucket(db);
      bucket.delete(key, function (error) {
        if (error)
          reject(error);
        resolve();
      })
    });
  }
github parse-community / parse-server / src / Adapters / Files / GridFSBucketAdapter.js View on Github external
    return this._connect().then(database => new GridFSBucket(database));
  }