Class: JobIoWrapper

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
app/models/job_io_wrapper.rb

Overview

Note:

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

Class Method Summary collapse

Instance Method Summary collapse

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_valkyrieObject

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.

Parameters:

  • user (User)
    • The user requesting to create this instance

  • file (#path, Hyrax::UploadedFile)
    • The file that is to be uploaded

  • relation (String)
  • file_set (FileSet)
    • The associated file set

Returns:

Raises:

  • ActiveRecord::RecordInvalid - if the instance is not valid



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

#fileFile, ...

The magic that switches once between local filepath and CarrierWave file

Returns:

  • (File, StringIO, #read)

    File-like object ready to #read



98
99
100
# File 'app/models/job_io_wrapper.rb', line 98

def file
  @file ||= (file_from_path || file_from_uploaded_file!)
end

#file_actorObject



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_fileHyrax::FileMetadata, FalseClass

Returns the created file metadata on success, false on failure.

Returns:



85
86
87
# File 'app/models/job_io_wrapper.rb', line 85

def ingest_file
  file_actor.ingest_file(self)
end

#mime_typeObject



64
65
66
# File 'app/models/job_io_wrapper.rb', line 64

def mime_type
  super || extracted_mime_type
end

#original_nameObject



60
61
62
# File 'app/models/job_io_wrapper.rb', line 60

def original_name
  super || extracted_original_name
end

#sizeObject



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_metadataObject



89
90
91
92
93
94
# File 'app/models/job_io_wrapper.rb', line 89

def 
  Hyrax::.new(label: original_name,
                          original_filename: original_name,
                          mime_type: mime_type,
                          use: [Hyrax::::Use::ORIGINAL_FILE])
end