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.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.create_with_varied_file_handling!(user:, file:, relation:, file_set:) ⇒ 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


37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/job_io_wrapper.rb', line 37

def self.create_with_varied_file_handling!(user:, file:, relation:, file_set:)
  args = { user: user, relation: relation.to_s, file_set_id: file_set.id }
  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


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

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

#file_actorObject


71
72
73
# File 'app/models/job_io_wrapper.rb', line 71

def file_actor
  Hyrax::Actors::FileActor.new(file_set, relation.to_sym, user)
end

#file_set(use_valkyrie: Hyrax.config.query_index_from_valkyrie) ⇒ Object


66
67
68
69
# File 'app/models/job_io_wrapper.rb', line 66

def file_set(use_valkyrie: Hyrax.config.query_index_from_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:


76
77
78
# File 'app/models/job_io_wrapper.rb', line 76

def ingest_file
  file_actor.ingest_file(self)
end

#mime_typeObject


56
57
58
# File 'app/models/job_io_wrapper.rb', line 56

def mime_type
  super || extracted_mime_type
end

#original_nameObject


52
53
54
# File 'app/models/job_io_wrapper.rb', line 52

def original_name
  super || extracted_original_name
end

#sizeObject


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

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


80
81
82
83
84
85
# File 'app/models/job_io_wrapper.rb', line 80

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