Sha256: 0477c2dbf9e9141c29a0bc223819a3002bed66d3dd38682f29f0582c233bf3d5

Contents?: true

Size: 1.27 KB

Versions: 12

Compression:

Stored size: 1.27 KB

Contents

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

class ImportUrlJob < ActiveFedoraPidBasedJob

  def queue_name
    :import_url
  end

  def run
    user = User.find_by_user_key(generic_file.depositor)

    Tempfile.open(self.pid) do |f|
      path = copy_remote_file(generic_file.import_url, f)
      # attach downloaded file to generic file stubbed out
      if Sufia::GenericFile::Actor.new(generic_file, user).create_content(f, path, 'content')
        # add message to user for downloaded file
        message = "The file (#{generic_file.content.label}) was successfully imported."
        job_user.send_message(user, message, 'File Import')
      else
        job_user.send_message(user, generic_file.errors.full_messages.join(', '), 'File Import Error')
      end
    end
  end

  def copy_remote_file(import_url, f)
    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

    f.rewind
    uri.path
  end

  def job_user
    User.batchuser
  end
end

Version data entries

12 entries across 12 versions & 2 rubygems

Version Path
sufia-5.0.0 sufia-models/app/jobs/import_url_job.rb
sufia-models-5.0.0 app/jobs/import_url_job.rb
sufia-5.0.0.rc1 sufia-models/app/jobs/import_url_job.rb
sufia-models-5.0.0.rc1 app/jobs/import_url_job.rb
sufia-4.3.1 sufia-models/app/jobs/import_url_job.rb
sufia-models-4.3.1 app/jobs/import_url_job.rb
sufia-4.2.0 sufia-models/app/jobs/import_url_job.rb
sufia-models-4.2.0 app/jobs/import_url_job.rb
sufia-4.1.0 sufia-models/app/jobs/import_url_job.rb
sufia-models-4.1.0 app/jobs/import_url_job.rb
sufia-4.0.1 sufia-models/app/jobs/import_url_job.rb
sufia-models-4.0.1 app/jobs/import_url_job.rb