1

I am trying to verify if a link is a valid image with magic number. Most of the images link work fine. But here are set of images on trump's site that does not produce correct magic numbers, though they appear to work fine on browser. Magic number they produce is 3c21444f.

Below is my code, Any help would be appreciated:

var request = require('request');
var magic = {
    jpg: 'ffd8ffe0',
    jpg1: 'ffd8ffe1',
    png: '89504e47',
    gif: '47494638'
};
var options = {
  method: 'GET',
  url: 'https://assets.donaldjtrump.com/gallery/4749/screen_shot_2016-10-30_at_1.39.54_pm.png',
  encoding: null // keeps the body as buffer
};
request(options, function (error, response, body) {
  if(!error) {
     var magicNumberInBody = body.toString('hex', 0, 4);
     if (magicNumberInBody == magic.jpg || 
         magicNumberInBody == magic.jpg1 || 
         magicNumberInBody == magic.png ||
         magicNumberInBody == magic.gif) {
         console.log('Valid image');
     } else {
         console.log('Invalid Image', magicNumberInBody);
     }
  }
});
Yalamber
  • 7,360
  • 15
  • 63
  • 89
  • 1
    That's `<!DO` which seems like it's probably a doctype tag. – polarysekt Feb 23 '17 at 13:46
  • hmm seems like cloudflare is returning html page instead of image when I am making request to that server from node.js script. May be I need to add proper headers to get image properly in script. Thanks – Yalamber Feb 23 '17 at 13:58

1 Answers1

0

So apparently it seemed to be issue with cloudflare blocking my requests to image. So I fixed it using UserAgent Headers to request for those images.

var options = {
  method: 'GET',
  url: 'https://assets.donaldjtrump.com/gallery/4749/screen_shot_2016-10-30_at_1.39.54_pm.png',
  headers: {
    'User-Agent': 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'
  },
  encoding: null // keeps the body as buffer
};
Yalamber
  • 7,360
  • 15
  • 63
  • 89