Sha256: 4673884e12e564563827d80316f37bb3116816ea58eafde9c061a88b40e5fc4a

Contents?: true

Size: 1.93 KB

Versions: 17

Compression:

Stored size: 1.93 KB

Contents

# Converts UploadedFiles into FileSets and attaches them to works.
class AttachFilesToWorkJob < Hyrax::ApplicationJob
  queue_as Hyrax.config.ingest_queue_name

  # @param [ActiveFedora::Base] work - the work object
  # @param [Array<Hyrax::UploadedFile>] uploaded_files - an array of files to attach
  def perform(work, uploaded_files, **work_attributes)
    validate_files!(uploaded_files)
    depositor = proxy_or_depositor(work)
    user = User.find_by_user_key(depositor)
    work_permissions = work.permissions.map(&:to_hash)
    metadata = visibility_attributes(work_attributes)
    uploaded_files.each do |uploaded_file|
      next if uploaded_file.file_set_uri.present?

      actor = Hyrax::Actors::FileSetActor.new(FileSet.create, user)
      uploaded_file.update(file_set_uri: actor.file_set.uri)
      actor.file_set.permissions_attributes = work_permissions
      actor.create_metadata(metadata)
      actor.create_content(uploaded_file)
      actor.attach_to_work(work)
    end
  end

  private

    # The attributes used for visibility - sent as initial params to created FileSets.
    def visibility_attributes(attributes)
      attributes.slice(:visibility, :visibility_during_lease,
                       :visibility_after_lease, :lease_expiration_date,
                       :embargo_release_date, :visibility_during_embargo,
                       :visibility_after_embargo)
    end

    def validate_files!(uploaded_files)
      uploaded_files.each do |uploaded_file|
        next if uploaded_file.is_a? Hyrax::UploadedFile
        raise ArgumentError, "Hyrax::UploadedFile required, but #{uploaded_file.class} received: #{uploaded_file.inspect}"
      end
    end

    ##
    # A work with files attached by a proxy user will set the depositor as the intended user
    # that the proxy was depositing on behalf of. See tickets #2764, #2902.
    def proxy_or_depositor(work)
      work.on_behalf_of.blank? ? work.depositor : work.on_behalf_of
    end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
hyrax-2.9.6 app/jobs/attach_files_to_work_job.rb
hyrax-2.9.5 app/jobs/attach_files_to_work_job.rb
hyrax-2.9.4 app/jobs/attach_files_to_work_job.rb
hyrax-2.9.3 app/jobs/attach_files_to_work_job.rb
hyrax-2.9.2 app/jobs/attach_files_to_work_job.rb
hyrax-2.9.1 app/jobs/attach_files_to_work_job.rb
hyrax-2.9.0 app/jobs/attach_files_to_work_job.rb
hyrax-2.8.0 app/jobs/attach_files_to_work_job.rb
hyrax-2.7.2 app/jobs/attach_files_to_work_job.rb
hyrax-2.7.1 app/jobs/attach_files_to_work_job.rb
hyrax-2.7.0 app/jobs/attach_files_to_work_job.rb
hyrax-2.6.0 app/jobs/attach_files_to_work_job.rb
hyrax-2.5.1 app/jobs/attach_files_to_work_job.rb
hyrax-2.5.0 app/jobs/attach_files_to_work_job.rb
hyrax-3.0.0.pre.beta2 app/jobs/attach_files_to_work_job.rb
hyrax-2.4.1 app/jobs/attach_files_to_work_job.rb
hyrax-3.0.0.pre.beta1 app/jobs/attach_files_to_work_job.rb