diff --git a/app/actors/hyrax/actors/file_actor.rb b/app/actors/hyrax/actors/file_actor.rb index 516d90f996..ed95e034a1 100644 --- a/app/actors/hyrax/actors/file_actor.rb +++ b/app/actors/hyrax/actors/file_actor.rb @@ -77,7 +77,7 @@ def perform_ingest_file_through_active_fedora(io) def perform_ingest_file_through_valkyrie(io) # Skip versioning because versions will be minted by VersionCommitter as necessary during save_characterize_and_record_committer. unsaved_file_metadata = io.to_file_metadata - unsaved_file_metadata.use = relation + unsaved_file_metadata.type = [relation] begin saved_file_metadata = file_metadata_builder.create(io_wrapper: io, file_metadata: unsaved_file_metadata, file_set: file_set) rescue StandardError => e # Handle error persisting file metadata @@ -103,21 +103,26 @@ def normalize_relation_for_active_fedora(relation) return relation if relation.is_a? Symbol return relation.to_sym if relation.respond_to? :to_sym - # TODO: whereever these are set, they should use Valkyrie::Vocab::PCDMUse... making the casecmp unnecessary - return :original_file if relation.to_s.casecmp(Valkyrie::Vocab::PCDMUse.original_file.to_s) - return :extracted_file if relation.to_s.casecmp(Valkyrie::Vocab::PCDMUse.extracted_file.to_s) - return :thumbnail_file if relation.to_s.casecmp(Valkyrie::Vocab::PCDMUse.thumbnail_file.to_s) + # TODO: whereever these are set, they should use FileSet.*_use... making the casecmp unnecessary + return :original_file if relation.to_s.casecmp(Hyrax::FileSet::ORIGINAL_FILE_USE.to_s) + return :extracted_file if relation.to_s.casecmp(Hyrax::FileSet::EXTRACTED_TEXT_USE.to_s) + return :thumbnail_file if relation.to_s.casecmp(Hyrax::FileSet::THUMBNAIL_USE.to_s) :original_file end def normalize_relation_for_valkyrie(relation) # TODO: When this is fully switched to valkyrie, this should probably be removed and relation should always be passed # in as a valid URI already set to the file's use - relation = relation.to_s.to_sym - return Valkyrie::Vocab::PCDMUse.original_file if relation == :original_file - return Valkyrie::Vocab::PCDMUse.extracted_file if relation == :extracted_file - return Valkyrie::Vocab::PCDMUse.thumbnail_file if relation == :thumbnail_file - Valkyrie::Vocab::PCDMUse.original_file + case relation.to_s.to_sym + when :original_file + Hyrax::FileSet::ORIGINAL_FILE_USE + when :extracted_file + Hyrax::FileSet.EXTRACTED_TEXT_USE + when :thumbnail_file + Hyrax::FileSet::THUMBNAIL_USE + else + Hyrax::FileSet::ORIGINAL_FILE_USE + end end end end diff --git a/app/models/hyrax/file_metadata.rb b/app/models/hyrax/file_metadata.rb index ac4ec083c4..6b22d766bd 100644 --- a/app/models/hyrax/file_metadata.rb +++ b/app/models/hyrax/file_metadata.rb @@ -10,7 +10,7 @@ class FileMetadata < Valkyrie::Resource attribute :label, ::Valkyrie::Types::Set attribute :original_filename, ::Valkyrie::Types::Set attribute :mime_type, ::Valkyrie::Types::Set - attribute :use, ::Valkyrie::Types::Set # AF::File type + attribute :type, ::Valkyrie::Types::Set # AF::File type attribute :content, ::Valkyrie::Types::Set # attributes set by fits @@ -76,19 +76,19 @@ def self.for(file:) new(label: file.original_filename, original_filename: file.original_filename, mime_type: file.content_type, - use: file.try(:use) || [::Valkyrie::Vocab::PCDMUse.OriginalFile]) + type: file.try(:type) || [Hyrax::FileSet::ORIGINAL_FILE_USE]) end def original_file? - use.include?(::Valkyrie::Vocab::PCDMUse.OriginalFile) + type.include?(Hyrax::FileSet::ORIGINAL_FILE_USE) end def thumbnail_file? - use.include?(::Valkyrie::Vocab::PCDMUse.ThumbnailImage) + type.include?(Hyrax::FileSet::THUMBNAIL_USE) end def extracted_file? - use.include?(::Valkyrie::Vocab::PCDMUse.ExtractedImage) + type.include?(Hyrax::FileSet::EXTRACTED_TEXT_USE) end def title diff --git a/app/models/hyrax/file_set.rb b/app/models/hyrax/file_set.rb index 92289b1cbb..592c74a2bf 100644 --- a/app/models/hyrax/file_set.rb +++ b/app/models/hyrax/file_set.rb @@ -8,6 +8,10 @@ module Hyrax class FileSet < Hyrax::Resource include Hyrax::Schema(:core_metadata) + ORIGINAL_FILE_USE = ::Valkyrie::Vocab::PCDMUse.OriginalFile + EXTRACTED_TEXT_USE = ::Valkyrie::Vocab::PCDMUse.ExtractedText + THUMBNAIL_USE = ::Valkyrie::Vocab::PCDMUse.Thumbnail + attribute :file_ids, Valkyrie::Types::Array.of(Valkyrie::Types::ID) # id for FileMetadata resources attribute :original_file_id, Valkyrie::Types::ID # id for FileMetadata resource attribute :thumbnail_id, Valkyrie::Types::ID # id for FileMetadata resource @@ -24,5 +28,36 @@ def pcdm_object? def file_set? true end + + ## + # Gives file metadata for the file filling the http://pcdm.org/OriginalFile use + # @return [FileMetadata] the FileMetadata resource of the original file + def original_file + filter_files_by_type(Hyrax::FileSet::ORIGINAL_FILE_USE).first + end + + ## + # Gives file metadata for the file filling the http://pcdm.org/ExtractedText use + # @return [FileMetadata] the FileMetadata resource of the extracted text + def extracted_text + filter_files_by_type(Hyrax::FileSet::EXTRACTED_TEXT_USE).first + end + + ## + # Gives file metadata for the file filling the http://pcdm.org/Thumbnail use + # @return [FileMetadata] the FileMetadata resource of the thumbnail + def thumbnail + filter_files_by_type(Hyrax::FileSet::THUMBNAIL_USE).first + end + + ## + # Gives file metadata for files that have the requested RDF Type for use + # @param [RDF::URI] uri for the desired Type + # @return [Enumerable] the FileMetadata resources + # @example + # filter_files_by_type(::RDF::URI("http://pcdm.org/ExtractedText")) + def filter_files_by_type(uri) + Hyrax.query_service.custom_queries.find_many_file_metadata_by_use(resource: self, use: uri) + end end end diff --git a/app/models/job_io_wrapper.rb b/app/models/job_io_wrapper.rb index bb6986fdab..7cff0962c3 100644 --- a/app/models/job_io_wrapper.rb +++ b/app/models/job_io_wrapper.rb @@ -80,7 +80,7 @@ def to_file_metadata Hyrax::FileMetadata.new(label: original_name, original_filename: original_name, mime_type: mime_type, - use: [Valkyrie::Vocab::PCDMUse.OriginalFile]) + use: [Hyrax::FileSet::ORIGINAL_FILE_USE]) end # The magic that switches *once* between local filepath and CarrierWave file diff --git a/app/services/hyrax/custom_queries/find_file_metadata.rb b/app/services/hyrax/custom_queries/find_file_metadata.rb index c1a1037b3f..7cc55d32d4 100644 --- a/app/services/hyrax/custom_queries/find_file_metadata.rb +++ b/app/services/hyrax/custom_queries/find_file_metadata.rb @@ -53,6 +53,16 @@ def find_many_file_metadata_by_ids(ids:) results = query_service.find_many_by_ids(ids: ids) results.select { |resource| resource.is_a? Hyrax::FileMetadata } end + + # Find file metadata for files within a resource that have the requested use. + # @param use [RDF::URI] uri for the desired use Type + # @return [Array(types) { types.include?(RDF::URI.new('http://pcdm.org/use#OriginalFile')) } + af_object.association(:original_file).target = pcdm_file + when ->(types) { types.include?(RDF::URI.new('http://pcdm.org/use#ExtractedText')) } + af_object.association(:extracted_text).target = pcdm_file + when ->(types) { types.include?(RDF::URI.new('http://pcdm.org/use#Thumbnail')) } + af_object.association(:thumbnail).target = pcdm_file + else + pcdm_file + end end # Normalizes the attributes parsed from the resource @@ -158,16 +155,13 @@ def convert_file(af_object, relation) # ActiveFedora::Base Class) # @return [Hash] def normal_attributes - normalized = {} - attributes.each_pair do |attr, value| + attributes.each_with_object({}) do |(attr, value), hash| property = active_fedora_class.properties[attr.to_s] - # This handles some cases where the attributes do not directly map to an - # RDF property value - normalized[attr] = value + # This handles some cases where the attributes do not directly map to an RDF property value + hash[attr] = value next if property.nil? - normalized[attr] = Array.wrap(value) if property.multiple? + hash[attr] = Array.wrap(value) if property.multiple? end - normalized end def apply_depositor_to(af_object) diff --git a/lib/wings/hydra/pcdm/models/concerns/pcdm_valkyrie_behavior.rb b/lib/wings/hydra/pcdm/models/concerns/pcdm_valkyrie_behavior.rb index 10c9c187d8..881154104b 100644 --- a/lib/wings/hydra/pcdm/models/concerns/pcdm_valkyrie_behavior.rb +++ b/lib/wings/hydra/pcdm/models/concerns/pcdm_valkyrie_behavior.rb @@ -132,11 +132,6 @@ def in_collection_ids(valkyrie: false) in_collections(valkyrie: valkyrie).map(&:id) end - def original_file - af_object = Wings::ActiveFedoraConverter.new(resource: self).convert - af_object.original_file - end - ## # @return [Boolean] whether this instance is an audio. def audio? diff --git a/lib/wings/hydra/works/services/add_file_to_file_set.rb b/lib/wings/hydra/works/services/add_file_to_file_set.rb index bf49aef420..204b580e1b 100644 --- a/lib/wings/hydra/works/services/add_file_to_file_set.rb +++ b/lib/wings/hydra/works/services/add_file_to_file_set.rb @@ -37,9 +37,9 @@ def association_type(type) end def type_to_association_type(type) - return :original_file if type.to_s.casecmp?(Valkyrie::Vocab::PCDMUse.original_file.to_s) - return :extracted_text if type.to_s.casecmp?(Valkyrie::Vocab::PCDMUse.extracted_text.to_s) - return :thumbnail if type.to_s.casecmp?(Valkyrie::Vocab::PCDMUse.thumbnail.to_s) + return :original_file if type.to_s.casecmp?(Hyrax::FileSet::ORIGINAL_FILE_USE.to_s) + return :extracted_text if type.to_s.casecmp?(Hyrax::FileSet::EXTRACTED_TEXT_USE.to_s) + return :thumbnail if type.to_s.casecmp?(Hyrax::FileSet::THUMBNAIL_USE.to_s) end def type_to_rdf_uri(type) diff --git a/lib/wings/services/custom_queries/find_file_metadata.rb b/lib/wings/services/custom_queries/find_file_metadata.rb index 613a2a5241..b5a0d1e1f4 100644 --- a/lib/wings/services/custom_queries/find_file_metadata.rb +++ b/lib/wings/services/custom_queries/find_file_metadata.rb @@ -9,7 +9,8 @@ class FindFileMetadata def self.queries [:find_file_metadata_by, :find_file_metadata_by_alternate_identifier, - :find_many_file_metadata_by_ids] + :find_many_file_metadata_by_ids, + :find_many_file_metadata_by_use] end def initialize(query_service:) @@ -53,7 +54,8 @@ def find_file_metadata_by_alternate_identifier(alternate_identifier:, use_valkyr # Find an array of file metadata using Valkyrie IDs, and map them to Hyrax::FileMetadata maintaining order based on given ids # @param ids [Array] - # @return [Array] or empty array if there are no ids or none of the ids map to Hyrax::FileMetadata + # @param use_valkyrie [boolean] defaults to true; optionally return ActiveFedora::File objects if false + # @return [Array] or empty array if there are no ids or none of the ids map to Hyrax::FileMetadata # NOTE: Ignores non-existent ids and ids for non-file metadata resources. def find_many_file_metadata_by_ids(ids:, use_valkyrie: true) results = [] @@ -68,6 +70,20 @@ def find_many_file_metadata_by_ids(ids:, use_valkyrie: true) end results end + + ## + # Find file metadata for files within a resource that have the requested use. + # @param use [RDF::URI] uri for the desired use Type + # @param use_valkyrie [boolean] defaults to true; optionally return ActiveFedora::File objects if false + # @return [Array] or empty array if there are no files with the requested use + # @example + # Hyrax.query_service.find_file_metadata_by_use(use: ::RDF::URI("http://pcdm.org/ExtractedText")) + def find_many_file_metadata_by_use(resource:, use:, use_valkyrie: true) + pcdm_files = find_many_file_metadata_by_ids(ids: resource.file_ids, use_valkyrie: false) + pcdm_files.select! { |pcdm_file| pcdm_file.metadata_node.type.include?(use) } + return pcdm_files if use_valkyrie == false + pcdm_files.collect { |pcdm_file| Wings::FileConverterService.af_file_to_resource(af_file: pcdm_file) } + end end end end diff --git a/lib/wings/services/file_converter_service.rb b/lib/wings/services/file_converter_service.rb index d9b6c63c55..20e22cec0d 100644 --- a/lib/wings/services/file_converter_service.rb +++ b/lib/wings/services/file_converter_service.rb @@ -34,7 +34,8 @@ def base_af_file_attributes(af_file:) content: af_file.content, size: af_file.size, original_filename: [af_file.original_name], - mime_type: [af_file.mime_type] } + mime_type: [af_file.mime_type], + type: af_file.metadata_node.type.to_a } end # extracts attributes that come from the metadata_node @@ -47,7 +48,7 @@ def metadata_node_to_attributes(metadata_node:, attributes:) def valkyrie_attributes_to_af_file(attributes:, af_file:) attributes.each do |k, v| - next if [:id, :content].include? k + next if [:id, :content, :type].include? k mname = (k.to_s + '=').to_sym if af_file.respond_to? mname af_file.send(mname, v) diff --git a/lib/wings/services/file_metadata_builder.rb b/lib/wings/services/file_metadata_builder.rb index 3abfa09c5f..f8a0228564 100644 --- a/lib/wings/services/file_metadata_builder.rb +++ b/lib/wings/services/file_metadata_builder.rb @@ -17,7 +17,7 @@ def initialize(storage_adapter:, persister:) # @param file_set [Valkyrie::Resouce, Hydra::Works::FileSet] the associated FileSet # TODO: WINGS - Remove Hydra::Works::FileSet as a potential type when valkyrization is complete. # @return [Hyrax::FileMetadata] the persisted metadata file_metadata that represents the file def create(io_wrapper:, file_metadata:, file_set:) - io_wrapper = build_file(io_wrapper, file_metadata.use) + io_wrapper = build_file(io_wrapper, file_metadata.type) file_set.save unless file_set.persisted? file_metadata.id = ::Valkyrie::ID.new(assign_id) file_metadata.file_set_id = file_set.id diff --git a/lib/wings/valkyrie/storage/active_fedora.rb b/lib/wings/valkyrie/storage/active_fedora.rb index e67b166f5b..63c7d30f64 100644 --- a/lib/wings/valkyrie/storage/active_fedora.rb +++ b/lib/wings/valkyrie/storage/active_fedora.rb @@ -4,15 +4,14 @@ module Wings::Storage # Implements the DataMapper Pattern to store binary data in fedora following the ActiveFedora structures class ActiveFedora < Valkyrie::Storage::Fedora - # @param file [IO] + # @param file [Wings::FileMetadataBuilder::IoDecorator] # @param original_filename [String] - # @param resource [Valkyrie::Resource] - # @param content_type [String] content type of file (e.g. 'image/tiff') (default='application/octet-stream') - # @param resource_uri_transformer [Lambda] transforms the resource's id (e.g. 'DDS78RK') into a uri (optional) + # @param resource [Hyrax::FileMetadata] FileMetadata resource + # @param resource_uri_transformer [Proc] transforms the resource's id (e.g. 'DDS78RK') into a uri (optional) # @param extra_arguments [Hash] additional arguments which may be passed to other adapters # @return [Valkyrie::StorageAdapter::StreamFile] def upload(file:, original_filename:, resource:, resource_uri_transformer: default_resource_uri_transformer, **_extra_arguments) # rubocop:disable Lint/UnusedMethodArgument - Wings::Works::AddFileToFileSet.call(file_set: file_set(resource), file: file, type: resource.use) + Wings::Works::AddFileToFileSet.call(file_set: file_set(resource), file: file, type: resource.type) identifier = resource_uri_transformer.call(resource, base_url) find_by(id: Valkyrie::ID.new(identifier.to_s.sub(/^.+\/\//, PROTOCOL))) end diff --git a/spec/actors/hyrax/actors/file_actor_spec.rb b/spec/actors/hyrax/actors/file_actor_spec.rb index e46a93dcd2..44254680c2 100644 --- a/spec/actors/hyrax/actors/file_actor_spec.rb +++ b/spec/actors/hyrax/actors/file_actor_spec.rb @@ -131,7 +131,7 @@ class FileSetWithExtras < FileSet context 'when using valkyrie' do let(:user) { create(:user) } let(:file_set) { create(:file_set) } - let(:relation) { Valkyrie::Vocab::PCDMUse.OriginalFile } + let(:relation) { Hyrax::FileSet::ORIGINAL_FILE_USE } let(:actor) { described_class.new(file_set, relation, user, use_valkyrie: true) } let(:fixture) { fixture_file_upload('/world.png', 'image/png') } let(:huf) { Hyrax::UploadedFile.new(user: user, file: fixture) } diff --git a/spec/models/hyrax/file_metadata_spec.rb b/spec/models/hyrax/file_metadata_spec.rb index ba77527c55..d797936705 100644 --- a/spec/models/hyrax/file_metadata_spec.rb +++ b/spec/models/hyrax/file_metadata_spec.rb @@ -19,18 +19,18 @@ expect(subject.original_filename).to contain_exactly('world.png') expect(subject.mime_type).to contain_exactly('image/png') expect(subject.format_label).to contain_exactly('test_format_label') - expect(subject.use).to contain_exactly(Valkyrie::Vocab::PCDMUse.OriginalFile) + expect(subject.type).to contain_exactly(Hyrax::FileSet::ORIGINAL_FILE_USE) end describe '#original_file?' do context 'when use says file is the original file' do - before { subject.use = [Valkyrie::Vocab::PCDMUse.OriginalFile, pcdm_file_uri] } + before { subject.type = [Hyrax::FileSet::ORIGINAL_FILE_USE, pcdm_file_uri] } it 'returns true' do expect(subject).to be_original_file end end context 'when use does not say file is the original file' do - before { subject.use = [Valkyrie::Vocab::PCDMUse.ThumbnailImage, pcdm_file_uri] } + before { subject.type = [Hyrax::FileSet::THUMBNAIL_USE, pcdm_file_uri] } it 'returns false' do expect(subject).not_to be_original_file end @@ -39,13 +39,13 @@ describe '#thumbnail_file?' do context 'when use says file is the thumbnail file' do - before { subject.use = [Valkyrie::Vocab::PCDMUse.ThumbnailImage, pcdm_file_uri] } + before { subject.type = [Hyrax::FileSet::THUMBNAIL_USE, pcdm_file_uri] } it 'returns true' do expect(subject).to be_thumbnail_file end end context 'when use does not say file is the thumbnail file' do - before { subject.use = [Valkyrie::Vocab::PCDMUse.OriginalFile, pcdm_file_uri] } + before { subject.type = [Hyrax::FileSet::ORIGINAL_FILE_USE, pcdm_file_uri] } it 'returns false' do expect(subject).not_to be_thumbnail_file end @@ -54,13 +54,13 @@ describe '#extracted_file?' do context 'when use says file is the extracted file' do - before { subject.use = [Valkyrie::Vocab::PCDMUse.ExtractedImage, pcdm_file_uri] } + before { subject.type = [Hyrax::FileSet::EXTRACTED_TEXT_USE, pcdm_file_uri] } it 'returns true' do expect(subject).to be_extracted_file end end context 'when use does not say file is the extracted file' do - before { subject.use = [Valkyrie::Vocab::PCDMUse.OriginalFile, pcdm_file_uri] } + before { subject.type = [Hyrax::FileSet::ORIGINAL_FILE_USE, pcdm_file_uri] } it 'returns false' do expect(subject).not_to be_extracted_file end diff --git a/spec/services/hyrax/custom_queries/find_file_metadata_spec.rb b/spec/services/hyrax/custom_queries/find_file_metadata_spec.rb index 2751ead145..9b1bcef162 100644 --- a/spec/services/hyrax/custom_queries/find_file_metadata_spec.rb +++ b/spec/services/hyrax/custom_queries/find_file_metadata_spec.rb @@ -97,4 +97,42 @@ end end end + + describe '.find_file_metadata_by_use' do + let!(:of_file_metadata) { FactoryBot.create_using_test_adapter(:hyrax_file_metadata, type: original_file_use) } + let!(:et_file_metadata) { FactoryBot.create_using_test_adapter(:hyrax_file_metadata, type: extracted_text_use) } + let!(:th_file_metadata) { FactoryBot.create_using_test_adapter(:hyrax_file_metadata, type: thumbnail_use) } + + let(:original_file_use) { Hyrax::FileSet::ORIGINAL_FILE_USE } + let(:extracted_text_use) { Hyrax::FileSet::EXTRACTED_TEXT_USE } + let(:thumbnail_use) { Hyrax::FileSet::THUMBNAIL_USE } + + context 'when file set has files of the requested use' do + let!(:file_set) { FactoryBot.create_using_test_adapter(:hyrax_file_set, files: [of_file_metadata, et_file_metadata, th_file_metadata]) } + + it 'returns Hyrax::FileMetadata resources matching use' do + result = query_handler.find_many_file_metadata_by_use(resource: file_set, use: extracted_text_use) + expect(result.size).to eq 1 + expect(result.first).to be_a Hyrax::FileMetadata + expect(result.first.type.first).to eq extracted_text_use + end + end + + context 'when file set has no files of the requested use' do + let!(:file_set) { FactoryBot.create_using_test_adapter(:hyrax_file_set, files: [of_file_metadata, th_file_metadata]) } + + it 'result is empty' do + result = query_handler.find_many_file_metadata_by_use(resource: file_set, use: extracted_text_use) + expect(result).to be_empty + end + end + + context 'when file set has no files' do + let!(:file_set) { FactoryBot.create_using_test_adapter(:hyrax_file_set) } + it 'result is empty' do + result = query_handler.find_many_file_metadata_by_use(resource: file_set, use: original_file_use) + expect(result).to be_empty + end + end + end end diff --git a/spec/wings/hydra/works/services/add_file_to_file_set_spec.rb b/spec/wings/hydra/works/services/add_file_to_file_set_spec.rb index 0eaec18d67..1bf159969d 100644 --- a/spec/wings/hydra/works/services/add_file_to_file_set_spec.rb +++ b/spec/wings/hydra/works/services/add_file_to_file_set_spec.rb @@ -6,9 +6,9 @@ let(:af_file_set) { create(:file_set, id: 'fileset_id') } let!(:file_set) { af_file_set.valkyrie_resource } - let(:original_file_use) { Valkyrie::Vocab::PCDMUse.OriginalFile } - let(:extracted_text_use) { Valkyrie::Vocab::PCDMUse.ExtractedText } - let(:thumbnail_use) { Valkyrie::Vocab::PCDMUse.Thumbnail } + let(:original_file_use) { Hyrax::FileSet::ORIGINAL_FILE_USE } + let(:extracted_text_use) { Hyrax::FileSet::EXTRACTED_TEXT_USE } + let(:thumbnail_use) { Hyrax::FileSet::THUMBNAIL_USE } let(:pdf_filename) { 'sample-file.pdf' } let(:pdf_mimetype) { 'application/pdf' } @@ -72,15 +72,18 @@ let(:transcript_use) { Valkyrie::Vocab::PCDMUse.Transcript } let(:service_file_use) { Valkyrie::Vocab::PCDMUse.ServiceFile } - subject { described_class.call(file_set: file_set, file: text_file, type: transcript_use) } - before do - described_class.call(file_set: file_set, file: pdf_file, type: service_file_use) + subject do + updated_file_set = described_class.call(file_set: file_set, file: pdf_file, type: service_file_use) + described_class.call(file_set: updated_file_set, file: text_file, type: transcript_use) end it 'adds the given file and applies the specified RDF::URI use to it' do - pending 'fix failing spec' - # TODO: Valkyrie resource needs to support filter_files_by_type - expect(subject.filter_files_by_type(transcript_use).first.content).to start_with('some updated content') - expect(subject.filter_files_by_type(service_file_use).first.content).to start_with('%PDF-1.3') + ids = subject.file_ids + expect(ids.size).to eq 2 + expect(ids.first).to be_a Valkyrie::ID + expect(ids.first.to_s).to start_with "#{file_set.id}/files/" + + expect(Hyrax.query_service.custom_queries.find_many_file_metadata_by_use(resource: subject, use: transcript_use).first.content.first).to start_with('some updated content') + expect(Hyrax.query_service.custom_queries.find_many_file_metadata_by_use(resource: subject, use: service_file_use).first.content.first).to start_with('%PDF-1.3') end end @@ -88,17 +91,18 @@ let(:versioning) { true } subject { described_class.call(file_set: file_set, file: pdf_file, type: original_file_use, versioning: versioning) } it 'updates the file and creates a version' do + pending 'Valkyrization of versioning in Hyrax::FileMetadata' expect(subject.original_file.versions.all.count).to eq(1) expect(subject.original_file.content).to start_with('%PDF-1.3') end context 'and there are already versions' do - before do - described_class.call(file_set: file_set, file: pdf_file, type: original_file_use, versioning: versioning) + subject do + updated_file_set = described_class.call(file_set: file_set, file: pdf_file, type: original_file_use, versioning: versioning) + described_class.call(file_set: updated_file_set, file: text_file, type: original_file_use, versioning: versioning) end - subject { described_class.call(file_set: file_set, file: text_file, type: original_file_use, versioning: versioning) } it 'adds to the version history' do - pending 'Valkyrization of versioning for files' + pending 'Valkyrization of versioning in Hyrax::FileMetadata' expect(subject.original_file.versions.all.count).to eq(2) expect(subject.original_file.content).to eq("some updated content\n") end @@ -107,11 +111,12 @@ context 'when :versioning => false' do let(:versioning) { false } - before do - described_class.call(file_set: file_set, file: pdf_file, type: original_file_use, versioning: versioning) + subject do + updated_file_set = described_class.call(file_set: file_set, file: pdf_file, type: original_file_use, versioning: versioning) + described_class.call(file_set: updated_file_set, file: text_file, type: original_file_use, versioning: versioning) end - subject { described_class.call(file_set: file_set, file: text_file, type: original_file_use, versioning: versioning) } it 'skips creating versions' do + pending 'Valkyrization of versioning in Hyrax::FileMetadata' expect(subject.original_file.versions.all.count).to eq(0) expect(subject.original_file.content).to eq("some updated content\n") end diff --git a/spec/wings/services/custom_queries/find_file_metadata_spec.rb b/spec/wings/services/custom_queries/find_file_metadata_spec.rb index c6aff8110b..4098c88bff 100644 --- a/spec/wings/services/custom_queries/find_file_metadata_spec.rb +++ b/spec/wings/services/custom_queries/find_file_metadata_spec.rb @@ -22,7 +22,8 @@ it 'lists queries' do expect(described_class.queries).to eq [:find_file_metadata_by, :find_file_metadata_by_alternate_identifier, - :find_many_file_metadata_by_ids] + :find_many_file_metadata_by_ids, + :find_many_file_metadata_by_use] end end @@ -113,4 +114,73 @@ end end end + + describe '.find_file_metadata_by_use' do + let(:af_file_set) { create(:file_set, id: 'fileset_id') } + let!(:file_set) { af_file_set.valkyrie_resource } + + let(:original_file_use) { Hyrax::FileSet::ORIGINAL_FILE_USE } + let(:extracted_text_use) { Hyrax::FileSet::EXTRACTED_TEXT_USE } + let(:thumbnail_use) { Hyrax::FileSet::THUMBNAIL_USE } + + let(:pdf_filename) { 'sample-file.pdf' } + let(:pdf_mimetype) { 'application/pdf' } + let(:pdf_file) { File.open(File.join(fixture_path, pdf_filename)) } + + let(:text_filename) { 'updated-file.txt' } + let(:text_mimetype) { 'text/plain' } + let(:text_file) { File.open(File.join(fixture_path, text_filename)) } + + let(:image_filename) { 'world.png' } + let(:image_mimetype) { 'image/png' } + let(:image_file) { File.open(File.join(fixture_path, image_filename)) } + + context 'when file set has files of the requested use' do + let!(:file_set) do + file_set = af_file_set.valkyrie_resource + file_set = Wings::Works::AddFileToFileSet.call(file_set: file_set, file: pdf_file, type: original_file_use) + file_set = Wings::Works::AddFileToFileSet.call(file_set: file_set, file: text_file, type: extracted_text_use) + Wings::Works::AddFileToFileSet.call(file_set: file_set, file: image_file, type: thumbnail_use) + end + + context 'and use_valkyrie is false' do + it 'returns AF Files matching use' do + result = query_handler.find_many_file_metadata_by_use(resource: file_set, use: original_file_use, use_valkyrie: false) + expect(result.size).to eq 1 + expect(result.first).to be_a Hydra::PCDM::File + expect(result.first.content).to start_with('%PDF-1.3') + end + end + + context 'and use_valkyrie is true' do + it 'returns Hyrax::FileMetadata resources matching use' do + result = query_handler.find_many_file_metadata_by_use(resource: file_set, use: extracted_text_use, use_valkyrie: true) + expect(result.size).to eq 1 + expect(result.first).to be_a Hyrax::FileMetadata + expect(result.first.content.first).to start_with('some updated content') + expect(result.first.type).to include extracted_text_use + end + end + end + + context 'when file set has no files of the requested use' do + let!(:file_set) do + file_set = af_file_set.valkyrie_resource + file_set = Wings::Works::AddFileToFileSet.call(file_set: file_set, file: pdf_file, type: original_file_use) + Wings::Works::AddFileToFileSet.call(file_set: file_set, file: image_file, type: thumbnail_use) + end + + it 'result is empty' do + result = query_handler.find_many_file_metadata_by_use(resource: file_set, use: extracted_text_use) + expect(result).to be_empty + end + end + + context 'when file set has no files' do + it 'result is empty' do + result = query_handler.find_many_file_metadata_by_use(resource: file_set, use: original_file_use) + expect(result).to be_empty + end + end + end end diff --git a/spec/wings/services/file_converter_service_spec.rb b/spec/wings/services/file_converter_service_spec.rb index 406954cf23..44a9822fac 100644 --- a/spec/wings/services/file_converter_service_spec.rb +++ b/spec/wings/services/file_converter_service_spec.rb @@ -26,6 +26,7 @@ expect(subject.file_identifiers).to match_valkyrie_ids_with_active_fedora_ids [af_file_id] expect(subject.created_at).to eq af_file.create_date expect(subject.updated_at).to eq af_file.modified_date + expect(subject.type).to match_array af_file.metadata_node.type expect(subject.original_filename).to eq Array(plain_text_af_attrs[:original_name]) expect(subject.mime_type).to eq Array(plain_text_af_attrs[:mime_type]) expect(subject.content).to eq Array(plain_text_af_attrs[:content]) @@ -72,6 +73,7 @@ private def validate_af_file_metadata(expected_attrs) # rubocop:disable Metrics/AbcSize + expect(subject.metadata_node.type).to match_array expected_attrs[:type] expect(subject.mime_type).to eq expected_attrs[:mime_type] expect(subject.content).to eq expected_attrs[:content] expect(subject.format_label).to eq Array(expected_attrs[:format_label]) @@ -86,7 +88,11 @@ def plain_text_af_attrs mime_type: 'text/plain', content: 'some text content for af_file_to_resource test', format_label: 'Plain Text', - language: 'en' } + language: 'en', + type: [RDF::URI.new('http://pcdm.org/models#File'), + RDF::URI.new('http://fedora.info/definitions/v4/repository#Binary'), + RDF::URI.new('http://fedora.info/definitions/v4/repository#Resource'), + RDF::URI.new('http://www.w3.org/ns/ldp#NonRDFSource')] } end def plain_text_valkyrie_attrs @@ -95,6 +101,7 @@ def plain_text_valkyrie_attrs content: '

