question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generate thumbnail is generating application/octet-stream as type

See original GitHub issue

I’m following the exact example to generate a thumbnail from a image, but the image is getting the application/octet-stream type on the GCS instead of image/jpeg. I don’t know what i need to change in this code so the image will have the properly extension.

static generateThumbnail(filePath) {
    return new Promise((resolve, reject) => {
      // File and directory paths.
      const fileDir = path.dirname(filePath);
      const fileName = path.basename(filePath);
      const thumbFilePath = path.normalize(path.join(fileDir, `${THUMB_PREFIX}${fileName}`));
      const tempLocalFile = path.join(os.tmpdir(), filePath);
      const tempLocalDir = path.dirname(tempLocalFile);
      const tempLocalThumbFile = path.join(os.tmpdir(), thumbFilePath);

      // Exit if the image is already a thumbnail.
      if (fileName.startsWith(THUMB_PREFIX)) {
        console.log('Already a Thumbnail.');
        return;
      }

      // Cloud Storage files.
      const bucket = gcs.bucket(BUCKET);
      const file = bucket.file(filePath);
      const thumbFile = bucket.file(thumbFilePath);

      // Create the temp directory where the storage file will be downloaded.
      return mkdirp(tempLocalDir).then(() => {
        // Download file from bucket.
        return file.download({ destination: tempLocalFile });
      }).then(() => {
        console.log('The file has been downloaded to', tempLocalFile);
        // Generate a thumbnail using ImageMagick.
        return spawn('convert', [tempLocalFile, '-thumbnail', `${THUMB_MAX_WIDTH}x${THUMB_MAX_HEIGHT}>`, tempLocalThumbFile]);
      }).then(() => {
        console.log('Thumbnail created at', tempLocalThumbFile);
        // Uploading the Thumbnail.
        return bucket.upload(tempLocalThumbFile, { destination: thumbFilePath });
      }).then(() => {
        console.log('Thumbnail uploaded to Storage at', thumbFilePath);
        // Once the image has been uploaded delete the local files to free up disk space.
        fs.unlinkSync(tempLocalFile);
        fs.unlinkSync(tempLocalThumbFile);
        // Get the Signed URLs for the thumbnail and original image.
        const config = {
          action: 'read',
          expires: '03-01-2500'
        };
        return Promise.all([
          thumbFile.getSignedUrl(config),
          file.getSignedUrl(config)
        ]);
      }).then((results) => {
        console.log('Got Signed URLs.');
        const thumbResult = results[0];
        const originalResult = results[1];
        const thumbFileUrl = thumbResult[0];
        const fileUrl = originalResult[0];
        // Add the URLs to the Database
        resolve({ path: fileUrl, thumbnail: thumbFileUrl });
      });
    });
  }

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
nicolasgarniercommented, Nov 21, 2017

When uploading the file to Cloud Storage here: return bucket.upload(tempLocalThumbFile, { destination: thumbFilePath }); You need to pass some metadata that indicates the mime type of the file. For instance the same as the original image:

return bucket.upload(tempLocalThumbFile, {
    destination: thumbFilePath,
    metadata: {
        contentType: event.data.contentType // That's the event object from the Cloud Functions trigger
    }
});
1reaction
zhouzicommented, Nov 21, 2017

I was looking into an “application/octet-stream” issue and eventually stumbled upon your report. You should probably enable syntax highlighting and simplify your code, there are lots of unrelated things in there. So I’m not sure if it could help but I found this page to help me understand why I get “application/octet-stream” when uploading an image: add file metadata.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Generate Thumbnail In Stream - Computer Vision)
This operation generates a thumbnail image with the user-specified width and height. ... Media Types: "application/octet-stream", "multipart/form-data" ...
Read more >
how handling image's mime type "application/octet-stream"?
I have a function to make thumbnail of a ...
Read more >
Solved: AEM 6.4 DAM Update Asset Workflow: Thumbnails Not
4 DAM Asset Update Workflow should generate thumbnails for 'application/octet-stream' mime type images. Please refer to "jcr:mimeType" property, if you change ...
Read more >
Thumbnail generation - Supported file types
This section details the file types that Helix Search can generate thumbnails for. Tip. Thumbnails are only generated by Helix Search if it...
Read more >
Get thumbnails of files from shared folders - Dropbox Community
You should be able to use /2/files/get_thumbnail_v2 to get a thumbnail of a file ... $headers[] = 'Content-Type: application/octet-stream; charset=utf-8'; ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found