Sha256: 5e6e59e1b4a4ece3b823132e33b7f2a26c1b14039aef9a018f735caeac006107

Contents?: true

Size: 1.76 KB

Versions: 58

Compression:

Stored size: 1.76 KB

Contents

# encoding: utf-8

require 'open-uri'

module CarrierWave
  module Uploader
    module Download
      extend ActiveSupport::Concern

      include CarrierWave::Uploader::Callbacks
      include CarrierWave::Uploader::Configuration
      include CarrierWave::Uploader::Cache

      class RemoteFile
        def initialize(uri)
          @uri = uri
        end

        def original_filename
          File.basename(file.base_uri.path)
        end

        def respond_to?(*args)
          super or file.respond_to?(*args)
        end

        def http?
          @uri.scheme =~ /^https?$/
        end

      private

        def file
          if @file.blank?
            @file = Kernel.open(@uri.to_s)
            @file = @file.is_a?(String) ? StringIO.new(@file) : @file
          end
          @file
        end

        def method_missing(*args, &block)
          file.send(*args, &block)
        end
      end

      ##
      # Caches the file by downloading it from the given URL.
      #
      # === Parameters
      #
      # [url (String)] The URL where the remote file is stored
      #
      def download!(uri)
        unless uri.blank?
          processed_uri = process_uri(uri)
          file = RemoteFile.new(processed_uri)
          raise CarrierWave::DownloadError, "trying to download a file which is not served over HTTP" unless file.http?
          cache!(file)
        end
      end

      ##
      # Processes the given URL by parsing and escaping it. Public to allow overriding.
      #
      # === Parameters
      #
      # [url (String)] The URL where the remote file is stored
      #
      def process_uri(uri)
        URI.parse(URI.escape(URI.unescape(uri)).gsub("[", "%5B").gsub("]", "%5D").gsub("+", "%2B"))
      end

    end # Download
  end # Uploader
end # CarrierWave

Version data entries

58 entries across 53 versions & 4 rubygems

Version Path
classiccms-0.7.5 vendor/bundle/gems/carrierwave-0.6.2/lib/carrierwave/uploader/download.rb
classiccms-0.7.4 vendor/bundle/gems/carrierwave-0.6.2/lib/carrierwave/uploader/download.rb
classiccms-0.7.3 vendor/bundle/gems/carrierwave-0.6.2/lib/carrierwave/uploader/download.rb
classiccms-0.7.2 vendor/bundle/gems/carrierwave-0.6.2/lib/carrierwave/uploader/download.rb
classiccms-0.7.1 vendor/bundle/gems/carrierwave-0.6.2/lib/carrierwave/uploader/download.rb
classiccms-0.7.0 vendor/bundle/gems/carrierwave-0.6.2/lib/carrierwave/uploader/download.rb
classiccms-0.6.9 vendor/bundle/gems/carrierwave-0.6.2/lib/carrierwave/uploader/download.rb
classiccms-0.6.8 vendor/bundle/gems/carrierwave-0.6.2/lib/carrierwave/uploader/download.rb
classiccms-0.6.7 vendor/bundle/gems/carrierwave-0.6.2/lib/carrierwave/uploader/download.rb
classiccms-0.6.6 vendor/bundle/gems/carrierwave-0.6.2/lib/carrierwave/uploader/download.rb
classiccms-0.6.5 vendor/bundle/gems/carrierwave-0.6.2/lib/carrierwave/uploader/download.rb
classiccms-0.6.4 vendor/bundle/gems/carrierwave-0.6.2/lib/carrierwave/uploader/download.rb
classiccms-0.6.3 vendor/bundle/gems/carrierwave-0.6.2/lib/carrierwave/uploader/download.rb
classiccms-0.6.2 vendor/bundle/gems/carrierwave-0.6.2/lib/carrierwave/uploader/download.rb
classiccms-0.6.1 vendor/bundle/gems/carrierwave-0.6.2/lib/carrierwave/uploader/download.rb
classiccms-0.6.0 vendor/bundle/gems/carrierwave-0.6.2/lib/carrierwave/uploader/download.rb
classiccms-0.5.17 vendor/bundle/gems/carrierwave-0.6.2/lib/carrierwave/uploader/download.rb
classiccms-0.5.16 vendor/bundle/gems/carrierwave-0.6.2/lib/carrierwave/uploader/download.rb
classiccms-0.5.15 vendor/bundle/gems/carrierwave-0.6.2/lib/carrierwave/uploader/download.rb
classiccms-0.5.14 vendor/bundle/gems/carrierwave-0.6.2/lib/carrierwave/uploader/download.rb