Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Node.js, given a URL, how do I check whether its a jpg/png/gif?

My current method is this:

var request = require('request');
var mime = require('mime');
var fs = require('fs');
var uri = 'http://www.sweetslyrics.com/images/img_gal/25646_christina-perri-213968.jpg';
request({
        'method':'GET',
        'uri': uri
},function(err, response,body){
    var tmp_path = '/tmp/123456';
    fs.writeFile(tmp_path, body, function(err) {
        console.log(mime.lookup(tmp_path));  //application/octet-stream ?????
    });
});

The image is obviously a picture, but node-mime says it's application/octet-stream. Why?

Note: - I do not want to rely on the Response Headers content-type, because based on my experience, sometimes those response headers are set incorrectly...and they do not determine the true file type. (that's why I save it to a file, and then have node-mime determine it for me!)

I want to know the best way to determine if a file is an image, with 0 margin of error.

Edit: I just realized that node-mime isn't "magic". It just checks for the extension :( ...

Edit2: I found this: https://github.com/SaltwaterC/mime-magic

like image 611
TIMEX Avatar asked Dec 12 '11 11:12

TIMEX


People also ask

How do you check a url is an image?

On your computer, go to images.google.com. Search for the image. In Images results, click the image. At the top of your browser, click the address bar to select the entire URL.

How do I validate an image link?

To check if a url is an image, call the test() method on a regular expression that matches an image extension at the end of a string, e.g. . png or . jpg . The test() method will check if the url ends with an image extension and will return true if it does.


1 Answers

Just read the first bytes of the stream, and check it for the so called "magic number".

Magic numbers are the first bits of a file which uniquely identify the type of file.

For example:
-Every JPEG file begins with ff d8 (hex).
-Every png file begins with a 89 50 4e 47.
-There is a comprehensive table of magic numbers here

This way even if you have a file without extension you can still detect its type.
Hope this helps.

like image 169
rcode Avatar answered Oct 13 '22 05:10

rcode