Skip to content

Commit

Permalink
Bucket derivatives on disk.
Browse files Browse the repository at this point in the history
Closes #331.
  • Loading branch information
Trey Terrell committed Sep 11, 2015
1 parent a6f2f01 commit 9addee9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ def derivative_path_for_reference(object, destination_name)

def derivative_path(object, extension, destination_name)
file_name = destination_name + extension
File.join(CurationConcerns.config.derivatives_path, object.id, file_name)
File.join(CurationConcerns.config.derivatives_path, pair_path(object.id, file_name))
end

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

def extension_for(destination_name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,14 @@ def self.call(object, file, destination_name)
# 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)
name = derivative_path_factory.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

def self.derivative_path_factory
DerivativePath
end
end
end
2 changes: 1 addition & 1 deletion spec/services/derivative_path_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@
let(:object) { double(id: '123') }
let(:destination_name) { 'thumbnail' }

it { is_expected.to eq 'tmp/123/thumbnail.jpeg' }
it { is_expected.to eq 'tmp/12/3-thumbnail.jpeg' }
end
end
6 changes: 3 additions & 3 deletions spec/services/persist_derivatives_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
describe '.output_file' do
subject { described_class.output_file(object, destination_name, &block) }

let(:object) { double(id: '123') }
let(:object) { double(id: '1234567') }
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(FileUtils).to receive(:mkdir_p).with('tmp/12/34/56')
expect(File).to receive(:open).with('tmp/12/34/56/7-thumbnail.jpeg', 'wb') do |*_, &blk|
expect(blk).to be(block)
end
subject
Expand Down

0 comments on commit 9addee9

Please sign in to comment.