2013-04-08 19 views
6

リモートURLが画像であるかどうかを確認しようとしています。ほとんどのURLには.jpg、.pngなどがありますが、Google画像のような画像には拡張子がありません。Carrierwave画像の拡張子

https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSbK2NSUILnFozlX-oCWQ0r2PS2gHPPF7c8XaxGuJFGe83KGJkhFtlLXU_u

私は、URLが画像であるかどうかを判断するためにFastImageを使用してみました。それはどんなURLがそれに供給されても動作します...

リモートURLがFastImageを使用し、アップロードされたファイルがホワイトリストを使用することをどうすれば保証できますか?ここに私のアップローダには何があります。 Avatar_remote_urlは認識されません...アップローダでは、通常のファイルではなくリモートURLをテストするために何をしますか?あなたが仕事をしなければならないすべてはあなたが画像のコンテンツタイプを取得するために、サーバーにHEADリクエストを送ることができるというようなURLがある場合

def extension_white_list 
    if defined? avatar_remote_url && !FastImage.type(CGI::unescape(avatar_remote_url)).nil? 
     # ok to process 
    else # regular uploaded file should detect the following extensions 
     %w(jpg jpeg gif png) 
    end 
    end 
+0

正規表現を使用してhttp://encrpyted-tbn0.gstaticから何かをアップロードできる可能性があります – ahmet

答えて

3

。それからは、拡張子

require 'net/http' 
require 'mime/types' 

def get_extension(url) 
    uri = URI.parse(url) 
    http = Net::HTTP.new(uri.host, uri.port) 
    http.use_ssl = true if uri.scheme == 'https' 
    request = Net::HTTP::Head.new(uri.request_uri) 
    response = http.request(request) 
    content_type = response['Content-Type'] 
    MIME::Types[content_type].first.extensions.first 
end 
2

を得ることができ、私はあなたが提供してCarrierWave Wiki for validating remote URLsで提供されているコードのいくつかのコードで働いています。

lib/remote_image_validator.rbに新しいバリデータを作成できます。お使いのモデルで次に

require 'fastimage' 

class RemoteImageValidator < ActiveModel::EachValidator 
    def validate_each(object, attribute, value) 
    raise(ArgumentError, "A regular expression must be supplied as the :format option of the options hash") unless options[:format].nil? || options[:format].is_a?(Regexp) 
    configuration = { :message => "is invalid or not responding", :format => URI::regexp(%w(http https)) } 
    configuration.update(options) 

    if value =~ configuration[:format] 
     begin 
     if FastImage.type(CGI::unescape(avatar_remote_url)) 
      true 
     else 
      object.errors.add(attribute, configuration[:message]) and false 
     end 
     rescue 
     object.errors.add(attribute, configuration[:message]) and false 
     end 
    else 
     object.errors.add(attribute, configuration[:message]) and false 
    end 
    end 
end 

class User < ActiveRecord::Base 
    validates :avatar_remote_url, 
    :remote_image => { 
     :format => /(^$)|(^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$)/ix, 
     :unless => remote_avatar_url.blank? 
    } 
end 
1

ImageMagickには、使用する正しいエンコーダを見つけ出すことができなかったので、私は、元は失敗していたとは別のバージョンを作成、同様の問題を抱えていたため、行方不明の拡張に。ここで私は私の問題を固定レールに適用されるサル-パッチは次のとおりです。

module CarrierWave 
    module Uploader 
    module Download 
     class RemoteFile 
     def original_filename 
      value = File.basename(file.base_uri.path) 
      mime_type = Mime::Type.lookup(file.content_type) 
      unless File.extname(value).present? || mime_type.blank? 
      value = "#{value}.#{mime_type.symbol}" 
      end 
      value 
     end 
     end 
    end 
    end 
end 

私は、コンテンツの種類が適切に設定されている場合には、ファイルの拡張子が存在することを保証しますので、これは同様にあなたが持っている問題を解決すると考えています。

UPDATE:

carrierwaveのマスターブランチがファイル名を把握するContent-Dispositionヘッダを使用してこの問題に対する別の解決策を持っています。 githubに関連するプルリクエストがあります。

+0

これはすばらしいことです!なぜ、あなたはCarrierwaveへのプルリクエストをしなかったのですか?これは現時点ではイメージサーバとすべての現代で明らかに良いアプローチです... –

+0

私は実際に1つを送信していました@NielsKristian :-D – Hamed

+0

歓迎されるhttps://github.com/carrierwaveuploader/carrierwave/issues/1247 :-) –

関連する問題