Created
June 22, 2018 14:42
-
-
Save David-Melo/4b36e969b9cfcda8f065156e056ddaab to your computer and use it in GitHub Desktop.
Feathers.js/Express.js Multi-Part Upload Handler
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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