Sha256: 14e7d18ac87fb75d714e1902abad224f0e055ed4a8c01d3b54e2f5fbabb77ed7

Contents?: true

Size: 1.29 KB

Versions: 7

Compression:

Stored size: 1.29 KB

Contents

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

class ImportUrlJob < ActiveFedoraIdBasedJob
  queue_as :import_url

  def perform(id)
    @id = id
    user = User.find_by_user_key(file_set.depositor)

    Tempfile.open(id.tr('/', '_')) do |f|
      copy_remote_file(file_set.import_url, f)

      # reload the generic file once the data is copied since this is a long running task
      file_set.reload

      # attach downloaded file to generic file stubbed out
      if CurationConcerns::FileSetActor.new(file_set, user).create_content(f)
        # send message to user on download success
        CurationConcerns.config.callback.run(:after_import_url_success, file_set, user)
      else
        CurationConcerns.config.callback.run(:after_import_url_failure, file_set, user)
      end
    end
  end

  def copy_remote_file(_import_url, f)
    f.binmode
    # download file from url
    uri = URI(file_set.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
    mime_type = nil

    http.start do
      http.request_get(uri.request_uri) do |resp|
        mime_type = resp.content_type
        resp.read_body do |segment|
          f.write(segment)
        end
      end
    end
    f.rewind
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
curation_concerns-models-0.10.0 app/jobs/import_url_job.rb
curation_concerns-models-0.9.0 app/jobs/import_url_job.rb
curation_concerns-models-0.8.0 app/jobs/import_url_job.rb
curation_concerns-models-0.7.0 app/jobs/import_url_job.rb
curation_concerns-models-0.6.0 app/jobs/import_url_job.rb
curation_concerns-models-0.5.0 app/jobs/import_url_job.rb
curation_concerns-models-0.4.0 app/jobs/import_url_job.rb