Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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');
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());
});
});
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());;
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 = {}
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);
});
})
}
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;
}
}
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);
}
});
}
_ = 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
}
return new Promise((resolve, reject) => {
const bucket = new GridFSBucket(db);
bucket.delete(key, function (error) {
if (error)
reject(error);
resolve();
})
});
}
return this._connect().then(database => new GridFSBucket(database));
}