Skip to content

Commit

Permalink
Merge branch 'main' into GH-6207
Browse files Browse the repository at this point in the history
  • Loading branch information
dunn committed Aug 28, 2023
2 parents b165b7d + 5ba61e4 commit 320f4f0
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def send_file_contents_valkyrie(file_set)
file.rewind
send_data send_range_valkyrie(file: file), data_options(file_metadata)
else
send_file file.disk_path
send_file file.disk_path, data_options(file_metadata).except(:status)
end
end

Expand Down
18 changes: 16 additions & 2 deletions app/controllers/hyrax/single_use_links_viewer_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ def show

private

def send_content
if @asset.is_a?(Hyrax::FileSet)
send_file_contents_valkyrie(@asset)
else
super
end
end

def curation_concern
response, _document_list = search_service.search_results
response.documents.first
Expand All @@ -52,6 +60,12 @@ def content_options
end
end

def data_options(file_metadata)
super.tap do |options|
options[:disposition] = 'attachment' if action_name == 'download'
end
end

# This is called in a before filter. It causes @asset to be set.
def authorize_download!
authorize! :read, asset
Expand All @@ -66,7 +80,7 @@ def not_found_exception
end

def asset
@asset ||= Hyrax.query_service.find_by_alternate_identifier(alternate_identifier: single_use_link.item_id, use_valkyrie: false)
@asset ||= Hyrax.query_service.find_by(id: single_use_link.item_id)
end

def current_ability
Expand Down Expand Up @@ -94,7 +108,7 @@ def initialize(user, single_use_link)
return unless single_use_link

@single_use_link = single_use_link
can :read, [ActiveFedora::Base, ::SolrDocument] do |obj|
can :read, [ActiveFedora::Base, ::SolrDocument, Hyrax::Resource] do |obj|
single_use_link.valid? && single_use_link.item_id == obj.id && single_use_link.destroy!
end
end
Expand Down
4 changes: 1 addition & 3 deletions app/jobs/valkyrie_ingest_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,12 @@ def perform(file, pcdm_use: Hyrax::FileMetadata::Use::ORIGINAL_FILE)
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)
updated_metadata = upload_file(
upload_file(
file: file,
file_set: file_set,
pcdm_use: pcdm_use,
user: file.user
)

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

##
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ def self.run(metadata:, file:, user: ::User.system_user, **options)
new(metadata: metadata, file: file, **options).characterize
saved = Hyrax.persister.save(resource: metadata)
Hyrax.publisher.publish('file.metadata.updated', metadata: saved, user: user)

Hyrax.publisher.publish('file.characterized',
file_set: Hyrax.query_service.find_by(id: saved.file_set_id),
file_id: saved.id.to_s,
path_hint: saved.file_identifier.to_s)
end

##
Expand Down
16 changes: 13 additions & 3 deletions app/services/hyrax/listeners/file_metadata_listener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,16 @@ class FileMetadataListener
# @param [Dry::Events::Event] event
# @return [void]
def on_file_characterized(event)
CreateDerivativesJob
.perform_later(event[:file_set], event[:file_id], event[:path_hint])
file_set = event[:file_set]

case file_set
when ActiveFedora::Base # ActiveFedora
CreateDerivativesJob
.perform_later(file_set, event[:file_id], event[:path_hint])
else
ValkyrieCreateDerivativesJob
.perform_later(file_set.id.to_s, event[:file_id])
end
end

##
Expand All @@ -39,7 +47,9 @@ def on_file_metadata_updated(event)
# @param [Dry::Events::Event] event
# @return [void]
def on_file_uploaded(event)
# Run characterization
# Run characterization for original file only
return unless event[:metadata]&.original_file?

Hyrax.config
.characterization_service
.run(metadata: event[:metadata], file: event[:metadata].file)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
routes { Hyrax::Engine.routes }
let(:user) { build(:user) }
let(:file) do
create(:file_set, label: 'world.png', user: user)
Hyrax.query_service.find_by(id: work.member_ids.first)
end
let(:file_metadata) { Hyrax.custom_queries.find_file_metadata_by(id: file.original_file_id) }
let(:disk_file) { Hyrax.storage_adapter.find_by(id: file_metadata.file_identifier) }
let(:work) do
FactoryBot.valkyrie_create(:hyrax_work, uploaded_files: [FactoryBot.create(:uploaded_file)])
end

describe "retrieval links" do
Expand All @@ -12,17 +17,16 @@
end

let :download_link do
Hydra::Works::AddFileToFileSet.call(file, File.open(fixture_path + '/world.png'), :original_file)
SingleUseLink.create item_id: file.id, path: Hyrax::Engine.routes.url_helpers.download_path(id: file, locale: 'en')
SingleUseLink.create item_id: file.id, path: Hyrax::Engine.routes.url_helpers.download_path(id: file.id.to_s, locale: 'en')
end

let(:show_link_hash) { show_link.download_key }
let(:download_link_hash) { download_link.download_key }

describe "GET 'download'" do
let(:expected_content) { Hyrax.query_service.find_by_alternate_identifier(alternate_identifier: file.id, use_valkyrie: false).original_file.content }
let(:expected_content) { disk_file.read }
it "downloads the file and deletes the link from the database" do
expect(controller).to receive(:send_file_headers!).with({ filename: 'world.png', disposition: 'attachment', type: 'image/png' })
expect(controller).to receive(:send_file_headers!).with({ filename: 'image.jp2', disposition: 'attachment', type: file_metadata.mime_type })
get :download, params: { id: download_link_hash }
expect(response.body).to eq expected_content
expect(response).to be_successful
Expand Down
10 changes: 10 additions & 0 deletions spec/factories/hyrax_work.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
members { nil }
visibility_setting { nil }
with_index { true }
uploaded_files { [] }
end

after(:build) do |work, evaluator|
Expand All @@ -41,6 +42,15 @@
.new(resource: work)
.assign_access_for(visibility: evaluator.visibility_setting)
end
if evaluator.uploaded_files.present?
Hyrax::WorkUploadsHandler.new(work: work).add(files: evaluator.uploaded_files).attach
evaluator.uploaded_files.each do |file|
allow(Hyrax.config.characterization_service).to receive(:run).and_return(true)
# I don't love this - we might want to just run background jobs so
# this is more real, but we'd have to stub some things.
ValkyrieIngestJob.perform_now(file)
end
end

work.permission_manager.edit_groups = evaluator.edit_groups
work.permission_manager.edit_users = evaluator.edit_users
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@
original_filename: 'test_world.png')
end

before { Hyrax.publisher.subscribe(listener) }
before do
Hyrax.publisher.subscribe(listener)
metadata.file_set_id = file_set.id
end
after { Hyrax.publisher.unsubscribe(listener) }

describe '#run' do
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def ci_build?
FactoryBot.register_strategy(:json, JsonStrategy)
FactoryBot.definition_file_paths = [File.expand_path("../factories", __FILE__)]
FactoryBot.find_definitions
require 'rspec/mocks'

require 'shoulda/matchers'
require 'shoulda/callback/matchers'
Expand Down Expand Up @@ -142,6 +143,7 @@ def clean_active_fedora_repository
config.use_transactional_fixtures = false

config.before :suite do
FactoryBot::SyntaxRunner.include RSpec::Mocks::ExampleMethods
Hyrax::RedisEventStore.instance.then(&:flushdb)
DatabaseCleaner.clean_with(:truncation)
# Noid minting causes extra LDP requests which slow the test suite.
Expand Down

0 comments on commit 320f4f0

Please sign in to comment.