0

I have a Firebase Cloud Function that converts a buffer (from PDF) to a PNG using GraphcisMagick. When I attempt to write this PNG buffer to Firebase Storage, I get a file stub but no content (empty file). My conversion to PNG is failing...

async function createThumbnail(newthumbname, mimetype, filebuffer) {
    const file = bucket.file(newthumbname)
    const thumbstream = file.createWriteStream({metadata:{contentType:mimetype}})
    const gm = require('gm').subClass({imageMagick: true})
    gm(filebuffer)
    .toBuffer('png', (err, thumbbuffer)=>{
        console.log(filebuffer)
        console.log(thumbbuffer)
        thumbstream.end(thumbbuffer)
    })
}

Firebase Storage Console

The filebuffer passed into the createThumbnail() has content,

<Buffer 25 50 44 46 2d 31 2e 33 0a 25 c4 e5 f2 e5 eb a7 f3 a0 d0 c4 c6 0a 33 20 30 20 6f 62 6a 0a 3c 3c 20 2f 46 69 6c 74 65 72 20 2f 46 6c 61 74 65 44 65 63 ... 6464 more bytes>

But the gm(filebuffer).toBuffer() is producing and empty thumbbuffer with Error: Stream yields empty buffer

What am I doing wrong here?

Colin
  • 930
  • 3
  • 19
  • 42

1 Answers1

0

This appears to be a png issue, as jpg seems to work with both, .stream() and .toBuffer().

I'll settle for jpg until I can figure out what's wrong with png.

async function createThumbnail(newthumbname, mimetype, filebuffer) {
    const file = bucket.file(newthumbname)
    const thumbstream = file.createWriteStream({metadata:{contentType:mimetype}})
    const gm = require('gm').subClass({imageMagick: true})
    gm(filebuffer)
    // .setFormat("jpg")
    // .stream()
    // .pipe(thumbstream)
    .toBuffer('jpg', (err, thumbbuffer)=>{
        thumbstream.end(thumbbuffer)
    })
}
Colin
  • 930
  • 3
  • 19
  • 42