Class: ImageVise::FetcherHTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/image_vise/fetchers/fetcher_http.rb

Defined Under Namespace

Classes: AccessError, UpstreamError

Constant Summary collapse

EXTERNAL_IMAGE_FETCH_TIMEOUT_SECONDS =
5

Class Method Summary collapse

Class Method Details

.fetch_uri_to_tempfile(uri) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/image_vise/fetchers/fetcher_http.rb', line 14

def self.fetch_uri_to_tempfile(uri)
  tf = Tempfile.new 'imagevise-http-download'
  verify_uri_access!(uri)
  s = Patron::Session.new
  s.automatic_content_encoding = true
  s.timeout = EXTERNAL_IMAGE_FETCH_TIMEOUT_SECONDS
  s.connect_timeout = EXTERNAL_IMAGE_FETCH_TIMEOUT_SECONDS
  
  response = s.get_file(uri.to_s, tf.path)
  
  if response.status != 200
    raise UpstreamError.new(response.status, "Unfortunate upstream response #{response.status} on #{uri}")
  end
  
  tf
rescue Exception => e
  ImageVise.close_and_unlink(tf)
  raise e
end

.verify_uri_access!(uri) ⇒ Object

Raises:



34
35
36
37
38
# File 'lib/image_vise/fetchers/fetcher_http.rb', line 34

def self.verify_uri_access!(uri)
  host = uri.host
  return if ImageVise.allowed_hosts.include?(uri.host)
  raise AccessError, "#{uri} is not permitted as source"
end