jnicklas-carrierwave 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/Generators +4 -0
- data/LICENSE +20 -0
- data/README.md +211 -0
- data/Rakefile +96 -0
- data/TODO +0 -0
- data/lib/carrierwave/mount.rb +93 -0
- data/lib/carrierwave/orm/activerecord.rb +20 -0
- data/lib/carrierwave/orm/datamapper.rb +20 -0
- data/lib/carrierwave/processing/image_science.rb +70 -0
- data/lib/carrierwave/processing/rmagick.rb +161 -0
- data/lib/carrierwave/sanitized_file.rb +231 -0
- data/lib/carrierwave/storage/abstract.rb +80 -0
- data/lib/carrierwave/storage/file.rb +40 -0
- data/lib/carrierwave/storage/s3.rb +83 -0
- data/lib/carrierwave/uploader.rb +420 -0
- data/lib/carrierwave.rb +63 -0
- data/lib/generators/templates/uploader.rbt +32 -0
- data/lib/generators/uploader_generator.rb +20 -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 +180 -0
- data/spec/orm/activerecord_spec.rb +168 -0
- data/spec/orm/datamapper_spec.rb +133 -0
- data/spec/sanitized_file_spec.rb +618 -0
- data/spec/spec_helper.rb +122 -0
- data/spec/uploader_spec.rb +709 -0
- metadata +89 -0
data/spec/mount_spec.rb
ADDED
@@ -0,0 +1,180 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe CarrierWave::Mount do
|
4
|
+
|
5
|
+
include SanitizedFileSpecHelper
|
6
|
+
|
7
|
+
after do
|
8
|
+
FileUtils.rm_rf(public_path)
|
9
|
+
end
|
10
|
+
|
11
|
+
describe '.mount_uploader' do
|
12
|
+
|
13
|
+
before do
|
14
|
+
@class = Class.new
|
15
|
+
@class.send(:extend, CarrierWave::Mount)
|
16
|
+
|
17
|
+
@uploader = Class.new(CarrierWave::Uploader)
|
18
|
+
|
19
|
+
@class.mount_uploader(:image, @uploader)
|
20
|
+
@instance = @class.new
|
21
|
+
end
|
22
|
+
|
23
|
+
describe '#image' do
|
24
|
+
|
25
|
+
it "should return nil when nothing has been assigned" do
|
26
|
+
@instance.should_receive(:read_uploader).with(:image).and_return(nil)
|
27
|
+
@instance.image.should be_nil
|
28
|
+
end
|
29
|
+
|
30
|
+
it "should return nil when an empty string has been assigned" do
|
31
|
+
@instance.should_receive(:read_uploader).with(:image).and_return('')
|
32
|
+
@instance.image.should be_nil
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should retrieve a file from the storage if a value is stored in the database" do
|
36
|
+
@instance.should_receive(:read_uploader).with(:image).and_return('test.jpg')
|
37
|
+
@instance.image.should be_an_instance_of(@uploader)
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should set the path to the store dir" do
|
41
|
+
@instance.should_receive(:read_uploader).with(:image).and_return('test.jpg')
|
42
|
+
@instance.image.current_path.should == public_path('uploads/test.jpg')
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
describe '#image=' do
|
48
|
+
|
49
|
+
it "should cache a file" do
|
50
|
+
@instance.image = stub_file('test.jpg')
|
51
|
+
@instance.image.should be_an_instance_of(@uploader)
|
52
|
+
end
|
53
|
+
|
54
|
+
it "should copy a file into into the cache directory" do
|
55
|
+
@instance.image = stub_file('test.jpg')
|
56
|
+
@instance.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
|
57
|
+
end
|
58
|
+
|
59
|
+
it "should do nothing when nil is assigned" do
|
60
|
+
@instance.should_not_receive(:write_uploader)
|
61
|
+
@instance.image = nil
|
62
|
+
end
|
63
|
+
|
64
|
+
it "should do nothing when an empty string is assigned" do
|
65
|
+
@instance.should_not_receive(:write_uploader)
|
66
|
+
@instance.image = ''
|
67
|
+
end
|
68
|
+
|
69
|
+
end
|
70
|
+
|
71
|
+
describe '#image_cache' do
|
72
|
+
|
73
|
+
before do
|
74
|
+
@instance.stub!(:write_uploader)
|
75
|
+
@instance.stub!(:read_uploader).and_return(nil)
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should return nil when nothing has been assigned" do
|
79
|
+
@instance.image_cache.should be_nil
|
80
|
+
end
|
81
|
+
|
82
|
+
it "should be nil when a file has been stored" do
|
83
|
+
@instance.image = stub_file('test.jpg')
|
84
|
+
@instance.image.store!
|
85
|
+
@instance.image_cache.should be_nil
|
86
|
+
end
|
87
|
+
|
88
|
+
it "should be the cache name when a file has been cached" do
|
89
|
+
@instance.image = stub_file('test.jpg')
|
90
|
+
@instance.image_cache.should =~ %r(^[\d]{8}\-[\d]{4}\-[\d]+\-[\d]{4}/test\.jpg$)
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
describe '#image_cache=' do
|
96
|
+
|
97
|
+
before do
|
98
|
+
@instance.stub!(:write_uploader)
|
99
|
+
@instance.stub!(:read_uploader).and_return(nil)
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should do nothing when nil is assigned" do
|
103
|
+
@instance.image_cache = nil
|
104
|
+
@instance.image.should be_nil
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should do nothing when an empty string is assigned" do
|
108
|
+
@instance.image_cache = ''
|
109
|
+
@instance.image.should be_nil
|
110
|
+
end
|
111
|
+
|
112
|
+
it "retrieve from cache when a cache name is assigned" do
|
113
|
+
@instance.image_cache = '19990512-1202-123-1234/test.jpg'
|
114
|
+
@instance.image.current_path.should == public_path('uploads/tmp/19990512-1202-123-1234/test.jpg')
|
115
|
+
end
|
116
|
+
|
117
|
+
it "should not write over a previously assigned file" do
|
118
|
+
@instance.image = stub_file('test.jpg')
|
119
|
+
@instance.image_cache = '19990512-1202-123-1234/monkey.jpg'
|
120
|
+
@instance.image.current_path.should =~ /test.jpg$/
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
describe '#store_image!' do
|
125
|
+
|
126
|
+
before do
|
127
|
+
@instance.stub!(:write_uploader)
|
128
|
+
@instance.stub!(:read_uploader).and_return(nil)
|
129
|
+
end
|
130
|
+
|
131
|
+
it "should do nothing when no file has been uploaded" do
|
132
|
+
@instance.store_image!
|
133
|
+
@instance.image.should be_nil
|
134
|
+
end
|
135
|
+
|
136
|
+
it "store an assigned file" do
|
137
|
+
@instance.image = stub_file('test.jpg')
|
138
|
+
@instance.store_image!
|
139
|
+
@instance.image.current_path.should == public_path('uploads/test.jpg')
|
140
|
+
end
|
141
|
+
end
|
142
|
+
|
143
|
+
end
|
144
|
+
|
145
|
+
describe '#mount_uploader with a block' do
|
146
|
+
|
147
|
+
before do
|
148
|
+
@class = Class.new
|
149
|
+
@class.send(:extend, CarrierWave::Mount)
|
150
|
+
@class.mount_uploader(:image) do
|
151
|
+
def monkey
|
152
|
+
'blah'
|
153
|
+
end
|
154
|
+
end
|
155
|
+
@instance = @class.new
|
156
|
+
end
|
157
|
+
|
158
|
+
describe '#image' do
|
159
|
+
|
160
|
+
before do
|
161
|
+
@instance.stub!(:read_uploader).and_return('test.jpg')
|
162
|
+
end
|
163
|
+
|
164
|
+
it "should return an instance of a subclass of CarrierWave::Uploader" do
|
165
|
+
@instance.image.should be_a(CarrierWave::Uploader)
|
166
|
+
end
|
167
|
+
|
168
|
+
it "should set the path to the store dir" do
|
169
|
+
@instance.image.current_path.should == public_path('uploads/test.jpg')
|
170
|
+
end
|
171
|
+
|
172
|
+
it "should apply any custom modifications" do
|
173
|
+
@instance.image.monkey.should == "blah"
|
174
|
+
end
|
175
|
+
|
176
|
+
end
|
177
|
+
|
178
|
+
end
|
179
|
+
|
180
|
+
end
|
@@ -0,0 +1,168 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
require 'carrierwave/orm/activerecord'
|
4
|
+
|
5
|
+
# change this if sqlite is unavailable
|
6
|
+
dbconfig = {
|
7
|
+
:adapter => 'sqlite3',
|
8
|
+
:database => ':memory:'
|
9
|
+
}
|
10
|
+
|
11
|
+
ActiveRecord::Base.establish_connection(dbconfig)
|
12
|
+
ActiveRecord::Migration.verbose = false
|
13
|
+
|
14
|
+
class TestMigration < ActiveRecord::Migration
|
15
|
+
def self.up
|
16
|
+
create_table :events, :force => true do |t|
|
17
|
+
t.column :image, :string
|
18
|
+
t.column :textfile, :string
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.down
|
23
|
+
drop_table :events
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class Event < ActiveRecord::Base; end # setup a basic AR class for testing
|
28
|
+
|
29
|
+
describe CarrierWave::ActiveRecord do
|
30
|
+
|
31
|
+
include SanitizedFileSpecHelper
|
32
|
+
|
33
|
+
describe '.mount_uploader' do
|
34
|
+
|
35
|
+
before(:all) { TestMigration.up }
|
36
|
+
after(:all) { TestMigration.down }
|
37
|
+
after { Event.delete_all }
|
38
|
+
|
39
|
+
before do
|
40
|
+
@class = Class.new(ActiveRecord::Base)
|
41
|
+
@class.table_name = "events"
|
42
|
+
@uploader = Class.new(CarrierWave::Uploader)
|
43
|
+
@class.mount_uploader(:image, @uploader)
|
44
|
+
@event = @class.new
|
45
|
+
end
|
46
|
+
|
47
|
+
describe '#image' do
|
48
|
+
|
49
|
+
it "should return nil when nothing has been assigned" do
|
50
|
+
@event.image.should be_nil
|
51
|
+
end
|
52
|
+
|
53
|
+
it "should return nil when an empty string has been assigned" do
|
54
|
+
@event[:image] = ''
|
55
|
+
@event.save
|
56
|
+
@event.reload
|
57
|
+
@event.image.should be_nil
|
58
|
+
end
|
59
|
+
|
60
|
+
it "should retrieve a file from the storage if a value is stored in the database" do
|
61
|
+
@event[:image] = 'test.jpeg'
|
62
|
+
@event.save
|
63
|
+
@event.reload
|
64
|
+
@event.image.should be_an_instance_of(@uploader)
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should set the path to the store dir" do
|
68
|
+
@event[:image] = 'test.jpeg'
|
69
|
+
@event.save
|
70
|
+
@event.reload
|
71
|
+
@event.image.current_path.should == public_path('uploads/test.jpeg')
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
describe '#image=' do
|
77
|
+
|
78
|
+
it "should cache a file" do
|
79
|
+
@event.image = stub_file('test.jpeg')
|
80
|
+
@event.image.should be_an_instance_of(@uploader)
|
81
|
+
end
|
82
|
+
|
83
|
+
it "should write nothing to the database, to prevent overriden filenames to fail because of unassigned attributes" do
|
84
|
+
@event[:image].should be_nil
|
85
|
+
end
|
86
|
+
|
87
|
+
it "should copy a file into into the cache directory" do
|
88
|
+
@event.image = stub_file('test.jpeg')
|
89
|
+
@event.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
|
90
|
+
end
|
91
|
+
|
92
|
+
it "should do nothing when nil is assigned" do
|
93
|
+
@event.image = nil
|
94
|
+
@event.image.should be_nil
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should do nothing when an empty string is assigned" do
|
98
|
+
@event.image = ''
|
99
|
+
@event.image.should be_nil
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
103
|
+
|
104
|
+
describe '#save' do
|
105
|
+
|
106
|
+
it "should do nothing when no file has been assigned" do
|
107
|
+
@event.save.should be_true
|
108
|
+
@event.image.should be_nil
|
109
|
+
end
|
110
|
+
|
111
|
+
it "should copy the file to the upload directory when a file has been assigned" do
|
112
|
+
@event.image = stub_file('test.jpeg')
|
113
|
+
@event.save.should be_true
|
114
|
+
@event.image.should be_an_instance_of(@uploader)
|
115
|
+
@event.image.current_path.should == public_path('uploads/test.jpeg')
|
116
|
+
end
|
117
|
+
|
118
|
+
it "should do nothing when a validation fails" do
|
119
|
+
@class.validate { |r| r.errors.add :textfile, "FAIL!" }
|
120
|
+
@event.image = stub_file('test.jpeg')
|
121
|
+
@event.save.should be_false
|
122
|
+
@event.image.should be_an_instance_of(@uploader)
|
123
|
+
@event.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
|
124
|
+
end
|
125
|
+
|
126
|
+
it "should assign the filename to the database" do
|
127
|
+
@event.image = stub_file('test.jpeg')
|
128
|
+
@event.save.should be_true
|
129
|
+
@event.reload
|
130
|
+
@event[:image].should == 'test.jpeg'
|
131
|
+
end
|
132
|
+
|
133
|
+
end
|
134
|
+
|
135
|
+
describe 'with overriddent filename' do
|
136
|
+
|
137
|
+
describe '#save' do
|
138
|
+
|
139
|
+
before do
|
140
|
+
@uploader.class_eval do
|
141
|
+
def filename
|
142
|
+
model.name + File.extname(super)
|
143
|
+
end
|
144
|
+
end
|
145
|
+
@event.stub!(:name).and_return('jonas')
|
146
|
+
end
|
147
|
+
|
148
|
+
it "should copy the file to the upload directory when a file has been assigned" do
|
149
|
+
@event.image = stub_file('test.jpeg')
|
150
|
+
@event.save.should be_true
|
151
|
+
@event.image.should be_an_instance_of(@uploader)
|
152
|
+
@event.image.current_path.should == public_path('uploads/jonas.jpeg')
|
153
|
+
end
|
154
|
+
|
155
|
+
it "should assign an overridden filename to the database" do
|
156
|
+
@event.image = stub_file('test.jpeg')
|
157
|
+
@event.save.should be_true
|
158
|
+
@event.reload
|
159
|
+
@event[:image].should == 'jonas.jpeg'
|
160
|
+
end
|
161
|
+
|
162
|
+
end
|
163
|
+
|
164
|
+
end
|
165
|
+
|
166
|
+
end
|
167
|
+
|
168
|
+
end
|
@@ -0,0 +1,133 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
require 'carrierwave/orm/datamapper'
|
4
|
+
|
5
|
+
DataMapper.setup(:default, 'sqlite3::memory:')
|
6
|
+
|
7
|
+
describe CarrierWave::DataMapper do
|
8
|
+
|
9
|
+
include SanitizedFileSpecHelper
|
10
|
+
|
11
|
+
before do
|
12
|
+
uploader = Class.new(CarrierWave::Uploader)
|
13
|
+
|
14
|
+
@class = Class.new
|
15
|
+
@class.class_eval do
|
16
|
+
include DataMapper::Resource
|
17
|
+
|
18
|
+
storage_names[:default] = 'events'
|
19
|
+
|
20
|
+
property :id, Integer, :key => true
|
21
|
+
property :image, String
|
22
|
+
|
23
|
+
mount_uploader :image, uploader
|
24
|
+
end
|
25
|
+
|
26
|
+
@class.auto_migrate!
|
27
|
+
|
28
|
+
@uploader = uploader
|
29
|
+
|
30
|
+
@event = @class.new
|
31
|
+
end
|
32
|
+
|
33
|
+
describe '#image' do
|
34
|
+
|
35
|
+
it "should return nil when nothing has been assigned" do
|
36
|
+
@event.image.should be_nil
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should return nil when an empty string has been assigned" do
|
40
|
+
repository(:default).adapter.query("INSERT INTO events (image) VALUES ('')")
|
41
|
+
@event = @class.first
|
42
|
+
|
43
|
+
@event.image.should be_nil
|
44
|
+
end
|
45
|
+
|
46
|
+
it "should retrieve a file from the storage if a value is stored in the database" do
|
47
|
+
repository(:default).adapter.query("INSERT INTO events (image) VALUES ('test.jpg')")
|
48
|
+
@event = @class.first
|
49
|
+
|
50
|
+
@event.save
|
51
|
+
@event.reload
|
52
|
+
@event.image.should be_an_instance_of(@uploader)
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should set the path to the store dir" do
|
56
|
+
@event.attribute_set(:image, 'test.jpeg')
|
57
|
+
@event.save
|
58
|
+
@event.reload
|
59
|
+
@event.image.current_path.should == public_path('uploads/test.jpeg')
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
describe '#image=' do
|
65
|
+
|
66
|
+
it "should cache a file" do
|
67
|
+
@event.image = stub_file('test.jpeg')
|
68
|
+
@event.image.should be_an_instance_of(@uploader)
|
69
|
+
end
|
70
|
+
|
71
|
+
it "should write nothing to the database, to prevent overriden filenames to fail because of unassigned attributes" do
|
72
|
+
@event.attribute_get(:image).should be_nil
|
73
|
+
end
|
74
|
+
|
75
|
+
it "should copy a file into into the cache directory" do
|
76
|
+
@event.image = stub_file('test.jpeg')
|
77
|
+
@event.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
|
78
|
+
end
|
79
|
+
|
80
|
+
it "should do nothing when nil is assigned" do
|
81
|
+
@event.image = nil
|
82
|
+
@event.image.should be_nil
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should do nothing when an empty string is assigned" do
|
86
|
+
@event.image = ''
|
87
|
+
@event.image.should be_nil
|
88
|
+
end
|
89
|
+
|
90
|
+
end
|
91
|
+
|
92
|
+
describe '#save' do
|
93
|
+
|
94
|
+
it "should do nothing when no file has been assigned" do
|
95
|
+
@event.save
|
96
|
+
@event.image.should be_nil
|
97
|
+
end
|
98
|
+
|
99
|
+
it "should copy the file to the upload directory when a file has been assigned" do
|
100
|
+
@event.image = stub_file('test.jpeg')
|
101
|
+
@event.save
|
102
|
+
@event.image.should be_an_instance_of(@uploader)
|
103
|
+
@event.image.current_path.should == public_path('uploads/test.jpeg')
|
104
|
+
end
|
105
|
+
|
106
|
+
it "should do nothing when a validation fails" do
|
107
|
+
pending "how do we test with and without dm-validations?"
|
108
|
+
@class.validate { |r| r.errors.add :textfile, "FAIL!" }
|
109
|
+
@event.image = stub_file('test.jpeg')
|
110
|
+
@event.save
|
111
|
+
@event.image.should be_an_instance_of(@uploader)
|
112
|
+
@event.image.current_path.should =~ /^#{public_path('uploads/tmp')}/
|
113
|
+
end
|
114
|
+
|
115
|
+
it "should assign the filename to the database" do
|
116
|
+
@event.image = stub_file('test.jpeg')
|
117
|
+
@event.save
|
118
|
+
@event.reload
|
119
|
+
@event.attribute_get(:image).should == 'test.jpeg'
|
120
|
+
end
|
121
|
+
|
122
|
+
it "should assign the filename before validation" do
|
123
|
+
pending "how do we test with and without dm-validations?"
|
124
|
+
@class.validate { |r| r.errors.add_to_base "FAIL!" if r[:image].nil? }
|
125
|
+
@event.image = stub_file('test.jpeg')
|
126
|
+
@event.save
|
127
|
+
@event.reload
|
128
|
+
@event.attribute_get(:image).should == 'test.jpeg'
|
129
|
+
end
|
130
|
+
|
131
|
+
end
|
132
|
+
|
133
|
+
end
|