honza / node-thumbnail

Thumbnail worker queue for node.js
http://honza.ca/node-thumbnail/
Other
103 stars 23 forks source link

Create thumbnail for files without file extension #24

Closed derMani closed 7 years ago

derMani commented 7 years ago

Scenario:

Image files are uploaded by users (for example with MULTER).

Situation:

After the upload, file names are obfuscated for security reasons. A file

myBeautifulImage.jpg

might be renamed to

6724dc10ee5cc720df7535d814f2945c

Problem

Node-thumbnail expects a file extension. With the file name above, thumbnails cannot be created

The file on the file system is clearly an image (see terminal command file)

$ file 6724dc10ee5cc720df7535d814f2945c

6724dc10ee5cc720df7535d814f2945c: PNG image data, 468 x 425, 8-bit/color RGBA, non-interlaced

Expected behavior

Thumbnails should also be created for files, which have no file extensions. The image type should be identified by the header of the file, not by the name of the extension.

How to test

Put a PNG file with the file name 6724dc10ee5cc720df7535d814f2945c into the directory, which is defined in the variable uploadPath.

var fs = require('fs');
var thumb = require('node-thumbnail').thumb;

var uploadPath = '/path/to/my/images/'

var thumbOpts = {
  source: uploadPath + '6724dc10ee5cc720df7535d814f2945c',
  destination: uploadPath,
  prefix: 'thumb_',
  suffix: '',
  concurrency: 4,
  width: 100
};
console.log(thumbOpts)
thumb(thumbOpts, function(files, err, stdout, stderr) {
  if (err) return console.log(err);
  console.log(files);
});

Regards

derMani

honza commented 7 years ago

Fixed in 280c04376995274689008945b9fb1b56c9e34682, I'll be publishing 0.12.0 to NPM shortly.

You can now use the ignore setting to disable the file extension check. Simply using 'ignore: true` in your example should do the trick.