Guest User

Untitled

a guest
Jun 23rd, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. const BlobService = require('feathers-blob');
  2. const fs = require('fs-blob-store');
  3. const multer = require('multer');
  4. const multipartMiddleware = multer();
  5. const { getBase64DataURI } = require('dauria');
  6.  
  7. const blobStorage = fs(__dirname + '/uploads');
  8.  
  9. module.exports = function (app) {
  10.  
  11. app.use('/uploads',
  12. multipartMiddleware.single('file'),
  13. function(req,res,next){
  14. req.feathers.file = req.file;
  15. next();
  16. },
  17. BlobService({
  18. Model: blobStorage
  19. })
  20. );
  21.  
  22. // Get our initialized service so that we can register hooks and filters
  23. const blobService = app.service('uploads');
  24.  
  25. //service.hooks(hooks);
  26.  
  27. blobService.hooks({
  28. before:{
  29. create: [
  30. function(context) {
  31. if (!context.data.uri && context.params.file){
  32. const file = context.params.file;
  33. const uri = getBase64DataURI(file.buffer, file.mimetype);
  34. context.data = {uri: uri};
  35. }
  36. }
  37. ]
  38. }
  39. });
  40.  
  41. const blob = {
  42. uri: getBase64DataURI(new Buffer('hello world'), 'text/plain')
  43. };
  44.  
  45. blobService.create(blob).then(function (result) {
  46. console.log('Stored blob with id', result.id);
  47. }).catch(err => {
  48. console.error(err);
  49. });
  50.  
  51. };
Add Comment
Please, Sign In to add comment