Skip to content

Commit

Permalink
Delete derivatives when FileSet is destroyed.
Browse files Browse the repository at this point in the history
  • Loading branch information
Trey Terrell committed Jan 11, 2016
1 parent 5527443 commit a937ad2
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Derivatives
Hydra::Derivatives.source_file_service = CurationConcerns::LocalFileService
Hydra::Derivatives.output_file_service = CurationConcerns::PersistDerivatives
Hydra::Derivatives::FullTextExtract.output_file_service = CurationConcerns::PersistDirectlyContainedOutputFileService
after_destroy :cleanup_derivatives
end

# This completely overrides the version in Hydra::Works so that we
Expand Down Expand Up @@ -46,9 +47,19 @@ def create_derivatives(filename)
# The destination_name parameter has to match up with the file parameter
# passed to the DownloadsController
def derivative_url(destination_name)
path = DerivativePath.derivative_path_for_reference(self, destination_name)
path = derivative_path_factory.derivative_path_for_reference(self, destination_name)
URI("file://#{path}").to_s
end

def cleanup_derivatives
derivative_path_factory.derivatives_for_reference(self).each do |path|
FileUtils.rm_f(path)
end
end

def derivative_path_factory
DerivativePath
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,33 @@ def derivative_path_for_reference(object, destination_name)
derivative_path(object, extension_for(destination_name), destination_name)
end

# @return [Array<String>] Array of paths to derivatives for this object.
def derivatives_for_reference(object)
Dir.glob(root_path(object).join("*")).select do |path|
path.start_with?(path_prefix(object).to_s)
end
end

private

# @param [#id] object Object whose ID is used to generate root path
# @return [String] Returns the root path where derivatives will be generated into.
def root_path(object)
Pathname.new(derivative_path(object, "", "")).dirname
end

# @return <Pathname> Full prefix of the path for object.
def path_prefix(object)
Pathname.new(CurationConcerns.config.derivatives_path).join(pair_path(object.id))
end

def derivative_path(object, extension, destination_name)
file_name = destination_name + extension
File.join(CurationConcerns.config.derivatives_path, pair_path(object.id, file_name))
"#{path_prefix(object)}-#{file_name}"
end

def pair_path(id, file_name)
pair = id.split('').each_slice(2).map(&:join).join('/')
"#{pair}-#{file_name}"
def pair_path(id)
id.split('').each_slice(2).map(&:join).join('/')
end

def extension_for(destination_name)
Expand Down
14 changes: 14 additions & 0 deletions spec/models/curation_concerns/file_set/derivatives_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,18 @@
end
end
end

describe "cleanup" do
let(:mime_type) { 'image/jpg' }
it "cleans up all created derivatives" do
allow(CurationConcerns::DerivativePath).to receive(:derivatives_for_reference).with(file_set).and_return([
"tmp/1/2.jpg"
])
allow(FileUtils).to receive(:rm_f).with("tmp/1/2.jpg")

file_set.destroy

expect(FileUtils).to have_received(:rm_f).with("tmp/1/2.jpg")
end
end
end
24 changes: 24 additions & 0 deletions spec/services/derivative_path_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,28 @@

it { is_expected.to eq 'tmp/12/3-thumbnail.jpeg' }
end

describe "#derivatives_for_reference" do
subject { described_class.derivatives_for_reference(object) }
before do
FileUtils.mkdir_p("tmp/12")
File.open("tmp/12/3-thumbnail.jpeg", 'w') do |f|
f.write "test"
end
File.open("tmp/12/4-thumbnail.jpeg", 'w') do |f|
f.write "test"
end
end
after do
FileUtils.rm_rf("tmp/12")
end

let(:object) { double(id: '123') }

it "lists all the paths to derivatives" do
expect(subject).to eq [
"tmp/12/3-thumbnail.jpeg"
]
end
end
end

0 comments on commit a937ad2

Please sign in to comment.