Class: ValkyrieIngestJob

Inherits:
Hyrax::ApplicationJob show all
Defined in:
app/jobs/valkyrie_ingest_job.rb

Overview

Ingests a Hyrax::UploadedFile as file member of a Hyrax::FileSet.

The Hyrax::UploadedFile is passed into #perform, and should have a Hyrax::UploadedFile#file_set_uri identifying an existing Hyrax::FileSet.

Instance Method Summary collapse

Instance Method Details

#add_file_to_file_set(file_set:, file_metadata:, user:) ⇒ Hyrax::FileSet

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns updated file set.

Parameters:

  • file_set (Hyrax::FileSet)

    the file set to add to

  • file_metadata (Hyrax::FileMetadata)

    the metadata object representing the file to add

  • user (::User)

    the user performing the add

Returns:


48
49
50
51
52
53
54
# File 'app/jobs/valkyrie_ingest_job.rb', line 48

def add_file_to_file_set(file_set:, file_metadata:, user:)
  file_set.file_ids << .id
  set_file_use_ids(file_set, )

  Hyrax.persister.save(resource: file_set)
  Hyrax.publisher.publish('object.membership.updated', object: file_set, user: user)
end

#find_or_create_metadata(id:, file:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


99
100
101
102
103
104
105
106
# File 'app/jobs/valkyrie_ingest_job.rb', line 99

def (id:, file:)
  Hyrax.custom_queries.(id: id)
rescue Valkyrie::Persistence::ObjectNotFoundError => e
  Hyrax.logger.warn "Failed to find existing metadata for #{id}:"
  Hyrax.logger.warn e.message
  Hyrax.logger.warn "Creating Hyrax::FileMetadata now"
  Hyrax::FileMetadata.for(file: file)
end

#ingest(file:, pcdm_use:) ⇒ void

This method returns an undefined value.


26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/jobs/valkyrie_ingest_job.rb', line 26

def ingest(file:, pcdm_use:)
  file_set_uri = Valkyrie::ID.new(file.file_set_uri)
  file_set = Hyrax.query_service.find_by(id: file_set_uri)

   = upload_file(file: file, file_set: file_set, pcdm_use: pcdm_use)

  add_file_to_file_set(file_set: file_set,
                       file_metadata: ,
                       user: file.user)

  ValkyrieCreateDerivativesJob.perform_later(file_set.id.to_s, .id.to_s)
end

#perform(file, pcdm_use: Hyrax::FileMetadata::Use::ORIGINAL_FILE) ⇒ Object

Parameters:

  • file (Hyrax::UploadedFile)
  • pcdm_use (RDF::URI) (defaults to: Hyrax::FileMetadata::Use::ORIGINAL_FILE)

    is the use/type to apply to the created FileMetadata

See Also:


15
16
17
# File 'app/jobs/valkyrie_ingest_job.rb', line 15

def perform(file, pcdm_use: Hyrax::FileMetadata::Use::ORIGINAL_FILE)
  ingest(file: file, pcdm_use: pcdm_use)
end

#set_file_use_ids(file_set, file_metadata) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.


110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/jobs/valkyrie_ingest_job.rb', line 110

def set_file_use_ids(file_set, )
  .type.each do |type|
    case type
    when Hyrax::FileMetadata::Use::ORIGINAL_FILE
      file_set.original_file_id = .id
    when Hyrax::FileMetadata::Use::THUMBNAIL
      file_set.thumbnail_id = .id
    when Hyrax::FileMetadata::Use::EXTRACTED_TEXT
      file_set.extracted_text_id = .id
    else
      Rails.logger.warn "Unknown file use #{.type} specified for #{.file_identifier}"
    end
  end
end

#upload_file(file:, file_set:, pcdm_use:) ⇒ Hyrax::FileMetadata

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns the metadata representing the uploaded file.

Parameters:

Returns:


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/jobs/valkyrie_ingest_job.rb', line 64

def upload_file(file:, file_set:, pcdm_use:) # rubocop:disable Metrics/MethodLength
  carrier_wave_sanitized_file = file.uploader.file
  # Pull file, since carrierwave files don't respond to a proper IO #read. See
  # https://github.com/carrierwaveuploader/carrierwave/issues/1959
  file_io = carrier_wave_sanitized_file.to_file
  uploaded = Hyrax.storage_adapter
                  .upload(resource: file_set,
                          file: file_io,
                          original_filename: carrier_wave_sanitized_file.original_filename)

   = (id: uploaded.id, file: carrier_wave_sanitized_file)

  .type << pcdm_use
  .file_set_id = file.file_set_uri
  .file_identifier = uploaded.id
  .size = uploaded.size

   = Hyrax.persister.save(resource: )
  Hyrax.publisher.publish("object.file.uploaded", metadata: )
  file_io.close

  if pcdm_use == Hyrax::FileMetadata::Use::ORIGINAL_FILE
    # Set file set label.
    reset_title = file_set.title.first == file_set.label
    # set title to label if that's how it was before this characterization
    file_set.title = .original_filename if reset_title
    # always set the label to the original_name
    file_set.label = .original_filename
  end

  
end