Upload image to DO spaces after resize with sharp


#1

Hello
I am trying to make on upload to digital ocean spaces and I am keep having problems.
I tried the example from the docs and everything seems to work as expected. The problem is when I am adding sharp library

this is how my code looks right now

request.multipart.file('photo', {}, async file => {
      await sharp(file.stream)
        .resize(200)
        .toBuffer({ resolveWithObject: true })
        .then(async data => {
          await Drive.disk('do').put(file.clientName, data, {
            ACL: 'public-read',
            ContentType: 'image/jpeg'
          })
        })
        .catch(err => {
          return response.json({ status: err })
        })
    })

    await request.multipart.process()

any thoughts?
I don’t get any error, but my file is not uploaded to digital ocean spaces


#2

This works for me:

  try {
      await sharp(b64Buffer)
        .resize(width, height, {
          kernel: sharp.kernel.nearest,
          fit: 'contain',
          position: 'centre',
          background: { r: 255, g: 255, b: 255 }
        })
        .jpeg({
          quality: 90
        })
        .toBuffer({ resolveWithObject: true })
        .then(({ data, info }) => {
          Drive.disk('spaces').put(
            `path/to/image.jpg`,
            data,
            {
              ACL: 'public-read',
              ContentType: 'image/jpeg',
              ContentDisposition: `inline; filename=image.jpg`
            }
          )
        })
    } catch (error) {
      console.log(error)
    }

#3

Hello @peter thank you for your reply, but unfortunately it’s still not working for me.
I’ll keep trying maybe I will find something


#4

I’ve managed to make the upload to digital ocean spaces, but now I have another problem. After I apply sharp resize function my image is not an image anymore.
When I download the image from spaces and try to open it, I have the following error

Error interpreting JPEG image file (Not a JPEG file: starts with 0x0d 0xea)

based on the file size I assume the sharp.resize() function is working, but the output is not the correct one.
any ideas?

this is my current code

async store({ request, response }) {
    let filename = ''
    let transform = sharp()
    request.multipart.file('photo', {}, async file => {
      transform.resize({ width: 200 }).jpeg({
        quality: 100,
        chromaSubsampling: '4:4:4'
      })
      file.stream.pipe(transform).pipe(file.stream)

      await Drive.disk('do').put(file.clientName, file.stream, {
        ACL: 'public-read',
        ContentType: 'image/jpeg'
      })
    })

    await request.multipart.process()
  }

#5

OK, managed to solve this.
I’ll put my code here maybe it will be helpful for somebody.

async store({ request, response }) {
    let transform = sharp()
    request.multipart.file('photo', {}, async file => {
      const data = await transform
        .resize({ width: 200 })
        .jpeg({
          quality: 100,
          chromaSubsampling: '4:4:4'
        })
        .toFormat('jpeg')
      file.stream.pipe(transform).pipe(file.stream)

      await Drive.disk('do').put(file.clientName, data, {
        ACL: 'public-read',
        ContentType: 'image/jpeg'
      })
    })

    await request.multipart.process()
  }