different text content for valkyrie_to_af_file test

', format_label: 'HTML Text', language: 'en', + type: [RDF::URI.new('http://pcdm.org/models#File')], created_at: Time.now.getlocal - 5.days, updated_at: Time.now.getlocal } end diff --git a/spec/wings/services/file_metadata_builder_spec.rb b/spec/wings/services/file_metadata_builder_spec.rb index 39cba9c47e..89f2d4b336 100644 --- a/spec/wings/services/file_metadata_builder_spec.rb +++ b/spec/wings/services/file_metadata_builder_spec.rb @@ -15,13 +15,13 @@ let(:file) { File.open(File.join(fixture_path, original_name)) } let(:original_name) { 'sample-file.pdf' } let(:mime_type) { 'application/pdf' } - let(:use) { Valkyrie::Vocab::PCDMUse.OriginalFile } + let(:use) { Hyrax::FileSet::ORIGINAL_FILE_USE } let(:original_file_metadata) do Hyrax::FileMetadata.new(label: original_name, original_filename: original_name, mime_type: mime_type, - use: [use]) + type: [use]) end describe '#create(io_wrapper:, file_metadata:, file_set:)' do @@ -33,7 +33,7 @@ expect(built_file_metadata.label).to contain_exactly(original_name) expect(built_file_metadata.original_filename).to contain_exactly(original_name) expect(built_file_metadata.mime_type).to contain_exactly(mime_type) - expect(built_file_metadata.use).to contain_exactly(use) + expect(built_file_metadata.type).to contain_exactly(use) end end end