durran-carrierwave 0.3.2.3
Sign up to get free protection for your applications and to get access to all the features.
- data/Generators +4 -0
- data/History.txt +66 -0
- data/LICENSE +8 -0
- data/Manifest.txt +89 -0
- data/README.rdoc +342 -0
- data/Rakefile +30 -0
- data/carrierwave.gemspec +57 -0
- data/cucumber.yml +2 -0
- data/features/caching.feature +28 -0
- data/features/file_storage.feature +37 -0
- data/features/file_storage_overridden_filename.feature +38 -0
- data/features/file_storage_overridden_store_dir.feature +38 -0
- data/features/file_storage_reversing_processor.feature +43 -0
- data/features/fixtures/bork.txt +1 -0
- data/features/fixtures/monkey.txt +1 -0
- data/features/mount_activerecord.feature +46 -0
- data/features/mount_datamapper.feature +46 -0
- data/features/step_definitions/activerecord_steps.rb +22 -0
- data/features/step_definitions/caching_steps.rb +14 -0
- data/features/step_definitions/datamapper_steps.rb +29 -0
- data/features/step_definitions/file_steps.rb +42 -0
- data/features/step_definitions/general_steps.rb +80 -0
- data/features/step_definitions/mount_steps.rb +19 -0
- data/features/step_definitions/store_steps.rb +18 -0
- data/features/support/activerecord.rb +30 -0
- data/features/support/datamapper.rb +7 -0
- data/features/support/env.rb +35 -0
- data/features/versions_basics.feature +50 -0
- data/features/versions_nested_versions.feature +70 -0
- data/features/versions_overridden_filename.feature +51 -0
- data/features/versions_overriden_store_dir.feature +41 -0
- data/lib/carrierwave.rb +145 -0
- data/lib/carrierwave/compatibility/paperclip.rb +95 -0
- data/lib/carrierwave/core_ext/blank.rb +46 -0
- data/lib/carrierwave/core_ext/inheritable_attributes.rb +104 -0
- data/lib/carrierwave/core_ext/module_setup.rb +51 -0
- data/lib/carrierwave/mount.rb +332 -0
- data/lib/carrierwave/orm/activerecord.rb +73 -0
- data/lib/carrierwave/orm/datamapper.rb +27 -0
- data/lib/carrierwave/orm/mongomapper.rb +27 -0
- data/lib/carrierwave/orm/sequel.rb +57 -0
- data/lib/carrierwave/processing/image_science.rb +72 -0
- data/lib/carrierwave/processing/rmagick.rb +286 -0
- data/lib/carrierwave/sanitized_file.rb +272 -0
- data/lib/carrierwave/storage/abstract.rb +32 -0
- data/lib/carrierwave/storage/file.rb +50 -0
- data/lib/carrierwave/storage/s3.rb +215 -0
- data/lib/carrierwave/test/matchers.rb +114 -0
- data/lib/carrierwave/uploader.rb +43 -0
- data/lib/carrierwave/uploader/cache.rb +116 -0
- data/lib/carrierwave/uploader/callbacks.rb +42 -0
- data/lib/carrierwave/uploader/default_path.rb +23 -0
- data/lib/carrierwave/uploader/extension_whitelist.rb +37 -0
- data/lib/carrierwave/uploader/mountable.rb +39 -0
- data/lib/carrierwave/uploader/paths.rb +27 -0
- data/lib/carrierwave/uploader/processing.rb +81 -0
- data/lib/carrierwave/uploader/proxy.rb +62 -0
- data/lib/carrierwave/uploader/remove.rb +23 -0
- data/lib/carrierwave/uploader/store.rb +156 -0
- data/lib/carrierwave/uploader/url.rb +24 -0
- data/lib/carrierwave/uploader/versions.rb +147 -0
- data/lib/generators/uploader_generator.rb +22 -0
- data/rails_generators/uploader/USAGE +2 -0
- data/rails_generators/uploader/templates/uploader.rb +47 -0
- data/rails_generators/uploader/uploader_generator.rb +21 -0
- data/script/console +10 -0
- data/script/destroy +14 -0
- data/script/generate +14 -0
- data/spec/compatibility/paperclip_spec.rb +43 -0
- data/spec/fixtures/bork.txt +1 -0
- data/spec/fixtures/test.jpeg +1 -0
- data/spec/fixtures/test.jpg +1 -0
- data/spec/mount_spec.rb +517 -0
- data/spec/orm/activerecord_spec.rb +271 -0
- data/spec/orm/datamapper_spec.rb +161 -0
- data/spec/orm/mongomapper_spec.rb +184 -0
- data/spec/orm/sequel_spec.rb +192 -0
- data/spec/sanitized_file_spec.rb +612 -0
- data/spec/spec_helper.rb +99 -0
- data/spec/uploader/cache_spec.rb +196 -0
- data/spec/uploader/default_path_spec.rb +68 -0
- data/spec/uploader/extension_whitelist_spec.rb +44 -0
- data/spec/uploader/mountable_spec.rb +33 -0
- data/spec/uploader/paths_spec.rb +22 -0
- data/spec/uploader/processing_spec.rb +62 -0
- data/spec/uploader/proxy_spec.rb +54 -0
- data/spec/uploader/remove_spec.rb +70 -0
- data/spec/uploader/store_spec.rb +274 -0
- data/spec/uploader/url_spec.rb +87 -0
- data/spec/uploader/versions_spec.rb +306 -0
- metadata +228 -0
@@ -0,0 +1,192 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
4
|
+
|
5
|
+
require 'carrierwave/orm/sequel'
|
6
|
+
|
7
|
+
DB = Sequel.sqlite
|
8
|
+
|
9
|
+
describe CarrierWave::Sequel do
|
10
|
+
|
11
|
+
def setup_variables_for_class(klass)
|
12
|
+
uploader = Class.new(CarrierWave::Uploader::Base)
|
13
|
+
klass.mount_uploader(:image, uploader)
|
14
|
+
model = klass.new
|
15
|
+
[klass, uploader, model]
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '.mount_uploader' do
|
19
|
+
|
20
|
+
before(:all) do
|
21
|
+
DB.create_table :events do
|
22
|
+
primary_key :id
|
23
|
+
column :image, :string
|
24
|
+
column :textfile, :string
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
after(:all) do
|
29
|
+
DB.drop_table :events
|
30
|
+
end
|
31
|
+
|
32
|
+
before(:each) do
|
33
|
+
@class = Class.new(Sequel::Model)
|
34
|
+
@class.set_dataset :events
|
35
|
+
@class, @uploader, @event = setup_variables_for_class(@class)
|
36
|
+
end
|
37
|
+
|
38
|
+
after(:each) { @class.delete }
|
39
|
+
|
40
|
+
describe '#image' do
|
41
|
+
|
42
|
+
it "should return blank uploader when nothing has been assigned" do
|
43
|
+
@event.image.should be_blank
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should return blank uploader when an empty string has been assigned" do
|
47
|
+
@event[:image] = ''
|
48
|
+
@event.save
|
49
|
+
@event.reload
|
50
|
+
@event.image.should be_blank
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should retrieve a file from the storage if a value is stored in the database" do
|
54
|
+
@event[:image] = 'test.jpeg'
|
55
|
+
@event.save
|
56
|
+
@event.reload
|
57
|
+
@event.image.should be_an_instance_of(@uploader)
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should set the path to the store dir" do
|
61
|
+
@event[:image] = 'test.jpeg'
|
62
|
+
@event.save
|
63
|
+
@event.reload
|
64
|
+
@event.image.current_path.should == public_path('uploads/test.jpeg')
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
describe '#image=' do
|
70
|
+
|
71
|
+
it "should cache a file" do
|
72
|
+
@event.image = stub_file('test.jpeg')
|
73
|
+
@event.image.should be_an_instance_of(@uploader)
|
74
|
+
end
|
75
|
+
|
76
|
+
it "should write nothing to the database, to prevent overriden filenames to fail because of unassigned attributes" do
|
77
|
+
@event[:image].should be_nil
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should copy a file into into the cache directory" do
|
81
|
+
@event.image = stub_file('test.jpeg')
|
82
|
+
@event.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should do nothing when nil is assigned" do
|
86
|
+
@event.image = nil
|
87
|
+
@event.image.should be_blank
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should do nothing when an empty string is assigned" do
|
91
|
+
@event.image = ''
|
92
|
+
@event.image.should be_blank
|
93
|
+
end
|
94
|
+
|
95
|
+
end
|
96
|
+
|
97
|
+
describe '#save' do
|
98
|
+
|
99
|
+
it "should do nothing when no file has been assigned" do
|
100
|
+
@event.save.should be_true
|
101
|
+
@event.image.should be_blank
|
102
|
+
end
|
103
|
+
|
104
|
+
it "should copy the file to the upload directory when a file has been assigned" do
|
105
|
+
@event.image = stub_file('test.jpeg')
|
106
|
+
@event.save.should be_true
|
107
|
+
@event.image.should be_an_instance_of(@uploader)
|
108
|
+
@event.image.current_path.should == public_path('uploads/test.jpeg')
|
109
|
+
end
|
110
|
+
|
111
|
+
describe 'with validation' do
|
112
|
+
|
113
|
+
before do
|
114
|
+
# Add validations
|
115
|
+
if CarrierWave::Sequel.new_sequel?
|
116
|
+
@class.class_eval do
|
117
|
+
def validate
|
118
|
+
errors.add(:image, 'FAIL!')
|
119
|
+
end
|
120
|
+
end
|
121
|
+
else
|
122
|
+
@class.class_eval do
|
123
|
+
validates_each(:image) do |o,a,v|
|
124
|
+
o.errors.add(a, 'FAIL!')
|
125
|
+
end
|
126
|
+
end
|
127
|
+
end
|
128
|
+
# Turn off raising the exceptions on save
|
129
|
+
@event.raise_on_save_failure = false
|
130
|
+
end
|
131
|
+
|
132
|
+
it "should do nothing when a validation fails" do
|
133
|
+
@event.image = stub_file('test.jpeg')
|
134
|
+
@event.should_not be_valid
|
135
|
+
@event.save
|
136
|
+
@event.should be_new
|
137
|
+
@event.image.should be_an_instance_of(@uploader)
|
138
|
+
@event.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
|
139
|
+
end
|
140
|
+
end
|
141
|
+
|
142
|
+
it "should assign the filename to the database" do
|
143
|
+
@event.image = stub_file('test.jpeg')
|
144
|
+
@event.save.should be_true
|
145
|
+
@event.reload
|
146
|
+
@event[:image].should == 'test.jpeg'
|
147
|
+
end
|
148
|
+
|
149
|
+
it "should remove the image if remove_image? returns true" do
|
150
|
+
@event.image = stub_file('test.jpeg')
|
151
|
+
@event.save
|
152
|
+
@event.remove_image = true
|
153
|
+
@event.save
|
154
|
+
@event.reload
|
155
|
+
@event.image.should be_blank
|
156
|
+
@event[:image].should == ''
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
describe 'with overriddent filename' do
|
161
|
+
|
162
|
+
describe '#save' do
|
163
|
+
|
164
|
+
before do
|
165
|
+
@uploader.class_eval do
|
166
|
+
def filename
|
167
|
+
model.name + File.extname(super)
|
168
|
+
end
|
169
|
+
end
|
170
|
+
@event.stub!(:name).and_return('jonas')
|
171
|
+
end
|
172
|
+
|
173
|
+
it "should copy the file to the upload directory when a file has been assigned" do
|
174
|
+
@event.image = stub_file('test.jpeg')
|
175
|
+
@event.save.should be_true
|
176
|
+
@event.image.should be_an_instance_of(@uploader)
|
177
|
+
@event.image.current_path.should == public_path('uploads/jonas.jpeg')
|
178
|
+
end
|
179
|
+
|
180
|
+
it "should assign an overridden filename to the database" do
|
181
|
+
@event.image = stub_file('test.jpeg')
|
182
|
+
@event.save.should be_true
|
183
|
+
@event.reload
|
184
|
+
@event[:image].should == 'jonas.jpeg'
|
185
|
+
end
|
186
|
+
|
187
|
+
end
|
188
|
+
|
189
|
+
end
|
190
|
+
|
191
|
+
end
|
192
|
+
end
|
@@ -0,0 +1,612 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
4
|
+
|
5
|
+
describe CarrierWave::SanitizedFile do
|
6
|
+
|
7
|
+
before do
|
8
|
+
unless File.exists?(file_path('llama.jpg'))
|
9
|
+
FileUtils.cp(file_path('test.jpg'), file_path('llama.jpg'))
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
after(:all) do
|
14
|
+
if File.exists?(file_path('llama.jpg'))
|
15
|
+
FileUtils.rm(file_path('llama.jpg'))
|
16
|
+
end
|
17
|
+
FileUtils.rm_rf(public_path)
|
18
|
+
end
|
19
|
+
|
20
|
+
describe '#empty?' do
|
21
|
+
|
22
|
+
it "should be empty for nil" do
|
23
|
+
@sanitized_file = CarrierWave::SanitizedFile.new(nil)
|
24
|
+
@sanitized_file.should be_empty
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should be empty for an empty string" do
|
28
|
+
@sanitized_file = CarrierWave::SanitizedFile.new("")
|
29
|
+
@sanitized_file.should be_empty
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should be empty for an empty StringIO" do
|
33
|
+
@sanitized_file = CarrierWave::SanitizedFile.new(StringIO.new(""))
|
34
|
+
@sanitized_file.should be_empty
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should be empty for a file with a zero size" do
|
38
|
+
empty_file = mock('emptyfile')
|
39
|
+
empty_file.should_receive(:size).at_least(:once).and_return(0)
|
40
|
+
@sanitized_file = CarrierWave::SanitizedFile.new(empty_file)
|
41
|
+
@sanitized_file.should be_empty
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
describe '#original_filename' do
|
47
|
+
it "should default to the original_filename" do
|
48
|
+
file = mock('file', :original_filename => 'llama.jpg')
|
49
|
+
sanitized_file = CarrierWave::SanitizedFile.new(file)
|
50
|
+
sanitized_file.original_filename.should == "llama.jpg"
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should defer to the base name of the path if original_filename is unavailable" do
|
54
|
+
file = mock('file', :path => '/path/to/test.jpg')
|
55
|
+
sanitized_file = CarrierWave::SanitizedFile.new(file)
|
56
|
+
sanitized_file.original_filename.should == "test.jpg"
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should be nil otherwise" do
|
60
|
+
file = mock('file')
|
61
|
+
sanitized_file = CarrierWave::SanitizedFile.new(file)
|
62
|
+
sanitized_file.original_filename.should be_nil
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
describe '#basename' do
|
67
|
+
it "should return the basename for complicated extensions" do
|
68
|
+
@sanitized_file = CarrierWave::SanitizedFile.new(file_path('complex.filename.tar.gz'))
|
69
|
+
@sanitized_file.basename.should == "complex.filename"
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should be the filename if the file has no extension" do
|
73
|
+
@sanitized_file = CarrierWave::SanitizedFile.new(file_path('complex'))
|
74
|
+
@sanitized_file.basename.should == "complex"
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
describe '#extension' do
|
79
|
+
it "should return the extension for complicated extensions" do
|
80
|
+
@sanitized_file = CarrierWave::SanitizedFile.new(file_path('complex.filename.tar.gz'))
|
81
|
+
@sanitized_file.extension.should == "tar.gz"
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should be an empty string if the file has no extension" do
|
85
|
+
@sanitized_file = CarrierWave::SanitizedFile.new(file_path('complex'))
|
86
|
+
@sanitized_file.extension.should == ""
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
describe '#filename' do
|
91
|
+
|
92
|
+
before do
|
93
|
+
@sanitized_file = CarrierWave::SanitizedFile.new(nil)
|
94
|
+
end
|
95
|
+
|
96
|
+
it "should default to the original filename if it is valid" do
|
97
|
+
@sanitized_file.should_receive(:original_filename).at_least(:once).and_return("llama.jpg")
|
98
|
+
@sanitized_file.filename.should == "llama.jpg"
|
99
|
+
end
|
100
|
+
|
101
|
+
it "should remove illegal characters from a filename" do
|
102
|
+
@sanitized_file.should_receive(:original_filename).at_least(:once).and_return("test-s,%&m#st?.jpg")
|
103
|
+
@sanitized_file.filename.should == "test-s___m_st_.jpg"
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should remove slashes from the filename" do
|
107
|
+
@sanitized_file.should_receive(:original_filename).at_least(:once).and_return("../../very_tricky/foo.bar")
|
108
|
+
@sanitized_file.filename.should_not =~ /[\\\/]/
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should remove illegal characters if there is no extension" do
|
112
|
+
@sanitized_file.should_receive(:original_filename).at_least(:once).and_return('`*foo')
|
113
|
+
@sanitized_file.filename.should == "__foo"
|
114
|
+
end
|
115
|
+
|
116
|
+
it "should remove the path prefix on Windows" do
|
117
|
+
@sanitized_file.should_receive(:original_filename).at_least(:once).and_return('c:\temp\foo.txt')
|
118
|
+
@sanitized_file.filename.should == "foo.txt"
|
119
|
+
end
|
120
|
+
|
121
|
+
it "should make sure the *nix directory thingies can't be used as filenames" do
|
122
|
+
@sanitized_file.should_receive(:original_filename).at_least(:once).and_return(".")
|
123
|
+
@sanitized_file.filename.should == "_."
|
124
|
+
end
|
125
|
+
|
126
|
+
it "should downcase uppercase filenames" do
|
127
|
+
@sanitized_file.should_receive(:original_filename).at_least(:once).and_return("DSC4056.JPG")
|
128
|
+
@sanitized_file.filename.should == "dsc4056.jpg"
|
129
|
+
end
|
130
|
+
|
131
|
+
end
|
132
|
+
|
133
|
+
shared_examples_for "all valid sanitized files" do
|
134
|
+
|
135
|
+
describe '#empty?' do
|
136
|
+
it "should not be empty" do
|
137
|
+
@sanitized_file.should_not be_empty
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
describe '#original_filename' do
|
142
|
+
it "should return the original filename" do
|
143
|
+
@sanitized_file.original_filename.should == "llama.jpg"
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
describe '#filename' do
|
148
|
+
it "should return the filename" do
|
149
|
+
@sanitized_file.filename.should == "llama.jpg"
|
150
|
+
end
|
151
|
+
end
|
152
|
+
|
153
|
+
describe '#basename' do
|
154
|
+
it "should return the basename" do
|
155
|
+
@sanitized_file.basename.should == "llama"
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
describe '#extension' do
|
160
|
+
it "should return the extension" do
|
161
|
+
@sanitized_file.extension.should == "jpg"
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
describe "#read" do
|
166
|
+
it "should return the contents of the file" do
|
167
|
+
@sanitized_file.read.should == "this is stuff"
|
168
|
+
end
|
169
|
+
end
|
170
|
+
|
171
|
+
describe "#size" do
|
172
|
+
it "should return the size of the file" do
|
173
|
+
@sanitized_file.size.should == 13
|
174
|
+
end
|
175
|
+
end
|
176
|
+
|
177
|
+
describe '#move_to' do
|
178
|
+
|
179
|
+
after do
|
180
|
+
FileUtils.rm(file_path('gurr.png'))
|
181
|
+
end
|
182
|
+
|
183
|
+
it "should be moved to the correct location" do
|
184
|
+
@sanitized_file.move_to(file_path('gurr.png'))
|
185
|
+
|
186
|
+
File.exists?( file_path('gurr.png') ).should be_true
|
187
|
+
end
|
188
|
+
|
189
|
+
it "should have changed its path when moved" do
|
190
|
+
@sanitized_file.move_to(file_path('gurr.png'))
|
191
|
+
@sanitized_file.path.should == file_path('gurr.png')
|
192
|
+
end
|
193
|
+
|
194
|
+
it "should have changed its filename when moved" do
|
195
|
+
@sanitized_file.move_to(file_path('gurr.png'))
|
196
|
+
@sanitized_file.filename.should == 'gurr.png'
|
197
|
+
end
|
198
|
+
|
199
|
+
it "should have changed its basename when moved" do
|
200
|
+
@sanitized_file.move_to(file_path('gurr.png'))
|
201
|
+
@sanitized_file.basename.should == 'gurr'
|
202
|
+
end
|
203
|
+
|
204
|
+
it "should have changed its extension when moved" do
|
205
|
+
@sanitized_file.move_to(file_path('gurr.png'))
|
206
|
+
@sanitized_file.extension.should == 'png'
|
207
|
+
end
|
208
|
+
|
209
|
+
it "should set the right permissions" do
|
210
|
+
@sanitized_file.move_to(file_path('gurr.png'), 0755)
|
211
|
+
@sanitized_file.should have_permissions(0755)
|
212
|
+
end
|
213
|
+
|
214
|
+
end
|
215
|
+
|
216
|
+
describe '#copy_to' do
|
217
|
+
|
218
|
+
after do
|
219
|
+
FileUtils.rm(file_path('gurr.png'))
|
220
|
+
end
|
221
|
+
|
222
|
+
it "should be copied to the correct location" do
|
223
|
+
@sanitized_file.copy_to(file_path('gurr.png'))
|
224
|
+
|
225
|
+
File.exists?( file_path('gurr.png') ).should be_true
|
226
|
+
|
227
|
+
file_path('gurr.png').should be_identical_to(file_path('llama.jpg'))
|
228
|
+
end
|
229
|
+
|
230
|
+
it "should not have changed its path when copied" do
|
231
|
+
running { @sanitized_file.copy_to(file_path('gurr.png')) }.should_not change(@sanitized_file, :path)
|
232
|
+
end
|
233
|
+
|
234
|
+
it "should not have changed its filename when copied" do
|
235
|
+
running { @sanitized_file.copy_to(file_path('gurr.png')) }.should_not change(@sanitized_file, :filename)
|
236
|
+
end
|
237
|
+
|
238
|
+
it "should return an object of the same class when copied" do
|
239
|
+
new_file = @sanitized_file.copy_to(file_path('gurr.png'))
|
240
|
+
new_file.should be_an_instance_of(@sanitized_file.class)
|
241
|
+
end
|
242
|
+
|
243
|
+
it "should adjust the path of the object that is returned when copied" do
|
244
|
+
new_file = @sanitized_file.copy_to(file_path('gurr.png'))
|
245
|
+
new_file.path.should == file_path('gurr.png')
|
246
|
+
end
|
247
|
+
|
248
|
+
it "should adjust the filename of the object that is returned when copied" do
|
249
|
+
new_file = @sanitized_file.copy_to(file_path('gurr.png'))
|
250
|
+
new_file.filename.should == 'gurr.png'
|
251
|
+
end
|
252
|
+
|
253
|
+
it "should adjust the basename of the object that is returned when copied" do
|
254
|
+
new_file = @sanitized_file.copy_to(file_path('gurr.png'))
|
255
|
+
new_file.basename.should == 'gurr'
|
256
|
+
end
|
257
|
+
|
258
|
+
it "should adjust the extension of the object that is returned when copied" do
|
259
|
+
new_file = @sanitized_file.copy_to(file_path('gurr.png'))
|
260
|
+
new_file.extension.should == 'png'
|
261
|
+
end
|
262
|
+
|
263
|
+
it "should set the right permissions" do
|
264
|
+
new_file = @sanitized_file.copy_to(file_path('gurr.png'), 0755)
|
265
|
+
new_file.should have_permissions(0755)
|
266
|
+
end
|
267
|
+
|
268
|
+
end
|
269
|
+
|
270
|
+
end
|
271
|
+
|
272
|
+
shared_examples_for "all valid sanitized files that are stored on disk" do
|
273
|
+
describe '#move_to' do
|
274
|
+
it "should not raise an error when moved to its own location" do
|
275
|
+
running { @sanitized_file.move_to(@sanitized_file.path) }.should_not raise_error
|
276
|
+
end
|
277
|
+
|
278
|
+
it "should remove the original file" do
|
279
|
+
original_path = @sanitized_file.path
|
280
|
+
@sanitized_file.move_to(public_path('blah.txt'))
|
281
|
+
File.exist?(original_path).should be_false
|
282
|
+
end
|
283
|
+
end
|
284
|
+
|
285
|
+
describe '#copy_to' do
|
286
|
+
it "should return a new instance when copied to its own location" do
|
287
|
+
running {
|
288
|
+
new_file = @sanitized_file.copy_to(@sanitized_file.path)
|
289
|
+
new_file.should be_an_instance_of(@sanitized_file.class)
|
290
|
+
}.should_not raise_error
|
291
|
+
end
|
292
|
+
|
293
|
+
it "should not remove the original file" do
|
294
|
+
new_file = @sanitized_file.copy_to(public_path('blah.txt'))
|
295
|
+
File.exist?(@sanitized_file.path).should be_true
|
296
|
+
File.exist?(new_file.path).should be_true
|
297
|
+
end
|
298
|
+
end
|
299
|
+
|
300
|
+
describe '#exists?' do
|
301
|
+
it "should be true" do
|
302
|
+
@sanitized_file.exists?.should be_true
|
303
|
+
end
|
304
|
+
end
|
305
|
+
|
306
|
+
describe '#delete' do
|
307
|
+
it "should remove it from the filesystem" do
|
308
|
+
File.exists?(@sanitized_file.path).should be_true
|
309
|
+
@sanitized_file.delete
|
310
|
+
File.exists?(@sanitized_file.path).should be_false
|
311
|
+
end
|
312
|
+
end
|
313
|
+
end
|
314
|
+
|
315
|
+
describe "with a valid Hash" do
|
316
|
+
before do
|
317
|
+
@hash = {
|
318
|
+
"tempfile" => stub_merb_tempfile('llama.jpg'),
|
319
|
+
"filename" => "llama.jpg",
|
320
|
+
"content_type" => 'image/jpeg'
|
321
|
+
}
|
322
|
+
@sanitized_file = CarrierWave::SanitizedFile.new(@hash)
|
323
|
+
end
|
324
|
+
|
325
|
+
it_should_behave_like "all valid sanitized files"
|
326
|
+
|
327
|
+
it_should_behave_like "all valid sanitized files that are stored on disk"
|
328
|
+
|
329
|
+
describe '#path' do
|
330
|
+
it "should return the path of the tempfile" do
|
331
|
+
@sanitized_file.path.should_not be_nil
|
332
|
+
@sanitized_file.path.should == @hash["tempfile"].path
|
333
|
+
end
|
334
|
+
end
|
335
|
+
|
336
|
+
describe '#is_path?' do
|
337
|
+
it "should be false" do
|
338
|
+
@sanitized_file.is_path?.should be_false
|
339
|
+
end
|
340
|
+
end
|
341
|
+
|
342
|
+
end
|
343
|
+
|
344
|
+
describe "with a valid Tempfile" do
|
345
|
+
before do
|
346
|
+
@tempfile = stub_tempfile('llama.jpg', 'image/jpeg')
|
347
|
+
@sanitized_file = CarrierWave::SanitizedFile.new(@tempfile)
|
348
|
+
end
|
349
|
+
|
350
|
+
it_should_behave_like "all valid sanitized files"
|
351
|
+
|
352
|
+
it_should_behave_like "all valid sanitized files that are stored on disk"
|
353
|
+
|
354
|
+
describe '#is_path?' do
|
355
|
+
it "should be false" do
|
356
|
+
@sanitized_file.is_path?.should be_false
|
357
|
+
end
|
358
|
+
end
|
359
|
+
|
360
|
+
describe '#path' do
|
361
|
+
it "should return the path of the tempfile" do
|
362
|
+
@sanitized_file.path.should_not be_nil
|
363
|
+
@sanitized_file.path.should == @tempfile.path
|
364
|
+
end
|
365
|
+
end
|
366
|
+
|
367
|
+
end
|
368
|
+
|
369
|
+
describe "with a valid StringIO" do
|
370
|
+
before do
|
371
|
+
@sanitized_file = CarrierWave::SanitizedFile.new(stub_stringio('llama.jpg', 'image/jpeg'))
|
372
|
+
end
|
373
|
+
|
374
|
+
it_should_behave_like "all valid sanitized files"
|
375
|
+
|
376
|
+
describe '#exists?' do
|
377
|
+
it "should be false" do
|
378
|
+
@sanitized_file.exists?.should be_false
|
379
|
+
end
|
380
|
+
end
|
381
|
+
|
382
|
+
describe '#is_path?' do
|
383
|
+
it "should be false" do
|
384
|
+
@sanitized_file.is_path?.should be_false
|
385
|
+
end
|
386
|
+
end
|
387
|
+
|
388
|
+
describe '#path' do
|
389
|
+
it "should be nil" do
|
390
|
+
@sanitized_file.path.should be_nil
|
391
|
+
end
|
392
|
+
end
|
393
|
+
|
394
|
+
describe '#delete' do
|
395
|
+
it "should not raise an error" do
|
396
|
+
running { @sanitized_file.delete }.should_not raise_error
|
397
|
+
end
|
398
|
+
end
|
399
|
+
|
400
|
+
end
|
401
|
+
|
402
|
+
describe "with a valid File object" do
|
403
|
+
before do
|
404
|
+
FileUtils.cp(file_path('test.jpg'), file_path('llama.jpg'))
|
405
|
+
@sanitized_file = CarrierWave::SanitizedFile.new(stub_file('llama.jpg', 'image/jpeg'))
|
406
|
+
@sanitized_file.should_not be_empty
|
407
|
+
end
|
408
|
+
|
409
|
+
it_should_behave_like "all valid sanitized files"
|
410
|
+
|
411
|
+
it_should_behave_like "all valid sanitized files that are stored on disk"
|
412
|
+
|
413
|
+
describe '#is_path?' do
|
414
|
+
it "should be false" do
|
415
|
+
@sanitized_file.is_path?.should be_false
|
416
|
+
end
|
417
|
+
end
|
418
|
+
|
419
|
+
describe '#path' do
|
420
|
+
it "should return the path of the file" do
|
421
|
+
@sanitized_file.path.should_not be_nil
|
422
|
+
@sanitized_file.path.should == file_path('llama.jpg')
|
423
|
+
end
|
424
|
+
end
|
425
|
+
|
426
|
+
end
|
427
|
+
|
428
|
+
describe "with a valid path" do
|
429
|
+
before do
|
430
|
+
FileUtils.cp(file_path('test.jpg'), file_path('llama.jpg'))
|
431
|
+
@sanitized_file = CarrierWave::SanitizedFile.new(file_path('llama.jpg'))
|
432
|
+
@sanitized_file.should_not be_empty
|
433
|
+
end
|
434
|
+
|
435
|
+
it_should_behave_like "all valid sanitized files"
|
436
|
+
|
437
|
+
it_should_behave_like "all valid sanitized files that are stored on disk"
|
438
|
+
|
439
|
+
describe '#is_path?' do
|
440
|
+
it "should be true" do
|
441
|
+
@sanitized_file.is_path?.should be_true
|
442
|
+
end
|
443
|
+
end
|
444
|
+
|
445
|
+
describe '#path' do
|
446
|
+
it "should return the path of the file" do
|
447
|
+
@sanitized_file.path.should_not be_nil
|
448
|
+
@sanitized_file.path.should == file_path('llama.jpg')
|
449
|
+
end
|
450
|
+
end
|
451
|
+
|
452
|
+
end
|
453
|
+
|
454
|
+
describe "with a valid Pathname" do
|
455
|
+
before do
|
456
|
+
FileUtils.copy_file(file_path('test.jpg'), file_path('llama.jpg'))
|
457
|
+
@sanitized_file = CarrierWave::SanitizedFile.new(Pathname.new(file_path('llama.jpg')))
|
458
|
+
@sanitized_file.should_not be_empty
|
459
|
+
end
|
460
|
+
|
461
|
+
it_should_behave_like "all valid sanitized files"
|
462
|
+
|
463
|
+
it_should_behave_like "all valid sanitized files that are stored on disk"
|
464
|
+
|
465
|
+
describe '#is_path?' do
|
466
|
+
it "should be true" do
|
467
|
+
@sanitized_file.is_path?.should be_true
|
468
|
+
end
|
469
|
+
end
|
470
|
+
|
471
|
+
describe '#path' do
|
472
|
+
it "should return the path of the file" do
|
473
|
+
@sanitized_file.path.should_not be_nil
|
474
|
+
@sanitized_file.path.should == file_path('llama.jpg')
|
475
|
+
end
|
476
|
+
end
|
477
|
+
|
478
|
+
end
|
479
|
+
|
480
|
+
describe "that is empty" do
|
481
|
+
before do
|
482
|
+
@empty = CarrierWave::SanitizedFile.new(nil)
|
483
|
+
end
|
484
|
+
|
485
|
+
describe '#empty?' do
|
486
|
+
it "should be true" do
|
487
|
+
@empty.should be_empty
|
488
|
+
end
|
489
|
+
end
|
490
|
+
|
491
|
+
describe '#exists?' do
|
492
|
+
it "should be false" do
|
493
|
+
@empty.exists?.should be_false
|
494
|
+
end
|
495
|
+
end
|
496
|
+
|
497
|
+
describe '#is_path?' do
|
498
|
+
it "should be false" do
|
499
|
+
@empty.is_path?.should be_false
|
500
|
+
end
|
501
|
+
end
|
502
|
+
|
503
|
+
describe '#size' do
|
504
|
+
it "should be zero" do
|
505
|
+
@empty.size.should be_zero
|
506
|
+
end
|
507
|
+
end
|
508
|
+
|
509
|
+
describe '#path' do
|
510
|
+
it "should be nil" do
|
511
|
+
@empty.path.should be_nil
|
512
|
+
end
|
513
|
+
end
|
514
|
+
|
515
|
+
describe '#original_filename' do
|
516
|
+
it "should be nil" do
|
517
|
+
@empty.original_filename.should be_nil
|
518
|
+
end
|
519
|
+
end
|
520
|
+
|
521
|
+
describe '#filename' do
|
522
|
+
it "should be nil" do
|
523
|
+
@empty.filename.should be_nil
|
524
|
+
end
|
525
|
+
end
|
526
|
+
|
527
|
+
describe '#basename' do
|
528
|
+
it "should be nil" do
|
529
|
+
@empty.basename.should be_nil
|
530
|
+
end
|
531
|
+
end
|
532
|
+
|
533
|
+
describe '#extension' do
|
534
|
+
it "should be nil" do
|
535
|
+
@empty.extension.should be_nil
|
536
|
+
end
|
537
|
+
end
|
538
|
+
|
539
|
+
describe '#delete' do
|
540
|
+
it "should not raise an error" do
|
541
|
+
running { @empty.delete }.should_not raise_error
|
542
|
+
end
|
543
|
+
end
|
544
|
+
end
|
545
|
+
|
546
|
+
describe "that is an empty string" do
|
547
|
+
before do
|
548
|
+
@empty = CarrierWave::SanitizedFile.new("")
|
549
|
+
end
|
550
|
+
|
551
|
+
describe '#empty?' do
|
552
|
+
it "should be true" do
|
553
|
+
@empty.should be_empty
|
554
|
+
end
|
555
|
+
end
|
556
|
+
|
557
|
+
describe '#exists?' do
|
558
|
+
it "should be false" do
|
559
|
+
@empty.exists?.should be_false
|
560
|
+
end
|
561
|
+
end
|
562
|
+
|
563
|
+
describe '#is_path?' do
|
564
|
+
it "should be false" do
|
565
|
+
@empty.is_path?.should be_false
|
566
|
+
end
|
567
|
+
end
|
568
|
+
|
569
|
+
describe '#size' do
|
570
|
+
it "should be zero" do
|
571
|
+
@empty.size.should be_zero
|
572
|
+
end
|
573
|
+
end
|
574
|
+
|
575
|
+
describe '#path' do
|
576
|
+
it "should be nil" do
|
577
|
+
@empty.path.should be_nil
|
578
|
+
end
|
579
|
+
end
|
580
|
+
|
581
|
+
describe '#original_filename' do
|
582
|
+
it "should be nil" do
|
583
|
+
@empty.original_filename.should be_nil
|
584
|
+
end
|
585
|
+
end
|
586
|
+
|
587
|
+
describe '#filename' do
|
588
|
+
it "should be nil" do
|
589
|
+
@empty.filename.should be_nil
|
590
|
+
end
|
591
|
+
end
|
592
|
+
|
593
|
+
describe '#basename' do
|
594
|
+
it "should be nil" do
|
595
|
+
@empty.basename.should be_nil
|
596
|
+
end
|
597
|
+
end
|
598
|
+
|
599
|
+
describe '#extension' do
|
600
|
+
it "should be nil" do
|
601
|
+
@empty.extension.should be_nil
|
602
|
+
end
|
603
|
+
end
|
604
|
+
|
605
|
+
describe '#delete' do
|
606
|
+
it "should not raise an error" do
|
607
|
+
running { @empty.delete }.should_not raise_error
|
608
|
+
end
|
609
|
+
end
|
610
|
+
end
|
611
|
+
|
612
|
+
end
|