Skip to content

Commit fe1de4f

Browse files
author
Fernando Blat
committed
Allow a new option in versions from which start to process the file.
For performance reasons, when you are working with big files it makes sense, if possible, start processing the file from a small version, instead of using the original one. I.e.: version :thumb do process resize_to_fill: [280, 280] end version :small, from_version: :thumb do process resize_to_fill: [60, 60] end
1 parent efaafc4 commit fe1de4f

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

features/fixtures/upcased_bork.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
THIS IS A FILE

features/step_definitions/general_steps.rb

+20-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@
99
@uploader = @klass.new
1010
end
1111

12+
Given /^a processor method named :upcase$/ do
13+
@klass.class_eval do
14+
define_method(:upcase) do
15+
content = File.read(current_path)
16+
File.write(current_path, content.upcase)
17+
end
18+
end
19+
end
20+
1221
Then /^the contents of the file should be '(.*?)'$/ do |contents|
1322
@uploader.read.chomp.should == contents
1423
end
@@ -45,10 +54,20 @@ def filename
4554
end
4655
end
4756

48-
Given /^that the uploader class has a version named '(.*?)'$/ do |name|
57+
Given /^that the uploader class has a version named '([^\']+)'$/ do |name|
4958
@klass.version(name)
5059
end
5160

61+
Given /^that the uploader class has a version named '([^\']+)' which process '([a-zA-Z0-9\_\?!]*)'$/ do |name, processor_name|
62+
@klass.version(name) do
63+
process processor_name.to_sym
64+
end
65+
end
66+
67+
Given /^that the uploader class has a version named '([^\']+)' which is based on version '(.*?)'$/ do |name, based_version_name|
68+
@klass.version(name, {:from_version => based_version_name.to_sym})
69+
end
70+
5271
Given /^yo dawg, I put a version called '(.*?)' in your version called '(.*?)'$/ do |v2, v1|
5372
@klass.version(v1) do
5473
version(v2)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
Feature: uploader with file storage and versions with overridden store dir
2+
In order to be awesome
3+
As a developer using CarrierWave
4+
I want to upload files to the filesystem
5+
6+
Background:
7+
Given an uploader class that uses the 'file' storage
8+
Given a processor method named :upcase
9+
And that the uploader class has a version named 'thumb' which process 'upcase'
10+
And that the version 'thumb' has the store_dir overridden to 'public/monkey/llama'
11+
And that the uploader class has a version named 'small_thumb' which is based on version 'thumb'
12+
And that the version 'small_thumb' has the store_dir overridden to 'public/monkey/toro'
13+
And an instance of that class
14+
15+
Scenario: cache a file and then store it
16+
When I cache the file 'fixtures/bork.txt'
17+
Then there should be a file called 'bork.txt' somewhere in a subdirectory of 'public/uploads/tmp'
18+
Then there should be a file called 'thumb_bork.txt' somewhere in a subdirectory of 'public/uploads/tmp'
19+
Then there should be a file called 'small_thumb_bork.txt' somewhere in a subdirectory of 'public/uploads/tmp'
20+
And the file called 'bork.txt' in a subdirectory of 'public/uploads/tmp' should be identical to the file at 'fixtures/bork.txt'
21+
And the file called 'thumb_bork.txt' in a subdirectory of 'public/uploads/tmp' should be identical to the file at 'fixtures/upcased_bork.txt'
22+
And the file called 'small_thumb_bork.txt' in a subdirectory of 'public/uploads/tmp' should be identical to the file at 'fixtures/upcased_bork.txt'
23+
And there should not be a file at 'public/uploads/bork.txt'
24+
And there should not be a file at 'public/monkey/llama/thumb_bork.txt'
25+
And there should not be a file at 'public/monkey/toro/small_thumb_bork.txt'
26+
When I store the file
27+
Then there should be a file at 'public/uploads/bork.txt'
28+
Then there should be a file at 'public/monkey/llama/thumb_bork.txt'
29+
Then there should be a file at 'public/monkey/toro/small_thumb_bork.txt'
30+
And the file at 'public/uploads/bork.txt' should be identical to the file at 'fixtures/bork.txt'
31+
And the file at 'public/monkey/llama/thumb_bork.txt' should be identical to the file at 'fixtures/upcased_bork.txt'
32+
And the file at 'public/monkey/toro/small_thumb_bork.txt' should be identical to the file at 'fixtures/upcased_bork.txt'

lib/carrierwave/uploader/versions.rb

+10-1
Original file line numberDiff line numberDiff line change
@@ -214,8 +214,17 @@ def cache_versions!(new_file)
214214
:filename => new_file.original_filename
215215

216216
active_versions.each do |name, v|
217+
# If option :from_version is present, create cache using cached file from
218+
# version indicated
219+
if self.class.versions[name][:options] &&
220+
self.class.versions[name][:options][:from_version]
221+
processed_version = SanitizedFile.new :tempfile => versions[self.class.versions[name][:options][:from_version]],
222+
:filename => new_file.original_filename
223+
v.cache!(processed_version)
224+
else
225+
v.cache!(processed_parent)
226+
end
217227
v.send(:cache_id=, cache_id)
218-
v.cache!(processed_parent)
219228
end
220229
end
221230

0 commit comments

Comments
 (0)