-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create
FileSetDescription
to find characterization for FileSets
Valkyrie-based FileSet objects don't have direct access to characterization information. In ActiveFedora, we used a `#characterization_proxy` to provide that information. Here, we provide a small service that can find the "primary" file (`FileMetadata`) for a given FileSet, and ask it about its description.
- Loading branch information
Tom Johnson
committed
Jan 25, 2020
1 parent
b02489e
commit 49b4584
Showing
7 changed files
with
109 additions
and
5 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
42 changes: 42 additions & 0 deletions
42
app/services/hyrax/characterization/file_set_description.rb
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,42 @@ | ||
# frozen_string_literal: true | ||
|
||
module Hyrax | ||
module Characterization | ||
## | ||
# @api public | ||
class FileSetDescription | ||
include Hydra::Works::MimeTypes | ||
|
||
## | ||
# @!attribute [rw] file_set | ||
# @return [Hyrax::FileSet] | ||
attr_accessor :file_set | ||
|
||
delegate :mime_type, to: :primary_file | ||
|
||
## | ||
# @param [Hyrax::FileSet] file_set | ||
# @param [Symbol] primary_file a symbol mapping to the file_set member | ||
# used for characterization | ||
def initialize(file_set:, primary_file: :original_file) | ||
self.file_set = file_set | ||
@primary_file = primary_file | ||
end | ||
|
||
## | ||
# @api public | ||
# @return [Hyrax::FileMetadata] the member file to use for characterization | ||
def primary_file | ||
files.find { |f| f.used_for?(@primary_file) } || Hyrax::FileMetadata.new | ||
end | ||
|
||
private | ||
|
||
## | ||
# @api private | ||
def files | ||
@__files__ ||= Hyrax.query_service.custom_queries.find_files(file_set: file_set) | ||
end | ||
end | ||
end | ||
end |
Empty file.
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
37 changes: 37 additions & 0 deletions
37
spec/services/hyrax/characterization/file_set_description_spec.rb
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,37 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe Hyrax::Characterization::FileSetDescription, valkyrie_adapter: :test_adapter do | ||
subject(:description) { described_class.new(file_set: file_set) } | ||
|
||
let(:ctype) { 'image/png' } | ||
let(:file) { Rack::Test::UploadedFile.new('spec/fixtures/world.png', ctype) } | ||
let(:file_set) { FactoryBot.valkyrie_create(:hyrax_file_set, file_ids: file_ids) } | ||
let(:file_ids) { [] } | ||
|
||
describe '#mime_type' do | ||
context 'before the file set is saved' do | ||
let(:file_set) { FactoryBot.build(:hyrax_file_set) } | ||
|
||
it 'has a generic MIME type' do | ||
expect(description.mime_type).to eq 'application/octet-stream' | ||
end | ||
end | ||
|
||
context 'when it has no files' do | ||
it 'has a generic MIME type' do | ||
expect(description.mime_type).to eq 'application/octet-stream' | ||
end | ||
end | ||
|
||
context 'when it has an original file' do | ||
let(:file_ids) { [original_file.id] } | ||
let(:original_file) { Hyrax.persister.save(resource: Hyrax::FileMetadata.for(file: file)) } | ||
|
||
it { is_expected.to be_image } | ||
|
||
it 'has a mime type from a file' do | ||
expect(description.mime_type).to eq ctype | ||
end | ||
end | ||
end | ||
end |
Empty file.
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