Sha256: 32826a05597a8fa8a4dac5f005e556ccdf4809cbcd728805a0ae33731b289df0

Contents?: true

Size: 1.39 KB

Versions: 6

Compression:

Stored size: 1.39 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.parse(URI.escape(uri))
        end

        def original_filename
          File.basename(@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?
          file = RemoteFile.new(uri)
          raise CarrierWave::DownloadError, "trying to download a file which is not served over HTTP" unless file.http?
          cache!(file) 
        end
      end

    end # Download
  end # Uploader
end # CarrierWave

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
locomotive_carrierwave-0.5.0.1.beta3 lib/carrierwave/uploader/download.rb
carrierwave-0.5.1 lib/carrierwave/uploader/download.rb
locomotive_carrierwave-0.5.0.1.beta2 lib/carrierwave/uploader/download.rb
locomotive_carrierwave-0.5.0.1.beta1 lib/carrierwave/uploader/download.rb
locomotive_carrierwave-0.5.0.1 lib/carrierwave/uploader/download.rb
carrierwave-0.5.0 lib/carrierwave/uploader/download.rb