Sha256: 87d97963686b4f1a88581f024986c28dd6a1d96a09433b77b153f3ee7b8257bd

Contents?: true

Size: 1.29 KB

Versions: 5

Compression:

Stored size: 1.29 KB

Contents

module Moxify
  require 'mime/types'
  require 'openssl'
  require 'open-uri'

  class URLTempfile < Tempfile
    attr :content_type

    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

    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

    protected

    def openssl_verify_peer=(value)
      silence_warnings do
        OpenSSL::SSL.const_set("VERIFY_PEER", value)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
moxify-0.1.9.1 lib/moxify/url_tempfile.rb
moxify-0.1.9 lib/moxify/url_tempfile.rb
moxify-0.1.8 lib/moxify/url_tempfile.rb
moxify-0.1.7.4 lib/moxify/url_tempfile.rb
moxify-0.1.7.3 lib/moxify/url_tempfile.rb