SHARE
TWEET

Untitled

a guest Jul 18th, 2019 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function processImage(req, reply) {
  2.   const options = {}
  3.   let imageName = null
  4.   let imageURL = null
  5.   req.multipart(handler, done, options)
  6.  
  7.   // upload to rackspace here
  8.   function done() {
  9.     const readStream = fs.createReadStream(`${imgsPath}/${imageName}`)
  10.     const writeStream = rackspace.upload({
  11.       container: 'image-bearbrand-container',
  12.       remote: imageName
  13.     })
  14.     writeStream.on('error', function(err) {
  15.       reply.code(400).send({ err })
  16.     })
  17.     writeStream.on('success', function() {
  18.       rimraf.sync(`${imgsPath}/${imageName}`) // delete current image because it now uploaded to rackspace
  19.       reply.code(200).send({ imageURI: `${imageCdnUrl}${imageName}` })
  20.     })
  21.     readStream.pipe(writeStream)
  22.   }
  23.   // saving image to disk first
  24.   function handler(field, file, filename, encoding, mimetype) {
  25.     if (mime.includes(mimetype)) {
  26.       imageName = newFilename('jpg')
  27.       const imageStream = fs.createWriteStream(`${imgsPath}/${imageName}`)
  28.       pump(file, imageStream, err => {
  29.         if (err) throw Error(err.message)
  30.       })
  31.     } else {
  32.       reply.code(200).send({
  33.         message:
  34.           'Ssst! your file is malicious! We keep your secret do not worry'
  35.       })
  36.     }
  37.   }
  38. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top