Advertisement
Anatolyukropov

Upload File to Mongo GridFsBucket

Jul 27th, 2020 (edited)
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. @ Upload File to MongoDB
  2. import { Context } from 'moleculer';
  3.  
  4. export const upload = {
  5.   handler(ctx: Context) {
  6.     return new Promise((resolve, reject) => {
  7.       const uploadStream = this.gridFSBucket.openUploadStream(ctx.meta.filename, {
  8.         contentType: ctx.meta.type,
  9.       });
  10.  
  11.       uploadStream
  12.         .once('finish', () => {
  13.           resolve(uploadStream.id);
  14.         })
  15.         .once('error', err => {
  16.           ctx.broker.logger.error(err);
  17.           reject(err);
  18.         });
  19.  
  20.       ctx.params.pipe(uploadStream);
  21.     });
  22.   },
  23. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement