-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(incomplete) refactors Processors to use services for retrieving and …
…persisting files
- Loading branch information
1 parent
d116bfa
commit 946f581
Showing
17 changed files
with
240 additions
and
32 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
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
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
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
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
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
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
29 changes: 29 additions & 0 deletions
29
lib/hydra/derivatives/services/persist_indirectly_contained_file.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,29 @@ | ||
module Hydra::Derivatives | ||
# This Service is an implementation of the Hydra::Derivatives::PeristOutputFileService | ||
# It supports indirectly contained files, which is the behavior associated with Fedora 3 file datastreams that were migrated to Fedora 4 | ||
# and, at the time that this class was authored, corresponds to the behavior of ActiveFedora::Base.attach_file and ActiveFedora::Base.attached_files | ||
class PersistIndirectlyContainedOutputFile < PersistOutputFileService | ||
|
||
# This method conforms to the signature of the .call method on Hydra::Derivatives::PeristOutputFileService | ||
# * Persists the file within the object at destination_name | ||
# | ||
# NOTE: Uses indirect containment. If you want to use direct containment (ie. with PCDM) you must use a different service (ie. Hydra::Works::AddFileToGenericFile Service) | ||
# | ||
|
||
def self.call(object, file, destination_name, opts={}) | ||
# first, check for a defined file | ||
# if object.attached_files[destination_name] | ||
# output_file = object.attached_files[destination_name] | ||
# output_file.content = file | ||
# else | ||
# output_file = ActiveFedora::File.new("#{object.uri}/#{destination_name}").tap do |file| | ||
# object.attach_file(file, destination_name) | ||
# end | ||
# end | ||
# output_file.mime_type = opts[:mime_type] if opts[:mime_type] | ||
|
||
object.add_file(file, path: destination_name, mime_type: opts[:mime_type]) | ||
object.save | ||
end | ||
end | ||
end |
10 changes: 10 additions & 0 deletions
10
lib/hydra/derivatives/services/persist_output_file_service.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,10 @@ | ||
module Hydra::Derivatives | ||
class PersistOutputFileService | ||
|
||
# Persists the file within the object at destination_name. Uses indirect containment. | ||
# If you want to use direct containment (ie. with PCDM) you must use a different service (ie. Hydra::Works::AddFileToGenericFile Service) | ||
def self.call(object, file, destination_name, opts={}) | ||
raise NotImplementedError, "PersistOutputFileService is an abstract class. Implement `call' on #{self.class.name}" | ||
end | ||
end | ||
end |
8 changes: 8 additions & 0 deletions
8
lib/hydra/derivatives/services/retrieve_source_file_service.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,8 @@ | ||
module Hydra::Derivatives | ||
class RetrieveSourceFileService | ||
|
||
def self.call(object, source_name) | ||
object.send(source_name) | ||
end | ||
end | ||
end |
File renamed without changes.
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
44 changes: 44 additions & 0 deletions
44
spec/services/persist_indirectly_contained_output_file_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,44 @@ | ||
require 'spec_helper' | ||
require 'hydra/works' | ||
|
||
describe Hydra::Derivatives::PersistIndirectlyContainedOutputFile do | ||
|
||
before(:all) do | ||
class IndirectContainerObject < ActiveFedora::Base | ||
contains "the_derivative_name" | ||
end | ||
|
||
# This uses directly_contains (inherited from Hydra::PCDM::ObjectBehavior) | ||
class DirectContainerObject < Hydra::Works::GenericFile::Base | ||
end | ||
# If you manually built DirectContainerObject, it would look like this: | ||
# class DirectContainerObject < ActiveFedora::Base | ||
# | ||
# directly_contains :files, has_member_relation: RDFVocabularies::PCDMTerms.hasFile, | ||
# class_name: "Hydra::PCDM::File" | ||
# | ||
# def original_file | ||
# file_of_type(::RDF::URI("http://pcdm.org/OriginalFile")) | ||
# end | ||
# | ||
# def thumbnail | ||
# file_of_type(::RDF::URI("http://pcdm.org/ThumbnailImage")) | ||
# end | ||
# end | ||
end | ||
|
||
let(:object) { IndirectContainerObject.new } | ||
let(:file_path) { File.join(fixture_path, 'test.tif') } | ||
let(:file) { File.new(file_path)} | ||
let(:destination_name) { 'the_derivative_name' } | ||
|
||
context "when file is indirectly contained (default assumption)" do # alas, we have to support this as the default because all legacy code (and fedora 3 systems) created indirectly contained files | ||
let(:object) { IndirectContainerObject.new } | ||
it "persists the file to the specified destination on the given object" do | ||
described_class.call(object, "fake file content", destination_name) | ||
expect(object.send(destination_name.to_sym).content).to eq("fake file content") | ||
expect(object.send(destination_name.to_sym).content_changed?).to eq false | ||
end | ||
end | ||
|
||
end |
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,61 @@ | ||
require 'spec_helper' | ||
require 'hydra/works' | ||
|
||
describe Hydra::Derivatives::RetrieveSourceFileService do | ||
|
||
before(:all) do | ||
class IndirectContainerObject < ActiveFedora::Base | ||
contains "the_source_name" | ||
end | ||
|
||
# This uses directly_contains (inherited from Hydra::PCDM::ObjectBehavior) | ||
class DirectContainerObject < Hydra::Works::GenericFile::Base | ||
end | ||
# If you manually built DirectContainerObject, it would look like this: | ||
# class DirectContainerObject < ActiveFedora::Base | ||
# | ||
# directly_contains :files, has_member_relation: RDFVocabularies::PCDMTerms.hasFile, | ||
# class_name: "Hydra::PCDM::File" | ||
# | ||
# def original_file | ||
# file_of_type(::RDF::URI("http://pcdm.org/OriginalFile")) | ||
# end | ||
# | ||
# def thumbnail | ||
# file_of_type(::RDF::URI("http://pcdm.org/ThumbnailImage")) | ||
# end | ||
# end | ||
end | ||
|
||
let(:object) { IndirectContainerObject.new } | ||
let(:file_path) { File.join(fixture_path, 'test.pdf') } | ||
let(:file) { File.new(file_path)} | ||
let(:type_uri) { ::RDF::URI("http://sample.org/SourceFile") } | ||
|
||
let(:source_name) { 'the_source_name' } | ||
|
||
context "when file is indirectly contained (default assumption)" do # alas, we have to support this as the default because all legacy code (and fedora 3 systems) created indirectly contained files | ||
let(:object) { IndirectContainerObject.new } | ||
before do | ||
# attaches the file as an indirectly contained object | ||
object.the_source_name.content = "fake file content" | ||
end | ||
it "persists the file to the specified destination on the given object" do | ||
described_class.call(object, source_name) | ||
expect(object.send(source_name).content).to eq("fake file content") | ||
end | ||
end | ||
|
||
context "when file is directly contained" do # direct containers are more efficient, but most legacy code will have indirect containers | ||
let(:object) { DirectContainerObject.create } | ||
before do | ||
Hydra::Works::AddFileToGenericFile.call(object, file_path, type_uri) # attaches the file as a directly contained object | ||
end | ||
it "retrieves the file from the specified location on the given object" do | ||
expect(object.file_of_type(type_uri).content).to start_with("%PDF-1.4") | ||
end | ||
end | ||
|
||
|
||
|
||
end |
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
Oops, something went wrong.