Sha256: a4aa5862c8ffc986a75c44df406b1abbcf658aadf834e36f6d52d57a967564de

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

require 'uri'
require 'net/http'
require 'fileutils'
require 'image_resizer/lib/disk_file_cache'

module ImageResizer
  module Downloader
    ORIGINAL_PATH = "tmp/resize_original_images"
    FileUtils.mkdir_p(ORIGINAL_PATH)
    DOWNLOAD_CACHE = DiskFileCache.new(ORIGINAL_PATH, 1000)

    # Downloads the image to a temp file, returns the path to the temp file.
    def download_image_to_temp(image_url, tmp_path)
      ext = File.extname(image_url)

      # Download the image
      uri = URI(image_url)

      Net::HTTP.start(uri.host, uri.port, :use_ssl => (uri.scheme == 'https')) do |http|
        request = Net::HTTP::Get.new uri.request_uri
        http.request request do |response|
          File.open(tmp_path, 'wb') do |io|
            response.read_body do |chunk|
              io.write chunk
            end
          end
        end
      end
    end

    # Downloads or pulls the file from cache and returns the path to the file
    # locally.  Handles clearing the cache later.
    def download_cached(image_url)
      DOWNLOAD_CACHE.fetch(image_url) do |download_path|
        download_image_to_temp(image_url, download_path)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
volt-image_resizer-0.1.1 app/image_resizer/lib/downloader.rb
volt-image_resizer-0.1.0 app/s3_image_resizer/lib/downloader.rb