Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function processImage(req, reply) {
- const options = {}
- let imageName = null
- let imageURL = null
- req.multipart(handler, done, options)
- // upload to rackspace here
- function done() {
- const readStream = fs.createReadStream(`${imgsPath}/${imageName}`)
- const writeStream = rackspace.upload({
- container: 'image-bearbrand-container',
- remote: imageName
- })
- writeStream.on('error', function(err) {
- reply.code(400).send({ err })
- })
- writeStream.on('success', function() {
- rimraf.sync(`${imgsPath}/${imageName}`) // delete current image because it now uploaded to rackspace
- reply.code(200).send({ imageURI: `${imageCdnUrl}${imageName}` })
- })
- readStream.pipe(writeStream)
- }
- // saving image to disk first
- function handler(field, file, filename, encoding, mimetype) {
- if (mime.includes(mimetype)) {
- imageName = newFilename('jpg')
- const imageStream = fs.createWriteStream(`${imgsPath}/${imageName}`)
- pump(file, imageStream, err => {
- if (err) throw Error(err.message)
- })
- } else {
- reply.code(200).send({
- message:
- 'Ssst! your file is malicious! We keep your secret do not worry'
- })
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement