durran-carrierwave 0.3.2.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (91) hide show
  1. data/Generators +4 -0
  2. data/History.txt +66 -0
  3. data/LICENSE +8 -0
  4. data/Manifest.txt +89 -0
  5. data/README.rdoc +342 -0
  6. data/Rakefile +30 -0
  7. data/carrierwave.gemspec +57 -0
  8. data/cucumber.yml +2 -0
  9. data/features/caching.feature +28 -0
  10. data/features/file_storage.feature +37 -0
  11. data/features/file_storage_overridden_filename.feature +38 -0
  12. data/features/file_storage_overridden_store_dir.feature +38 -0
  13. data/features/file_storage_reversing_processor.feature +43 -0
  14. data/features/fixtures/bork.txt +1 -0
  15. data/features/fixtures/monkey.txt +1 -0
  16. data/features/mount_activerecord.feature +46 -0
  17. data/features/mount_datamapper.feature +46 -0
  18. data/features/step_definitions/activerecord_steps.rb +22 -0
  19. data/features/step_definitions/caching_steps.rb +14 -0
  20. data/features/step_definitions/datamapper_steps.rb +29 -0
  21. data/features/step_definitions/file_steps.rb +42 -0
  22. data/features/step_definitions/general_steps.rb +80 -0
  23. data/features/step_definitions/mount_steps.rb +19 -0
  24. data/features/step_definitions/store_steps.rb +18 -0
  25. data/features/support/activerecord.rb +30 -0
  26. data/features/support/datamapper.rb +7 -0
  27. data/features/support/env.rb +35 -0
  28. data/features/versions_basics.feature +50 -0
  29. data/features/versions_nested_versions.feature +70 -0
  30. data/features/versions_overridden_filename.feature +51 -0
  31. data/features/versions_overriden_store_dir.feature +41 -0
  32. data/lib/carrierwave.rb +145 -0
  33. data/lib/carrierwave/compatibility/paperclip.rb +95 -0
  34. data/lib/carrierwave/core_ext/blank.rb +46 -0
  35. data/lib/carrierwave/core_ext/inheritable_attributes.rb +104 -0
  36. data/lib/carrierwave/core_ext/module_setup.rb +51 -0
  37. data/lib/carrierwave/mount.rb +332 -0
  38. data/lib/carrierwave/orm/activerecord.rb +73 -0
  39. data/lib/carrierwave/orm/datamapper.rb +27 -0
  40. data/lib/carrierwave/orm/mongomapper.rb +27 -0
  41. data/lib/carrierwave/orm/sequel.rb +57 -0
  42. data/lib/carrierwave/processing/image_science.rb +72 -0
  43. data/lib/carrierwave/processing/rmagick.rb +286 -0
  44. data/lib/carrierwave/sanitized_file.rb +272 -0
  45. data/lib/carrierwave/storage/abstract.rb +32 -0
  46. data/lib/carrierwave/storage/file.rb +50 -0
  47. data/lib/carrierwave/storage/s3.rb +215 -0
  48. data/lib/carrierwave/test/matchers.rb +114 -0
  49. data/lib/carrierwave/uploader.rb +43 -0
  50. data/lib/carrierwave/uploader/cache.rb +116 -0
  51. data/lib/carrierwave/uploader/callbacks.rb +42 -0
  52. data/lib/carrierwave/uploader/default_path.rb +23 -0
  53. data/lib/carrierwave/uploader/extension_whitelist.rb +37 -0
  54. data/lib/carrierwave/uploader/mountable.rb +39 -0
  55. data/lib/carrierwave/uploader/paths.rb +27 -0
  56. data/lib/carrierwave/uploader/processing.rb +81 -0
  57. data/lib/carrierwave/uploader/proxy.rb +62 -0
  58. data/lib/carrierwave/uploader/remove.rb +23 -0
  59. data/lib/carrierwave/uploader/store.rb +156 -0
  60. data/lib/carrierwave/uploader/url.rb +24 -0
  61. data/lib/carrierwave/uploader/versions.rb +147 -0
  62. data/lib/generators/uploader_generator.rb +22 -0
  63. data/rails_generators/uploader/USAGE +2 -0
  64. data/rails_generators/uploader/templates/uploader.rb +47 -0
  65. data/rails_generators/uploader/uploader_generator.rb +21 -0
  66. data/script/console +10 -0
  67. data/script/destroy +14 -0
  68. data/script/generate +14 -0
  69. data/spec/compatibility/paperclip_spec.rb +43 -0
  70. data/spec/fixtures/bork.txt +1 -0
  71. data/spec/fixtures/test.jpeg +1 -0
  72. data/spec/fixtures/test.jpg +1 -0
  73. data/spec/mount_spec.rb +517 -0
  74. data/spec/orm/activerecord_spec.rb +271 -0
  75. data/spec/orm/datamapper_spec.rb +161 -0
  76. data/spec/orm/mongomapper_spec.rb +184 -0
  77. data/spec/orm/sequel_spec.rb +192 -0
  78. data/spec/sanitized_file_spec.rb +612 -0
  79. data/spec/spec_helper.rb +99 -0
  80. data/spec/uploader/cache_spec.rb +196 -0
  81. data/spec/uploader/default_path_spec.rb +68 -0
  82. data/spec/uploader/extension_whitelist_spec.rb +44 -0
  83. data/spec/uploader/mountable_spec.rb +33 -0
  84. data/spec/uploader/paths_spec.rb +22 -0
  85. data/spec/uploader/processing_spec.rb +62 -0
  86. data/spec/uploader/proxy_spec.rb +54 -0
  87. data/spec/uploader/remove_spec.rb +70 -0
  88. data/spec/uploader/store_spec.rb +274 -0
  89. data/spec/uploader/url_spec.rb +87 -0
  90. data/spec/uploader/versions_spec.rb +306 -0
  91. metadata +228 -0
