0

I have to create a preview image for each file which is uploaded into my Google Cloud Storage bucket. For this I am using the filepreview npm package. After I upload the file, I should see new image with the file preview, but this is missing and the error log says:

Error: spawnSync file ENOENT
   at exports._errnoException (util.js:1020:11)
   at spawnSync (child_process.js:451:20)
   at Object.execFileSync (child_process.js:488:13)
   at Object.generate (/user_code/node_modules/filepreview/filepreview.js:41:40)
   at exports.generatePreviewImage.event (/user_code/index.js:27:22)
   at /var/tmp/worker/worker.js:770:24
   at process._tickDomainCallback (internal/process/next_tick.js:135:7)

I suppose the path to the file is not correct. Could this be the problem? The name of the file uploaded is kitten.png. Here is my function:

exports.generatePreviewImage = event => {
  const object = event.data || event; // Node 6: event.data === Node 8+: event

  const file = storage.bucket(object.bucket).file(object.name);
  const filePath = `gs://${object.bucket}/${object.name}`;
  const newFilePath = `gs://${object.bucket}/${object.name}-thumb.png`;

  console.log(`filePath: ${filePath}`); // This logs 'file: gs://my-bucket/kitten.png'
  console.log(`newFilePath: ${newFilePath}`); // This logs 'gs://my-bucket/kitten.png-thumb.png'

  return filepreview.generate(filePath, newFilePath, function(error) {
    if (error) {
      return console.log('Error is now: ', error);
    }
    console.log(`File preview is: ${newFilePath}`);
  });
}
Joan Grau Noël
  • 3,084
  • 12
  • 21
decebal
  • 1,151
  • 3
  • 18
  • 38
  • 1
    Is this "filepreview" supposed to understand how to work Google Cloud Storage URLs that start with "gs"? I suspect that it does not. – Doug Stevenson Jan 27 '19 at 17:20
  • @DougStevenson - I think you are right. I suppose the correct way to do it is to download the file locally, do the work and then upload the new generated file back to the bucket. I am doing that now, but using another package, but I have a different error. Please see the new question: https://stackoverflow.com/questions/54417900/convert-pdf-to-png-on-google-cloud-storage – decebal Jan 29 '19 at 09:39
  • I get the same error from the title of this post even if I am downloading the file and using `filepreview`, so it id not related only to the `gs://` urls. – decebal Jan 29 '19 at 17:48
  • You should be able to download locally using the SDK and upload back. That is a common pattern. You can find it in official samples. https://github.com/firebase/functions-samples/tree/Node-8/generate-thumbnail – Doug Stevenson Jan 29 '19 at 17:53
  • @DougStevenson - I am not using Firebase, but Google Cloud Storage. Anyway, I can download and upload the file, the problem is with the conversion itself. – decebal Jan 29 '19 at 19:49
  • Doesn't matter, Firebase just wraps the exact same Cloud products. The pattern holds, no matter which SDKs you use. Maybe you should show the output of all your print logs to see if they make sense. – Doug Stevenson Jan 29 '19 at 20:51
  • @DougStevenson - I had a look at the Firebase functions you sent, but they apply only to images which are uploaded and this already works for me. So I can do it for image uploaded on a bucket, but I cannot do it for PDF files (or any other files - but I target at least PDF. If I manage to do it for image and PDFs, it's enough). Sorry, I wasn't too clear about this. Please see my other post and thanks for your advises: https://stackoverflow.com/questions/54417900/convert-pdf-to-png-on-google-cloud-storage/54427252#54427252 – decebal Jan 30 '19 at 09:20

0 Answers0