Class: JobIoWrapper
- Inherits:
-
ApplicationRecord
- Object
- ApplicationRecord
- JobIoWrapper
- Defined in:
- app/models/job_io_wrapper.rb
Overview
Along with user and file_set_id, path or uploaded_file are required. If both are provided: path is used preferentially for access IF it exists; however, the uploaded_file is used preferentially for default original_name and mime_type, because it already has that information.
Primarily for jobs like IngestJob to revivify an equivalent FileActor to one that existed on the caller’s side of an asynchronous Job invocation. This involves providing slots for the metadata that might travel w/ the actor’s various supported types of @file. For example, we cannot just do:
SomeJob.perform_later(arg1, arg2, File.new('/path/to/file'))
Because we’ll get:
ActiveJob::SerializationError: Unsupported argument type: File
This also applies to Hydra::Derivatives::IoDecorator, Tempfile, etc., pretty much any IO.
Instance Attribute Summary collapse
-
#use_valkyrie ⇒ Object
Returns the value of attribute use_valkyrie.
Class Method Summary collapse
-
.create_with_varied_file_handling!(user:, file:, relation:, file_set:, use_valkyrie: nil) ⇒ JobIoWrapper
Responsible for creating a JobIoWrapper from the given parameters, with a focus on sniffing out attributes from the given :file.
Instance Method Summary collapse
-
#file ⇒ File, ...
The magic that switches once between local filepath and CarrierWave file.
- #file_actor ⇒ Object
- #file_set(use_valkyrie: nil) ⇒ Object
-
#ingest_file ⇒ Hyrax::FileMetadata, FalseClass
The created file metadata on success, false on failure.
-
#initialize(attributes = {}) ⇒ JobIoWrapper
constructor
A new instance of JobIoWrapper.
- #mime_type ⇒ Object
- #original_name ⇒ Object
- #size ⇒ Object
- #to_file_metadata ⇒ Object
Constructor Details
#initialize(attributes = {}) ⇒ JobIoWrapper
Returns a new instance of JobIoWrapper.
54 55 56 57 58 |
# File 'app/models/job_io_wrapper.rb', line 54 def initialize(attributes = {}) @use_valkyrie = attributes&.delete(:use_valkyrie) @use_valkyrie = Hyrax.config.use_valkyrie? if @use_valkyrie.nil? super(attributes) end |
Instance Attribute Details
#use_valkyrie ⇒ Object
Returns the value of attribute use_valkyrie.
28 29 30 |
# File 'app/models/job_io_wrapper.rb', line 28 def use_valkyrie @use_valkyrie end |
Class Method Details
.create_with_varied_file_handling!(user:, file:, relation:, file_set:, use_valkyrie: nil) ⇒ JobIoWrapper
Responsible for creating a JobIoWrapper from the given parameters, with a focus on sniffing out attributes from the given :file.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'app/models/job_io_wrapper.rb', line 39 def self.create_with_varied_file_handling!(user:, file:, relation:, file_set:, use_valkyrie: nil) args = { user: user, relation: relation.to_s, file_set_id: file_set.id, use_valkyrie: use_valkyrie } if file.is_a?(Hyrax::UploadedFile) args[:uploaded_file] = file args[:path] = file.uploader.path elsif file.respond_to?(:path) args[:path] = file.path args[:original_name] = file.original_filename if file.respond_to?(:original_filename) args[:original_name] ||= file.original_name if file.respond_to?(:original_name) else raise "Require Hyrax::UploadedFile or File-like object, received #{file.class} object: #{file}" end create!(args) end |
Instance Method Details
#file ⇒ File, ...
The magic that switches once between local filepath and CarrierWave file
98 99 100 |
# File 'app/models/job_io_wrapper.rb', line 98 def file @file ||= (file_from_path || file_from_uploaded_file!) end |
#file_actor ⇒ Object
80 81 82 |
# File 'app/models/job_io_wrapper.rb', line 80 def file_actor Hyrax::Actors::FileActor.new(file_set, relation.to_sym, user) end |
#file_set(use_valkyrie: nil) ⇒ Object
74 75 76 77 78 |
# File 'app/models/job_io_wrapper.rb', line 74 def file_set(use_valkyrie: nil) use_valkyrie ||= @use_valkyrie return FileSet.find(file_set_id) unless use_valkyrie Hyrax.query_service.find_by(id: Valkyrie::ID.new(file_set_id)) end |
#ingest_file ⇒ Hyrax::FileMetadata, FalseClass
Returns the created file metadata on success, false on failure.
85 86 87 |
# File 'app/models/job_io_wrapper.rb', line 85 def ingest_file file_actor.ingest_file(self) end |
#mime_type ⇒ Object
64 65 66 |
# File 'app/models/job_io_wrapper.rb', line 64 def mime_type super || extracted_mime_type end |
#original_name ⇒ Object
60 61 62 |
# File 'app/models/job_io_wrapper.rb', line 60 def original_name super || extracted_original_name end |
#size ⇒ Object
68 69 70 71 72 |
# File 'app/models/job_io_wrapper.rb', line 68 def size return file.size.to_s if file.respond_to? :size return file.stat.size.to_s if file.respond_to? :stat nil # unable to determine end |
#to_file_metadata ⇒ Object
89 90 91 92 93 94 |
# File 'app/models/job_io_wrapper.rb', line 89 def Hyrax::FileMetadata.new(label: original_name, original_filename: original_name, mime_type: mime_type, use: [Hyrax::FileMetadata::Use::ORIGINAL_FILE]) end |