Skip to content

Commit

Permalink
better handling for error cases in FileMetadata.find
Browse files Browse the repository at this point in the history
the base case originally tested relied on a bug in
Valkyrie (samvera/valkyrie#905).

this reimplements with a more serious error handling approach.
  • Loading branch information
tamsin johnson committed Jun 16, 2022
1 parent f6ef1e3 commit fb8c75d
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
18 changes: 10 additions & 8 deletions app/models/hyrax/file_metadata.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
34 changes: 33 additions & 1 deletion spec/models/hyrax/file_metadata_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit fb8c75d

Please sign in to comment.