-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6212 from samvera/valk-index-thumbnail
Reindex the work during derivative creation if it is the work's thumbnail
- Loading branch information
Showing
3 changed files
with
61 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,24 @@ | ||
# frozen_string_literal: true | ||
class ValkyrieCreateDerivativesJob < Hyrax::ApplicationJob | ||
queue_as Hyrax.config.ingest_queue_name | ||
def perform(_file_set_id, file_id, _filepath = nil) | ||
def perform(file_set_id, file_id, _filepath = nil) | ||
file_metadata = Hyrax.custom_queries.find_file_metadata_by(id: file_id) | ||
return if file_metadata.video? && !Hyrax.config.enable_ffmpeg | ||
# Get file into a local path. | ||
file = Hyrax.storage_adapter.find_by(id: file_metadata.file_identifier) | ||
# Call derivatives with the file_set. | ||
derivative_service = Hyrax::DerivativeService.for(file_metadata) | ||
derivative_service.create_derivatives(file.disk_path) | ||
reindex_parent(file_set_id) | ||
end | ||
|
||
private | ||
|
||
def query_service | ||
Hyrax.query_service | ||
end | ||
|
||
def storage_adapter | ||
Hyrax.storage_adapter | ||
def reindex_parent(file_set_id) | ||
file_set = Hyrax.query_service.find_by(id: file_set_id) | ||
parent = Hyrax.custom_queries.find_parent_work(resource: file_set) | ||
return unless parent.thumbnail_id == file_set.id | ||
Hyrax.logger.debug { "Reindexing #{parent.id} due to creation of thumbnail derivatives." } | ||
Hyrax.index_adapter.save(resource: parent) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'hyrax/specs/spy_listener' | ||
|
||
RSpec.describe ValkyrieCreateDerivativesJob, perform_enqueued: true do | ||
# Create a work with two files, the first will be the work's thumbnail | ||
let(:work) { FactoryBot.valkyrie_create(:hyrax_work, :with_member_file_sets, :with_thumbnail) } | ||
let(:upload_0) { FactoryBot.create(:uploaded_file, file_set_uri: file_sets[0].id, file: File.open('spec/fixtures/image.png')) } | ||
let(:upload_1) { FactoryBot.create(:uploaded_file, file_set_uri: file_sets[1].id, file: File.open('spec/fixtures/world.png')) } | ||
let(:file_id_0) { file_sets[0].file_ids.first } | ||
let(:file_id_1) { file_sets[1].file_ids.first } | ||
|
||
let(:characterizer) { double(characterize: fits_response) } | ||
let(:fits_response) { IO.read('spec/fixtures/png_fits.xml') } | ||
|
||
let(:derivatives_service) { instance_double("Hyrax::FileSetDerivativeService") } | ||
|
||
def file_sets | ||
Hyrax.custom_queries.find_child_file_sets(resource: work).to_a | ||
end | ||
|
||
before do | ||
# stub out characterization to avoid system calls. It's important some | ||
# amount of characterization happens so listeners fire. | ||
allow(Hydra::FileCharacterization).to receive(:characterize).and_return(fits_response) | ||
|
||
# This job depends on routes existing for the work. SimpleWork doesn't here, so skip it. | ||
allow(ContentUpdateEventJob).to receive(:perform_later) | ||
|
||
# Using ValkyrieIngestJob here is slow and runs the job this spec is supposed to test. | ||
# It should instead be handled by factories once real files can be attached | ||
# in the Hyrax::Work and Hyrax::FileSet factories. | ||
ValkyrieIngestJob.perform_now(upload_0) | ||
ValkyrieIngestJob.perform_now(upload_1) | ||
end | ||
|
||
describe '.perform_now' do | ||
context 'with files including the work thumbnail' do | ||
before do | ||
allow(Hyrax::DerivativeService).to receive(:for).and_return derivatives_service | ||
allow(Hyrax.index_adapter).to receive(:save).and_call_original | ||
end | ||
|
||
it 'creates derivatives and reindexes the work once' do | ||
expect(derivatives_service).to receive(:create_derivatives).twice | ||
expect(Hyrax.index_adapter).to receive(:save).with(resource: work).once | ||
described_class.perform_now(file_sets[1].id, file_id_1) | ||
described_class.perform_now(file_sets[0].id, file_id_0) | ||
end | ||
end | ||
end | ||
end |