-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store derivatives on local file system
rather than in Fedora. This is much faster than Fedora and avoids the problem of differentiating files when there are multiple access copies for different purposes (e.g. webm vs mp4, mp3 vs ogg) sharing a use predicate. Fixes #188
- Loading branch information
Showing
10 changed files
with
138 additions
and
17 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
25 changes: 25 additions & 0 deletions
25
curation_concerns-models/app/services/curation_concerns/derivative_path.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,25 @@ | ||
module CurationConcerns | ||
class DerivativePath | ||
# Path on file system where derivative file is stored | ||
def self.derivative_path_for_reference(object, destination_name) | ||
destination_name = destination_name.gsub(/^original_file_/, '') | ||
derivative_path(object, extension_for(destination_name), destination_name) | ||
end | ||
|
||
private_class_method | ||
|
||
def self.derivative_path(object, extension, destination_name) | ||
file_name = destination_name + extension | ||
File.join(CurationConcerns.config.derivatives_path, object.id, file_name) | ||
end | ||
|
||
def self.extension_for(destination_name) | ||
case destination_name | ||
when 'thumbnail' | ||
".#{MIME::Types.type_for('jpg').first.extensions.first}" | ||
else | ||
".#{destination_name}" | ||
end | ||
end | ||
end | ||
end |
30 changes: 30 additions & 0 deletions
30
curation_concerns-models/app/services/curation_concerns/persist_derivatives.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,30 @@ | ||
module CurationConcerns | ||
class PersistDerivatives < Hydra::Derivatives::PersistOutputFileService | ||
# Persists a derivative to the local file system. | ||
# This Service conforms to the signature of `Hydra::Derivatives::PersistOutputFileService`. | ||
# This service is an alternative to the default Hydra::Derivatives::PersistOutputFileService. | ||
# This service will always update existing and does not do versioning of persisted files. | ||
# | ||
# @param [Hydra::Works::GenericFile::Base] object the file will be added to | ||
# @param [Hydra::Derivatives::IoDecorator] file the derivative filestream | ||
# @param [String] extract file type (e.g. 'thumbnail') from Hydra::Derivatives created destination_name | ||
# | ||
def self.call(object, file, destination_name) | ||
output_file(object, destination_name) do |output| | ||
while buffer = file.read(4096) | ||
output.write buffer | ||
end | ||
end | ||
end | ||
|
||
# Open the output file to write and yield the block to the | ||
# file. It will make the directories in the path if | ||
# necessary. | ||
def self.output_file(object, destination_name, &blk) | ||
name = DerivativePath.derivative_path_for_reference(object, destination_name) | ||
output_file_dir = File.dirname(name) | ||
FileUtils.mkdir_p(output_file_dir) unless File.directory?(output_file_dir) | ||
File.open(name, 'wb', &blk) | ||
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require 'spec_helper' | ||
|
||
describe CurationConcerns::DerivativePath do | ||
before do | ||
allow(CurationConcerns.config).to receive(:derivatives_path).and_return('tmp') | ||
end | ||
|
||
describe '.derivative_path_for_reference' do | ||
subject { described_class.derivative_path_for_reference(object, destination_name) } | ||
|
||
let(:object) { double(id: '123') } | ||
let(:destination_name) { 'thumbnail' } | ||
|
||
it { is_expected.to eq 'tmp/123/thumbnail.jpeg' } | ||
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,24 @@ | ||
require 'spec_helper' | ||
|
||
describe CurationConcerns::PersistDerivatives do | ||
before do | ||
allow(CurationConcerns.config).to receive(:derivatives_path).and_return('tmp') | ||
end | ||
|
||
describe '.output_file' do | ||
subject { described_class.output_file(object, destination_name, &block) } | ||
|
||
let(:object) { double(id: '123') } | ||
let(:destination_name) { 'thumbnail' } | ||
|
||
let(:block) { lambda { true } } | ||
|
||
it 'yields to the file' do | ||
expect(FileUtils).to receive(:mkdir_p).with('tmp/123') | ||
expect(File).to receive(:open).with('tmp/123/thumbnail.jpeg', 'wb') do |*_, &blk| | ||
expect(blk).to be(block) | ||
end | ||
subject | ||
end | ||
end | ||
end |