0
votes

I've got a cloud function for Firebase that triggers on a storage bucket write:

exports.convertToWebP = functions.storage.object().onChange(async event => {
    const path = event.data.name;
    const [pictureSlug, fileName] = path.split('/');
    const [fileSlug, fileType] = fileName.split('.');

    // Download the file
    const tmpFilePath = `/tmp/${fileSlug}.${fileType}`;

    const file = bucket.file(path);
    await file.download({ destination: tmpFilePath });

    await imagemin(
          [`tmp/${fileSlug}.${fileType}`], 
          '/tmp',
          { use: [ imageminWebp({quality: 50})] });

    const destination = `${pictureSlug}.webp`;
    await bucket.upload(tmpFilePath, { destination });

The issue I'm having is that when I call the .download method on the storage object, it goes to the /tmp/ folder, but I can't for the life of me figure out how to get imagemin to grab the file.

It could have something to do with the way I'm constructing the path, or it could be something else like Cloud Functions not supporting imagemin.

The reason I'm doing this to being with is because the local instance of ImageMagick doesn't support webp.

Also: This file is a .ts file and I'm using the typescript compiler to create a js file of it. It compiles to an ES5 version.

1
While I'm waiting to figure this out, I've decided to switch to imagemin.buffer() and just use the memory buffer to download, convert, and reupload the image.markgoho

1 Answers

0
votes

In this bit of code:

await imagemin(
      [`tmp/${fileSlug}.${fileType}`], 
      '/tmp',
      { use: [ imageminWebp({quality: 50})] });

It looks like you're building a different string to the tmp file than you did when you built tmpFilePath. It's missing the leading slash. Any reason why you wouldn't use this instead?

await imagemin(
      [tmpFilePath], 
      '/tmp',
      { use: [ imageminWebp({quality: 50})] });