Skip to content

Instantly share code, notes, and snippets.

@David-Melo
Created June 22, 2018 14:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save David-Melo/4b36e969b9cfcda8f065156e056ddaab to your computer and use it in GitHub Desktop.
Save David-Melo/4b36e969b9cfcda8f065156e056ddaab to your computer and use it in GitHub Desktop.
Feathers.js/Express.js Multi-Part Upload Handler
const BlobService = require('feathers-blob');
const fs = require('fs-blob-store');
const multer = require('multer');
const multipartMiddleware = multer();
const { getBase64DataURI } = require('dauria');
const blobStorage = fs(__dirname + '/uploads');
module.exports = function (app) {
app.use('/uploads',
multipartMiddleware.single('file'),
function(req,res,next){
req.feathers.file = req.file;
next();
},
BlobService({
Model: blobStorage
})
);
// Get our initialized service so that we can register hooks and filters
const blobService = app.service('uploads');
//service.hooks(hooks);
blobService.hooks({
before:{
create: [
function(context) {
if (!context.data.uri && context.params.file){
const file = context.params.file;
const uri = getBase64DataURI(file.buffer, file.mimetype);
context.data = {uri: uri};
}
}
]
}
});
const blob = {
uri: getBase64DataURI(new Buffer('hello world'), 'text/plain')
};
blobService.create(blob).then(function (result) {
console.log('Stored blob with id', result.id);
}).catch(err => {
console.error(err);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment