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..74543b4084 100644 --- a/spec/models/hyrax/file_metadata_spec.rb +++ b/spec/models/hyrax/file_metadata_spec.rb @@ -117,7 +117,39 @@ 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 + 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