@@ -0,0 +1,99 @@
1
+ # encoding: utf-8
2
+
3
+ $TESTING=true
4
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
5
+
6
+ require 'rubygems'
7
+
8
+ if ENV["AS"]
9
+ puts "--> using ActiveSupport"
10
+ require 'activesupport'
11
+ elsif ENV["EXTLIB"]
12
+ puts "--> using Extlib"
13
+ require 'extlib'
14
+ end
15
+
16
+ require 'tempfile'
17
+ #require 'ruby-debug'
18
+ require 'spec'
19
+ require 'spec/autorun'
20
+
21
+ require 'carrierwave'
22
+
23
+ require 'logger'
24
+ CarrierWave.logger = Logger.new(nil)
25
+
26
+ alias :running :lambda
27
+
28
+ def file_path( *paths )
29
+ File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', *paths))
30
+ end
31
+
32
+ def public_path( *paths )
33
+ File.expand_path(File.join(File.dirname(__FILE__), 'public', *paths))
34
+ end
35
+
36
+ CarrierWave.config[:public] = public_path
37
+ CarrierWave.config[:root] = File.expand_path(File.dirname(__FILE__))
38
+
39
+ module CarrierWave
40
+ module Test
41
+ module MockStorage
42
+ def mock_storage(kind)
43
+ storage = mock("storage for #{kind} uploader")
44
+ storage.stub!(:setup!)
45
+ storage
46
+ end
47
+ end
48
+
49
+ module MockFiles
50
+ def stub_merb_tempfile(filename)
51
+ raise "#{path} file does not exist" unless File.exist?(file_path(filename))
52
+
53
+ t = Tempfile.new(filename)
54
+ FileUtils.copy_file(file_path(filename), t.path)
55
+
56
+ return t
57
+ end
58
+
59
+ def stub_tempfile(filename, mime_type=nil, fake_name=nil)
60
+ raise "#{path} file does not exist" unless File.exist?(file_path(filename))
61
+
62
+ t = Tempfile.new(filename)
63
+ FileUtils.copy_file(file_path(filename), t.path)
64
+
65
+ # This is stupid, but for some reason rspec won't play nice...
66
+ eval <<-EOF
67
+ def t.original_filename; '#{fake_name || filename}'; end
68
+ def t.content_type; '#{mime_type}'; end
69
+ def t.local_path; path; end
70
+ EOF
71
+
72
+ return t
73
+ end
74
+
75
+ def stub_stringio(filename, mime_type=nil, fake_name=nil)
76
+ if filename
77
+ t = StringIO.new( IO.read( file_path( filename ) ) )
78
+ else
79
+ t = StringIO.new
80
+ end
81
+ t.stub!(:local_path).and_return("")
82
+ t.stub!(:original_filename).and_return(filename || fake_name)
83
+ t.stub!(:content_type).and_return(mime_type)
84
+ return t
85
+ end
86
+
87
+ def stub_file(filename, mime_type=nil, fake_name=nil)
88
+ f = File.open(file_path(filename))
89
+ return f
90
+ end
91
+ end
92
+ end
93
+ end
94
+
95
+ Spec::Runner.configure do |config|
96
+ config.include CarrierWave::Test::Matchers
97
+ config.include CarrierWave::Test::MockFiles
98
+ config.include CarrierWave::Test::MockStorage
99
+ end
@@ -0,0 +1,196 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+
5
+ describe CarrierWave::Uploader do
6
+
7
+ before do
8
+ @uploader_class = Class.new(CarrierWave::Uploader::Base)
9
+ @uploader = @uploader_class.new
10
+ end
11
+
12
+ after do
13
+ FileUtils.rm_rf(public_path)
14
+ end
15
+
16
+ describe '#cache_dir' do
17
+ it "should default to the config option" do
18
+ @uploader.cache_dir.should == 'uploads/tmp'
19
+ end
20
+ end
21
+
22
+ describe '#cache!' do
23
+
24
+ before do
25
+ CarrierWave.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
26
+ end
27
+
28
+ it "should cache a file" do
29
+ @uploader.cache!(File.open(file_path('test.jpg')))
30
+ @uploader.file.should be_an_instance_of(CarrierWave::SanitizedFile)
31
+ end
32
+
33
+ it "should be cached" do
34
+ @uploader.cache!(File.open(file_path('test.jpg')))
35
+ @uploader.should be_cached
36
+ end
37
+
38
+ it "should store the cache name" do
39
+ @uploader.cache!(File.open(file_path('test.jpg')))
40
+ @uploader.cache_name.should == '20071201-1234-345-2255/test.jpg'
41
+ end
42
+
43
+ it "should set the filename to the file's sanitized filename" do
44
+ @uploader.cache!(File.open(file_path('test.jpg')))
45
+ @uploader.filename.should == 'test.jpg'
46
+ end
47
+
48
+ it "should move it to the tmp dir" do
49
+ @uploader.cache!(File.open(file_path('test.jpg')))
50
+ @uploader.file.path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpg')
51
+ @uploader.file.exists?.should be_true
52
+ end
53
+
54
+ it "should not move it if cache_to_cache_dir is false" do
55
+ CarrierWave.config[:cache_to_cache_dir] = false
56
+ path = file_path('test.jpg')
57
+ @uploader.cache!(File.open(path))
58
+ @uploader.current_path.should == path
59
+ @uploader.file.exists?.should be_true
60
+ CarrierWave.config[:cache_to_cache_dir] = true
61
+ end
62
+
63
+ it "should set the url" do
64
+ @uploader.cache!(File.open(file_path('test.jpg')))
65
+ @uploader.url.should == '/uploads/tmp/20071201-1234-345-2255/test.jpg'
66
+ end
67
+
68
+ it "should raise an error when trying to cache a string" do
69
+ running {
70
+ @uploader.cache!(file_path('test.jpg'))
71
+ }.should raise_error(CarrierWave::FormNotMultipart)
72
+ end
73
+
74
+ it "should raise an error when trying to cache a pathname" do
75
+ running {
76
+ @uploader.cache!(Pathname.new(file_path('test.jpg')))
77
+ }.should raise_error(CarrierWave::FormNotMultipart)
78
+ end
79
+
80
+ it "should do nothing when trying to cache an empty file" do
81
+ @uploader.cache!(nil)
82
+ end
83
+
84
+ it "should set permissions if options are given" do
85
+ old_permissions = CarrierWave.config[:permissions]
86
+ CarrierWave.config[:permissions] = 0777
87
+
88
+ @uploader.cache!(File.open(file_path('test.jpg')))
89
+ @uploader.should have_permissions(0777)
90
+
91
+ CarrierWave.config[:permissions] = old_permissions
92
+ end
93
+ end
94
+
95
+ describe '#retrieve_from_cache!' do
96
+ it "should cache a file" do
97
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
98
+ @uploader.file.should be_an_instance_of(CarrierWave::SanitizedFile)
99
+ end
100
+
101
+ it "should be cached" do
102
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
103
+ @uploader.should be_cached
104
+ end
105
+
106
+ it "should set the path to the tmp dir" do
107
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
108
+ @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpeg')
109
+ end
110
+
111
+ it "should overwrite a file that has already been cached" do
112
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
113
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/bork.txt')
114
+ @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/bork.txt')
115
+ end
116
+
117
+ it "should store the cache_name" do
118
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
119
+ @uploader.cache_name.should == '20071201-1234-345-2255/test.jpeg'
120
+ end
121
+
122
+ it "should store the filename" do
123
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
124
+ @uploader.filename.should == 'test.jpeg'
125
+ end
126
+
127
+ it "should set the url" do
128
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpeg')
129
+ @uploader.url.should == '/uploads/tmp/20071201-1234-345-2255/test.jpeg'
130
+ end
131
+
132
+ it "should raise an error when the cache_id has an invalid format" do
133
+ running {
134
+ @uploader.retrieve_from_cache!('12345/test.jpeg')
135
+ }.should raise_error(CarrierWave::InvalidParameter)
136
+
137
+ @uploader.file.should be_nil
138
+ @uploader.filename.should be_nil
139
+ @uploader.cache_name.should be_nil
140
+ end
141
+
142
+ it "should raise an error when the original_filename contains invalid characters" do
143
+ running {
144
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/te/st.jpeg')
145
+ }.should raise_error(CarrierWave::InvalidParameter)
146
+ running {
147
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/te??%st.jpeg')
148
+ }.should raise_error(CarrierWave::InvalidParameter)
149
+
150
+ @uploader.file.should be_nil
151
+ @uploader.filename.should be_nil
152
+ @uploader.cache_name.should be_nil
153
+ end
154
+ end
155
+
156
+ describe 'with an overridden, reversing, filename' do
157
+ before do
158
+ @uploader_class.class_eval do
159
+ def filename
160
+ super.reverse unless super.blank?
161
+ end
162
+ end
163
+ end
164
+
165
+ describe '#cache!' do
166
+
167
+ before do
168
+ CarrierWave.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
169
+ end
170
+
171
+ it "should set the filename to the file's reversed filename" do
172
+ @uploader.cache!(File.open(file_path('test.jpg')))
173
+ @uploader.filename.should == "gpj.tset"
174
+ end
175
+
176
+ it "should move it to the tmp dir with the filename unreversed" do
177
+ @uploader.cache!(File.open(file_path('test.jpg')))
178
+ @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpg')
179
+ @uploader.file.exists?.should be_true
180
+ end
181
+ end
182
+
183
+ describe '#retrieve_from_cache!' do
184
+ it "should set the path to the tmp dir" do
185
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpg')
186
+ @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpg')
187
+ end
188
+
189
+ it "should set the filename to the reversed name of the file" do
190
+ @uploader.retrieve_from_cache!('20071201-1234-345-2255/test.jpg')
191
+ @uploader.filename.should == "gpj.tset"
192
+ end
193
+ end
194
+ end
195
+
196
+ end
@@ -0,0 +1,68 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+
5
+ describe CarrierWave::Uploader do
6
+
7
+ before do
8
+ @uploader_class = Class.new(CarrierWave::Uploader::Base)
9
+ @uploader = @uploader_class.new
10
+ end
11
+
12
+ after do
13
+ FileUtils.rm_rf(public_path)
14
+ end
15
+
16
+ describe 'with a default path' do
17
+ before do
18
+ @uploader_class.class_eval do
19
+ def default_path
20
+ file_path('test.jpg')
21
+ end
22
+ end
23
+ @uploader = @uploader_class.new
24
+ end
25
+
26
+ describe '#blank?' do
27
+ it "should be true by default" do
28
+ @uploader.should be_blank
29
+ end
30
+ end
31
+
32
+ describe '#current_path' do
33
+ it "should return the default path" do
34
+ @uploader.current_path.should == file_path('test.jpg')
35
+ end
36
+ end
37
+
38
+ describe '#cache!' do
39
+
40
+ before do
41
+ CarrierWave.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
42
+ end
43
+
44
+ it "should cache a file" do
45
+ @uploader.cache!(File.open(file_path('test.jpg')))
46
+ @uploader.file.should be_an_instance_of(CarrierWave::SanitizedFile)
47
+ end
48
+
49
+ it "should be cached" do
50
+ @uploader.cache!(File.open(file_path('test.jpg')))
51
+ @uploader.should be_cached
52
+ end
53
+
54
+ it "should no longer be blank" do
55
+ @uploader.cache!(File.open(file_path('test.jpg')))
56
+ @uploader.should_not be_blank
57
+ end
58
+
59
+ it "should set the current_path" do
60
+ @uploader.cache!(File.open(file_path('test.jpg')))
61
+ @uploader.current_path.should == public_path('uploads/tmp/20071201-1234-345-2255/test.jpg')
62
+ end
63
+
64
+ end
65
+
66
+ end
67
+
68
+ end
@@ -0,0 +1,44 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+
5
+ describe CarrierWave::Uploader do
6
+
7
+ before do
8
+ @uploader_class = Class.new(CarrierWave::Uploader::Base)
9
+ @uploader = @uploader_class.new
10
+ end
11
+
12
+ after do
13
+ FileUtils.rm_rf(public_path)
14
+ end
15
+
16
+ describe '#cache!' do
17
+
18
+ before do
19
+ CarrierWave.stub!(:generate_cache_id).and_return('20071201-1234-345-2255')
20
+ end
21
+
22
+ it "should not raise an integiry error if there is no white list" do
23
+ @uploader.stub!(:extension_white_list).and_return(nil)
24
+ running {
25
+ @uploader.cache!(File.open(file_path('test.jpg')))
26
+ }.should_not raise_error(CarrierWave::IntegrityError)
27
+ end
28
+
29
+ it "should not raise an integiry error if there is a white list and the file is on it" do
30
+ @uploader.stub!(:extension_white_list).and_return(%w(jpg gif png))
31
+ running {
32
+ @uploader.cache!(File.open(file_path('test.jpg')))
33
+ }.should_not raise_error(CarrierWave::IntegrityError)
34
+ end
35
+
36
+ it "should raise an integiry error if there is a white list and the file is not on it" do
37
+ @uploader.stub!(:extension_white_list).and_return(%w(txt doc xls))
38
+ running {
39
+ @uploader.cache!(File.open(file_path('test.jpg')))
40
+ }.should raise_error(CarrierWave::IntegrityError)
41
+ end
42
+ end
43
+
44
+ end
@@ -0,0 +1,33 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+
5
+ describe CarrierWave::Uploader do
6
+
7
+ before do
8
+ @uploader_class = Class.new(CarrierWave::Uploader::Base)
9
+ @uploader = @uploader_class.new
10
+ end
11
+
12
+ after do
13
+ FileUtils.rm_rf(public_path)
14
+ end
15
+
16
+ describe '#model' do
17
+ it "should be remembered from initialization" do
18
+ model = mock('a model object')
19
+ @uploader = @uploader_class.new(model)
20
+ @uploader.model.should == model
21
+ end
22
+ end
23
+
24
+ describe '#mounted_as' do
25
+ it "should be remembered from initialization" do
26
+ model = mock('a model object')
27
+ @uploader = @uploader_class.new(model, :llama)
28
+ @uploader.model.should == model
29
+ @uploader.mounted_as.should == :llama
30
+ end
31
+ end
32
+
33
+ end
@@ -0,0 +1,22 @@
1
+ # encoding: utf-8
2
+
3
+ require File.dirname(__FILE__) + '/../spec_helper'
4
+
5
+ describe CarrierWave::Uploader do
6
+
7
+ before do
8
+ @uploader_class = Class.new(CarrierWave::Uploader::Base)
9
+ @uploader = @uploader_class.new
10
+ end
11
+
12
+ after do
13
+ FileUtils.rm_rf(public_path)
14
+ end
15
+
16
+ describe '#root' do
17
+ it "should default to the config option" do
18
+ @uploader.root.should == public_path('..')
19
+ end
20
+ end
21
+
22
+ end