Sha256: 0388545ea04ce74321bf824bb103e4488a302f914b1eb99a2f9b4a548213bffb

Contents?: true

Size: 1.4 KB

Versions: 6

Compression:

Stored size: 1.4 KB

Contents

require 'net/https'
require 'uri'
require 'tempfile'

class ImportUrlJob 

  def queue_name
    :import_url
  end

  attr_accessor :pid

  def initialize(pid)
    self.pid = pid 
  end

  def run
    generic_file = GenericFile.find(self.pid)
    f = Tempfile.new(self.pid)
    f.binmode

    # download file from url
    uri = URI(generic_file.import_url)
    http = Net::HTTP.new(uri.host, uri.port) 
    http.use_ssl = uri.scheme == "https"  # enable SSL/TLS
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE

    http.start do  
      http.request_get(uri) do |resp|
        resp.read_body do |segment|
          f.write(segment)
        end
      end
    end 
    job_user = User.batchuser()
    user = User.find_by_user_key(generic_file.depositor)
    # check for virus
    if Sufia::GenericFile::Actions.virus_check(f) != 0
      message = "The file (#{File.basename(uri.path)}) was unable to be imported because it contained a virus."
      job_user.send_message(user, message, 'File Import Error') 
      return
    end

    f.rewind
    # attach downloaded file to generic file stubbed out
    Sufia::GenericFile::Actions.create_content(generic_file, f, File.basename(uri.path), 'content', user)
    # add message to user for downloaded file
    message = "The file (#{File.basename(uri.path)}) was successfully imported." 
    job_user.send_message(user, message, 'File Import') 
 
  ensure
    f.close
    f.unlink
  end
end

Version data entries

6 entries across 6 versions & 2 rubygems

Version Path
sufia-3.0.0 sufia-models/lib/sufia/models/jobs/import_url_job.rb
sufia-models-3.0.0 lib/sufia/models/jobs/import_url_job.rb
sufia-2.0.1 sufia-models/lib/sufia/models/jobs/import_url_job.rb
sufia-models-2.0.1 lib/sufia/models/jobs/import_url_job.rb
sufia-2.0.0 sufia-models/lib/sufia/models/jobs/import_url_job.rb
sufia-models-2.0.0 lib/sufia/models/jobs/import_url_job.rb