2016-08-16 5 views
0

標準リクエストモジュールを使用して、次のスクリプトを実行してファイルのMIMEタイプをチェックすることができます。request-promise:ファイルのMIMEタイプをダウンロード/チェックしますか?

var request = require('request'); 
var url = "http://www.somedomain.com/somepicture.jpg"; 
var magic = { 
    jpg: 'ffd8ffe0', 
    png: '89504e47', 
    gif: '47494638' 
}; 
var options = { 
    method: 'GET', 
    url: url, 
    encoding: null // keeps the body as buffer 
}; 

request(options, function (err, response, body) { 
    if(!err && response.statusCode == 200){ 
     var magicNumberInBody = body.toString('hex',0,4); 
     if (magicNumberInBody == magic.jpg || 
      magicNumberInBody == magic.png || 
      magicNumberInBody == magic.gif) { 
      console.log("It's an image!"); 
      return true; 
     } 
    } 
}); 

これはかなり自明です。しかし、node.jsを使ってPromisesに頼っているとき、どうすれば上記のことができますか?私はrequest-promiseをインストールして使用しようとしていて、次のスクリプトを書きました。

return rp("http://www.somedomain.com/somepicture.jpg") 
      .then((response) => { 
       var magicNumberInBody = response.toString('hex',0,4); 
       console.log(magicNumberInBody); 
       if (magicNumberInBody == magic.jpg || 
        magicNumberInBody == magic.png || 
        magicNumberInBody == magic.gif) { 
        console.log("It's an image!"); 
        return true; 
       } 

しかし、html/txtに基づいていないものについては、リクエストの約束は生のバイナリを返すようです。

上記のデータを意味のあるものに変更する方法は誰にも分かりますが、MIMEタイプがjpg/png/gifであるかどうかを簡単に識別できますか?

rp({ 
    uri: "http://www.somedomain.com/somepicture.jpg", 
    resolveWithFullResponse: true 
}).then(res => { 
    console.log(res.headers['content-type']) 
}) 

答えて

1

あなたがresolveWithFullResponseオプションを使用する必要があります。

これは何らかの方法で役立ちます。

+0

ありがとうございます。私はこれより多くのデータを入手しましたが、実際のスクリプトを実行する方法はまだありません。 res.headers ['content-type']は画像の "content-type": 'application/octet-stream'を返すだけです。実際のMIMEタイプを表示する必要があります。 – Eoghan

+0

申し訳ありませんが、これは他の問題であり、それについてはほとんどできません。https://stackoverflow.com/questions/9707204/how-handling-images-mime-type-application-octet-stream – martriay

0

私はその後、別の要求は、実際にファイルをダウンロードすることが

 // Download 
     return Image._head($, $(image).attr('src')) 
     .then((filename) => { 
      return Image._download($, filename) 
     }) 
     .then((code) => { 
      return $ 
     }) 


    // Request 
    Request.head(options) 
    .then((data) => { 
     console.log(data) 
     // Get the content type 
     // const type = data['content-type'] 
     // let ext = '' 

     // // Extract extension 
     // if (type === 'image/jpeg' || type === 'image/pjpeg') { 
     // ext = 'jpg' 
     // } else if (type === 'image/png') { 
     // ext = 'png' 
     // } else if (type === 'image/gif') { 
     // ext = 'gif' 
     // } 

     // Get filename 
     let filename = null 
     const disposition = data['content-disposition'] 

     if (disposition && disposition.indexOf('inline') !== -1) { 
     var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/ 
     var matches = filenameRegex.exec(disposition) 
     if (matches != null && matches[1]) { 
      filename = matches[1].replace(/['"]/g, '') 
     } 
     } 

     // Path 
     // const path = Path.join('./resources/images', `${filename}`) 

     // console.log('content-type:', data.headers['content-type']) 
     // console.log('content-length:', data.headers['content-length']) 
     // console.log('content-disposition:', data.headers['content-disposition']) 
     console.log('filename:', filename) 

     // resolve(data.html) 
     resolve(filename) 
    }) 
    .catch((error) => { 
     resolve(new Error(`${error} - Instagram - ${src}`)) 
    }) 

第1のヘッド要求を実行します。

関連する問題