From 2d17b97ec37857404c2f47787d52e87c3686c361 Mon Sep 17 00:00:00 2001 From: tamsin johnson Date: Thu, 16 Jun 2022 13:48:43 -0700 Subject: [PATCH] better handling for error cases in FileMetadata.find the base case originally tested relied on a bug in Valkyrie (https://github.com/samvera/valkyrie/issues/905). this reimplements with a more serious error handling approach. --- app/models/hyrax/file_metadata.rb | 18 +++++++------ spec/models/hyrax/file_metadata_spec.rb | 36 ++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/app/models/hyrax/file_metadata.rb b/app/models/hyrax/file_metadata.rb index 28bb74637d..94176c20af 100644 --- a/app/models/hyrax/file_metadata.rb +++ b/app/models/hyrax/file_metadata.rb @@ -155,15 +155,17 @@ def content '' end + ## + # @return [Valkyrie::StorageAdapter::File] + # + # @raise [Valkyrie::StorageAdapter::AdapterNotFoundError] if no adapter + # could be found matching the file_identifier's scheme + # @raise [Valkyrie::StorageAdapter::FileNotFound] when the file can't + # be found in the registered adapter def file - adapter = - begin - Valkyrie::StorageAdapter.adapter_for(id: file_identifier) - rescue Valkyrie::StorageAdapter::AdapterNotFoundError => _err - Hyrax.storage_adapter - end - - adapter.find_by(id: file_identifier) + Valkyrie::StorageAdapter + .adapter_for(id: file_identifier) + .find_by(id: file_identifier) end end end diff --git a/spec/models/hyrax/file_metadata_spec.rb b/spec/models/hyrax/file_metadata_spec.rb index cfb76de37d..63e943b497 100644 --- a/spec/models/hyrax/file_metadata_spec.rb +++ b/spec/models/hyrax/file_metadata_spec.rb @@ -117,7 +117,41 @@ describe '#file' do it 'returns file from storage adapter' do - expect(subject.file).to be_a Valkyrie::StorageAdapter::StreamFile + expect(file_metadata.file).to be_a Valkyrie::StorageAdapter::StreamFile + end + + context 'when file_identifier is saved with storage_adapter' do + let(:file_metadata) { described_class.new(file_identifier: stored.id) } + let(:file_set) { FactoryBot.valkyrie_create(:hyrax_file_set) } + + let(:stored) do + Hyrax.storage_adapter.upload(resource: file_set, + file: file, + original_filename: file.original_filename) + end + + it 'gives the exact file' do + require 'pry'; binding.pry + + expect(file_metadata.file.read).to eq stored.read + end + end + + context 'when file_identifier is saved with an arbitrary adapter' do + let(:file_metadata) { described_class.new(file_identifier: stored.id) } + let(:file_set) { FactoryBot.valkyrie_create(:hyrax_file_set) } + + let(:stored) do + Valkyrie::StorageAdapter + .find(:test_disk) + .upload(resource: file_set, + file: file, + original_filename: file.original_filename) + end + + it 'gives the exact file' do + expect(file_metadata.file.read).to eq stored.read + end end end