Class: Moxify::URLTempfile

Inherits:
Tempfile
  • Object
show all
Defined in:
lib/moxify/url_tempfile.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(url) ⇒ URLTempfile

Returns a new instance of URLTempfile.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/moxify/url_tempfile.rb', line 9

def initialize(url)
  @url = URI.parse(url)

  # see if we can get a filename
  raise "Unable to determine filename for URL uploaded file." unless original_filename

  begin
    # HACK to get around inability to set VERIFY_NONE with open-uri
    old_verify_peer_value = OpenSSL::SSL::VERIFY_PEER
    openssl_verify_peer = OpenSSL::SSL::VERIFY_NONE

    super('urlupload', Dir.tmpdir, :encoding => 'ascii-8bit')
    Kernel.open(url) do |file|
      @content_type = file.content_type
      raise "Unable to determine MIME type for URL uploaded file." unless content_type

      self.write file.read
      self.flush
    end
  ensure
    openssl_verify_peer = old_verify_peer_value
  end
end

Instance Attribute Details

#content_typeObject (readonly)

Returns the value of attribute content_type.



7
8
9
# File 'lib/moxify/url_tempfile.rb', line 7

def content_type
  @content_type
end

Instance Method Details

#original_filenameObject



33
34
35
36
37
38
# File 'lib/moxify/url_tempfile.rb', line 33

def original_filename
  # Take the URI path and strip off everything after last slash, assume this
  # to be filename (URI path already removes any query string)
  match = @url.path.match(/^.*\/(.+)$/)
  return (match ? match[1] : nil)
end