Class: CarrierWave::Downloader::RemoteFile

Inherits:
Object
  • Object
show all
Defined in:
lib/carrierwave/downloader/remote_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ RemoteFile

Returns a new instance of RemoteFile.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/carrierwave/downloader/remote_file.rb', line 6

def initialize(file)
  case file
  when String
    @file = StringIO.new(file)
  when Net::HTTPResponse
    body = file.body
    raise CarrierWave::DownloadError, 'could not download file: No Content' if body.nil?

    @file = StringIO.new(body)
    @content_type = file.content_type
    @headers = file
    @uri = file.uri
  else
    @file = file
    @content_type = file.content_type
    @headers = file.meta
    @uri = file.base_uri
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(*args, &block) ⇒ Object (private)



59
60
61
# File 'lib/carrierwave/downloader/remote_file.rb', line 59

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

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



4
5
6
# File 'lib/carrierwave/downloader/remote_file.rb', line 4

def file
  @file
end

#uriObject (readonly)

Returns the value of attribute uri.



4
5
6
# File 'lib/carrierwave/downloader/remote_file.rb', line 4

def uri
  @uri
end

Instance Method Details

#content_typeObject



26
27
28
# File 'lib/carrierwave/downloader/remote_file.rb', line 26

def content_type
  @content_type || 'application/octet-stream'
end

#headersObject



30
31
32
# File 'lib/carrierwave/downloader/remote_file.rb', line 30

def headers
  @headers || {}
end

#original_filenameObject



34
35
36
37
38
39
40
41
42
# File 'lib/carrierwave/downloader/remote_file.rb', line 34

def original_filename
  filename = filename_from_header || filename_from_uri
  extensions = Marcel::Magic.new(content_type).extensions
  unless File.extname(filename).present? || extensions.blank?
    extension = extensions.first
    filename = "#{filename}.#{extension}"
  end
  filename
end