Sha256: d8e12e25bffd9e3a8bdec8a165fbb887424027c838a259c745b6cf5e226d5462

Contents?: true

Size: 1.32 KB

Versions: 24

Compression:

Stored size: 1.32 KB

Contents

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

class ImportUrlJob < ActiveFedoraPidBasedJob

  def queue_name
    :import_url
  end

  def run
    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.request_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

24 entries across 24 versions & 2 rubygems

Version Path
sufia-3.4.0 sufia-models/lib/sufia/models/jobs/import_url_job.rb
sufia-models-3.4.0 lib/sufia/models/jobs/import_url_job.rb
sufia-3.4.0.rc4 sufia-models/lib/sufia/models/jobs/import_url_job.rb
sufia-models-3.4.0.rc4 lib/sufia/models/jobs/import_url_job.rb
sufia-3.4.0.rc3 sufia-models/lib/sufia/models/jobs/import_url_job.rb
sufia-models-3.4.0.rc3 lib/sufia/models/jobs/import_url_job.rb
sufia-3.3.1 sufia-models/lib/sufia/models/jobs/import_url_job.rb
sufia-models-3.3.1 lib/sufia/models/jobs/import_url_job.rb
sufia-3.4.0.rc2 sufia-models/lib/sufia/models/jobs/import_url_job.rb
sufia-models-3.4.0.rc2 lib/sufia/models/jobs/import_url_job.rb
sufia-3.4.0.rc1 sufia-models/lib/sufia/models/jobs/import_url_job.rb
sufia-models-3.4.0.rc1 lib/sufia/models/jobs/import_url_job.rb
sufia-3.3.0 sufia-models/lib/sufia/models/jobs/import_url_job.rb
sufia-models-3.3.0 lib/sufia/models/jobs/import_url_job.rb
sufia-3.2.1 sufia-models/lib/sufia/models/jobs/import_url_job.rb
sufia-models-3.2.1 lib/sufia/models/jobs/import_url_job.rb
sufia-3.1.3 sufia-models/lib/sufia/models/jobs/import_url_job.rb
sufia-models-3.1.3 lib/sufia/models/jobs/import_url_job.rb
sufia-3.1.2 sufia-models/lib/sufia/models/jobs/import_url_job.rb
sufia-models-3.1.2 lib/sufia/models/jobs/import_url_job.rb