From 8955b0199f44c0b1a1b68418b4a1a0f94f4e25d2 Mon Sep 17 00:00:00 2001 From: Chris Colvard Date: Fri, 21 Apr 2017 17:05:11 -0400 Subject: [PATCH] Persist active encode results in ActiveFedora::File objects with external content --- lib/hydra/derivatives.rb | 2 +- .../derivatives/processors/active_encode.rb | 4 ++- .../runners/active_encode_derivatives.rb | 2 +- .../services/null_output_file_service.rb | 7 ----- ...rsist_external_file_output_file_service.rb | 18 +++++++++++++ ..._external_file_output_file_service_spec.rb | 26 +++++++++++++++++++ 6 files changed, 49 insertions(+), 10 deletions(-) delete mode 100644 lib/hydra/derivatives/services/null_output_file_service.rb create mode 100644 lib/hydra/derivatives/services/persist_external_file_output_file_service.rb create mode 100644 spec/services/persist_external_file_output_file_service_spec.rb diff --git a/lib/hydra/derivatives.rb b/lib/hydra/derivatives.rb index c8e7e9d..62eb8c2 100644 --- a/lib/hydra/derivatives.rb +++ b/lib/hydra/derivatives.rb @@ -36,9 +36,9 @@ module Derivatives autoload :RemoteSourceFile autoload :PersistOutputFileService autoload :PersistBasicContainedOutputFileService + autoload :PersistExternalFileOutputFileService autoload :TempfileService autoload :MimeTypeService - autoload :NullOutputFileService end # Raised if the timout elapses diff --git a/lib/hydra/derivatives/processors/active_encode.rb b/lib/hydra/derivatives/processors/active_encode.rb index 031d686..8e3079f 100644 --- a/lib/hydra/derivatives/processors/active_encode.rb +++ b/lib/hydra/derivatives/processors/active_encode.rb @@ -18,7 +18,9 @@ class ActiveEncode < Processor def process encode = ::ActiveEncode::Base.create(source_path, directives) timeout ? wait_for_encode_with_timeout(encode) : wait_for_encode(encode) - # TODO: call output_file_service with the output url + encode.output.each do |output| + output_file_service.call(output, directives) + end end def wait_for_encode_with_timeout(encode) diff --git a/lib/hydra/derivatives/runners/active_encode_derivatives.rb b/lib/hydra/derivatives/runners/active_encode_derivatives.rb index 52594f3..9b6e9eb 100644 --- a/lib/hydra/derivatives/runners/active_encode_derivatives.rb +++ b/lib/hydra/derivatives/runners/active_encode_derivatives.rb @@ -20,7 +20,7 @@ def self.source_file_service # Use the output service configured for this class or default to the null output service def self.output_file_service - @output_file_service || NullOutputFileService + @output_file_service || PersistExternalFileOutputService end def self.processor_class diff --git a/lib/hydra/derivatives/services/null_output_file_service.rb b/lib/hydra/derivatives/services/null_output_file_service.rb deleted file mode 100644 index cd5f71e..0000000 --- a/lib/hydra/derivatives/services/null_output_file_service.rb +++ /dev/null @@ -1,7 +0,0 @@ -module Hydra::Derivatives - class NullOutputFileService - def self.call(_stream, _directives) - # no-op - end - end -end diff --git a/lib/hydra/derivatives/services/persist_external_file_output_file_service.rb b/lib/hydra/derivatives/services/persist_external_file_output_file_service.rb new file mode 100644 index 0000000..9a65372 --- /dev/null +++ b/lib/hydra/derivatives/services/persist_external_file_output_file_service.rb @@ -0,0 +1,18 @@ +module Hydra::Derivatives + class PersistExternalFileOutputFileService < PersistOutputFileService + # Persists a new file object that points to external content + # @param [Hash] output information about the external derivative file + # @option output [String] url the location of the external content + # @param [Hash] directives directions which can be used to determine where to persist to. + # @option directives [String] url This can determine the path of the object. + def self.call(output, directives) + external_file = ActiveFedora::File.new(directives[:url]) + # TODO: Replace the following two lines with the shorter call to #external_url once active_fedora/pull/1234 is merged + external_file.content = '' + external_file.mime_type = "message/external-body; access-type=URL; URL=\"#{output[:url]}\"" + # external_file.external_url = output[:url] + external_file.original_name = URI.parse(output[:url]).path.split('/').last + external_file.save + end + end +end diff --git a/spec/services/persist_external_file_output_file_service_spec.rb b/spec/services/persist_external_file_output_file_service_spec.rb new file mode 100644 index 0000000..a2c4395 --- /dev/null +++ b/spec/services/persist_external_file_output_file_service_spec.rb @@ -0,0 +1,26 @@ +require 'spec_helper' + +describe Hydra::Derivatives::PersistExternalFileOutputFileService do + before do + class ExternalDerivativeContainerObject < ActiveFedora::Base + has_subresource "external_derivative" + end + end + after do + Object.send(:remove_const, :ExternalDerivativeContainerObject) + end + + let(:object) { ExternalDerivativeContainerObject.create } + let(:directives) { { url: "#{object.uri}/external_derivative" } } + let(:external_url) { 'http://www.example.com/external/content' } + let(:output) { { url: external_url } } + let(:destination_name) { 'external_derivative' } + + describe '.call' do + it "persists the external file to the specified destination on the given object" do + described_class.call(output, directives) + expect(object.send(destination_name.to_sym).mime_type).to eq "message/external-body;access-type=URL;url=\"http://www.example.com/external/content\"" + expect(object.send(destination_name.to_sym).content).to eq '' + end + end +end