Sha256: aa66adb629cdcaec8701f7980696cbf0f51450a127f410a901fda471962cca32

Contents?: true

Size: 1.86 KB

Versions: 46

Compression:

Stored size: 1.86 KB

Contents

module Hyrax
  module Actors
    # Creates a work and attaches files to the work
    class CreateWithFilesActor < Hyrax::Actors::AbstractActor
      # @param [Hyrax::Actors::Environment] env
      # @return [Boolean] true if create was successful
      def create(env)
        uploaded_file_ids = filter_file_ids(env.attributes.delete(:uploaded_files))
        files = uploaded_files(uploaded_file_ids)
        validate_files(files, env) && next_actor.create(env) && attach_files(files, env)
      end

      # @param [Hyrax::Actors::Environment] env
      # @return [Boolean] true if update was successful
      def update(env)
        uploaded_file_ids = filter_file_ids(env.attributes.delete(:uploaded_files))
        files = uploaded_files(uploaded_file_ids)
        validate_files(files, env) && next_actor.update(env) && attach_files(files, env)
      end

      private

        def filter_file_ids(input)
          Array.wrap(input).select(&:present?)
        end

        # ensure that the files we are given are owned by the depositor of the work
        def validate_files(files, env)
          expected_user_id = env.user.id
          files.each do |file|
            if file.user_id != expected_user_id
              Rails.logger.error "User #{env.user.user_key} attempted to ingest uploaded_file #{file.id}, but it belongs to a different user"
              return false
            end
          end
          true
        end

        # @return [TrueClass]
        def attach_files(files, env)
          return true if files.blank?
          AttachFilesToWorkJob.perform_later(env.curation_concern, files, env.attributes.to_h.symbolize_keys)
          true
        end

        # Fetch uploaded_files from the database
        def uploaded_files(uploaded_file_ids)
          return [] if uploaded_file_ids.empty?
          UploadedFile.find(uploaded_file_ids)
        end
    end
  end
end

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
hyrax-2.9.6 app/actors/hyrax/actors/create_with_files_actor.rb
hyrax-2.9.5 app/actors/hyrax/actors/create_with_files_actor.rb
hyrax-2.9.4 app/actors/hyrax/actors/create_with_files_actor.rb
hyrax-2.9.3 app/actors/hyrax/actors/create_with_files_actor.rb
hyrax-2.9.2 app/actors/hyrax/actors/create_with_files_actor.rb
hyrax-2.9.1 app/actors/hyrax/actors/create_with_files_actor.rb
hyrax-2.9.0 app/actors/hyrax/actors/create_with_files_actor.rb
hyrax-2.8.0 app/actors/hyrax/actors/create_with_files_actor.rb
hyrax-2.7.2 app/actors/hyrax/actors/create_with_files_actor.rb
hyrax-2.7.1 app/actors/hyrax/actors/create_with_files_actor.rb
hyrax-2.7.0 app/actors/hyrax/actors/create_with_files_actor.rb
hyrax-2.6.0 app/actors/hyrax/actors/create_with_files_actor.rb
hyrax-2.5.1 app/actors/hyrax/actors/create_with_files_actor.rb
hyrax-2.5.0 app/actors/hyrax/actors/create_with_files_actor.rb
hyrax-3.0.0.pre.beta2 app/actors/hyrax/actors/create_with_files_actor.rb
hyrax-2.4.1 app/actors/hyrax/actors/create_with_files_actor.rb
hyrax-3.0.0.pre.beta1 app/actors/hyrax/actors/create_with_files_actor.rb
hyrax-2.4.0 app/actors/hyrax/actors/create_with_files_actor.rb
hyrax-2.3.3 app/actors/hyrax/actors/create_with_files_actor.rb
hyrax-2.3.2 app/actors/hyrax/actors/create_with_files_actor.rb