jnicklas-carrierwave 0.2.3 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/Rakefile +1 -1
- data/lib/carrierwave/orm/activerecord.rb +4 -0
- data/lib/carrierwave/processing/rmagick.rb +37 -4
- data/lib/carrierwave/storage/file.rb +3 -22
- data/lib/carrierwave/storage/s3.rb +23 -5
- data/lib/carrierwave/uploader/remove.rb +1 -1
- data/spec/orm/activerecord_spec.rb +19 -1
- data/spec/spec_helper.rb +1 -1
- data/spec/uploader/remove_spec.rb +3 -3
- data/spec/uploader/versions_spec.rb +5 -8
- metadata +2 -2
data/Rakefile
CHANGED
@@ -88,6 +88,33 @@ module CarrierWave
|
|
88
88
|
end
|
89
89
|
end
|
90
90
|
|
91
|
+
##
|
92
|
+
# Resize the image to fit within the specified dimensions while retaining
|
93
|
+
# the original aspect ratio. Will only resize the image if it is larger than the
|
94
|
+
# specified dimensions. The resulting image may be shorter or narrower than specified
|
95
|
+
# in the smaller dimension but will not be larger than the specified values.
|
96
|
+
#
|
97
|
+
# === Parameters
|
98
|
+
#
|
99
|
+
# [width (Integer)] the width to scale the image to
|
100
|
+
# [height (Integer)] the height to scale the image to
|
101
|
+
#
|
102
|
+
# === Yields
|
103
|
+
#
|
104
|
+
# [Magick::Image] additional manipulations to perform
|
105
|
+
#
|
106
|
+
def resize_to_limit(width, height)
|
107
|
+
manipulate! do |img|
|
108
|
+
geometry = Magick::Geometry.new(width, height, 0, 0, Magick::GreaterGeometry)
|
109
|
+
new_img = img.change_geometry(geometry) do |new_width, new_height|
|
110
|
+
img.resize(new_width, new_height)
|
111
|
+
end
|
112
|
+
destroy_image(img)
|
113
|
+
new_img = yield(new_img) if block_given?
|
114
|
+
new_img
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
91
118
|
##
|
92
119
|
# From the RMagick documentation: "Resize the image to fit within the
|
93
120
|
# specified dimensions while retaining the original aspect ratio. The
|
@@ -167,9 +194,9 @@ module CarrierWave
|
|
167
194
|
else
|
168
195
|
filled = new_img.color_floodfill(1, 1, ::Magick::Pixel.from_color(background))
|
169
196
|
end
|
170
|
-
new_img
|
197
|
+
destroy_image(new_img)
|
171
198
|
filled.composite!(img, gravity, ::Magick::OverCompositeOp)
|
172
|
-
img
|
199
|
+
destroy_image(img)
|
173
200
|
filled = yield(filled) if block_given?
|
174
201
|
filled
|
175
202
|
end
|
@@ -204,15 +231,21 @@ module CarrierWave
|
|
204
231
|
list << yield( frame )
|
205
232
|
end
|
206
233
|
list.write(current_path)
|
207
|
-
list
|
234
|
+
destroy_image(list)
|
208
235
|
else
|
209
236
|
frame = image.first
|
210
237
|
yield( frame ).write(current_path)
|
211
|
-
frame
|
238
|
+
destroy_image(frame)
|
212
239
|
end
|
213
240
|
rescue ::Magick::ImageMagickError => e
|
214
241
|
raise CarrierWave::ProcessingError.new("Failed to manipulate with rmagick, maybe it is not an image? Original Error: #{e}")
|
215
242
|
end
|
216
243
|
|
244
|
+
private
|
245
|
+
|
246
|
+
def destroy_image(image)
|
247
|
+
image.destroy! if image.respond_to?(:destroy!)
|
248
|
+
end
|
249
|
+
|
217
250
|
end # RMagick
|
218
251
|
end # CarrierWave
|
@@ -7,29 +7,10 @@ module CarrierWave
|
|
7
7
|
# pretty much it.
|
8
8
|
#
|
9
9
|
class File < Abstract
|
10
|
-
|
10
|
+
|
11
11
|
def initialize(uploader)
|
12
12
|
@uploader = uploader
|
13
13
|
end
|
14
|
-
|
15
|
-
##
|
16
|
-
# Delete the file to the uploader's store path.
|
17
|
-
#
|
18
|
-
# === Parameters
|
19
|
-
#
|
20
|
-
# [uploader (CarrierWave::Uploader)] an uploader object
|
21
|
-
# [file (CarrierWave::SanitizedFile)] the file to store
|
22
|
-
#
|
23
|
-
# === Returns
|
24
|
-
#
|
25
|
-
# [bool] True if file was removed or false
|
26
|
-
#
|
27
|
-
def self.destroy!(uploader, file)
|
28
|
-
unless file.blank?
|
29
|
-
CarrierWave.logger.info "CarrierWave::Storage::File: removing file #{file.path}"
|
30
|
-
file.delete
|
31
|
-
end
|
32
|
-
end
|
33
14
|
|
34
15
|
##
|
35
16
|
# Move the file to the uploader's store path.
|
@@ -49,7 +30,7 @@ module CarrierWave
|
|
49
30
|
file.move_to(path, CarrierWave.config[:permissions])
|
50
31
|
file
|
51
32
|
end
|
52
|
-
|
33
|
+
|
53
34
|
##
|
54
35
|
# Retrieve the file from its store path
|
55
36
|
#
|
@@ -67,7 +48,7 @@ module CarrierWave
|
|
67
48
|
path = ::File.expand_path(path, uploader.public)
|
68
49
|
CarrierWave::SanitizedFile.new(path)
|
69
50
|
end
|
70
|
-
|
51
|
+
|
71
52
|
end # File
|
72
53
|
end # Storage
|
73
54
|
end # CarrierWave
|
@@ -29,8 +29,8 @@ module CarrierWave
|
|
29
29
|
#
|
30
30
|
class S3 < Abstract
|
31
31
|
|
32
|
-
def initialize(
|
33
|
-
@
|
32
|
+
def initialize(path, identifier)
|
33
|
+
@path = path
|
34
34
|
@identifier = identifier
|
35
35
|
end
|
36
36
|
|
@@ -95,7 +95,18 @@ module CarrierWave
|
|
95
95
|
def self.retrieve!(uploader, identifier)
|
96
96
|
self.new(uploader.store_path(identifier), identifier)
|
97
97
|
end
|
98
|
-
|
98
|
+
|
99
|
+
##
|
100
|
+
# Returns the current path of the file on S3
|
101
|
+
#
|
102
|
+
# === Returns
|
103
|
+
#
|
104
|
+
# [String] A path
|
105
|
+
#
|
106
|
+
def path
|
107
|
+
@path
|
108
|
+
end
|
109
|
+
|
99
110
|
##
|
100
111
|
# Returns the filename on S3
|
101
112
|
#
|
@@ -115,7 +126,14 @@ module CarrierWave
|
|
115
126
|
# [String] contents of the file
|
116
127
|
#
|
117
128
|
def read
|
118
|
-
S3Object.value @
|
129
|
+
AWS::S3::S3Object.value @path, self.class.bucket
|
130
|
+
end
|
131
|
+
|
132
|
+
##
|
133
|
+
# Remove the file from Amazon S3
|
134
|
+
#
|
135
|
+
def delete
|
136
|
+
AWS::S3::S3Object.delete @path, self.class.bucket
|
119
137
|
end
|
120
138
|
|
121
139
|
##
|
@@ -126,7 +144,7 @@ module CarrierWave
|
|
126
144
|
# [String] file's url
|
127
145
|
#
|
128
146
|
def url
|
129
|
-
["http://s3.amazonaws.com", self.class.bucket, @
|
147
|
+
["http://s3.amazonaws.com", self.class.bucket, @path].compact.join('/')
|
130
148
|
end
|
131
149
|
|
132
150
|
end # S3
|
@@ -167,7 +167,25 @@ describe CarrierWave::ActiveRecord do
|
|
167
167
|
end
|
168
168
|
|
169
169
|
end
|
170
|
-
|
170
|
+
|
171
|
+
describe '#destroy' do
|
172
|
+
|
173
|
+
it "should do nothing when no file has been assigned" do
|
174
|
+
@event.save.should be_true
|
175
|
+
@event.destroy
|
176
|
+
end
|
177
|
+
|
178
|
+
it "should remove the file from the filesystem" do
|
179
|
+
@event.image = stub_file('test.jpeg')
|
180
|
+
@event.save.should be_true
|
181
|
+
@event.image.should be_an_instance_of(@uploader)
|
182
|
+
@event.image.current_path.should == public_path('uploads/test.jpeg')
|
183
|
+
@event.destroy
|
184
|
+
File.exist?(public_path('uploads/test.jpeg')).should be_false
|
185
|
+
end
|
186
|
+
|
187
|
+
end
|
188
|
+
|
171
189
|
describe 'with overriddent filename' do
|
172
190
|
|
173
191
|
describe '#save' do
|
data/spec/spec_helper.rb
CHANGED
@@ -19,9 +19,9 @@ describe CarrierWave::Uploader do
|
|
19
19
|
@stored_file.stub!(:path).and_return('/path/to/somewhere')
|
20
20
|
@stored_file.stub!(:url).and_return('http://www.example.com')
|
21
21
|
@stored_file.stub!(:identifier).and_return('this-is-me')
|
22
|
+
@stored_file.stub!(:delete)
|
22
23
|
|
23
24
|
@uploader_class.storage.stub!(:store!).and_return(@stored_file)
|
24
|
-
@uploader_class.storage.stub!(:destroy!)
|
25
25
|
@uploader.store!(@file)
|
26
26
|
end
|
27
27
|
|
@@ -46,8 +46,8 @@ describe CarrierWave::Uploader do
|
|
46
46
|
@uploader.identifier.should be_nil
|
47
47
|
end
|
48
48
|
|
49
|
-
it "should
|
50
|
-
@
|
49
|
+
it "should delete the file" do
|
50
|
+
@stored_file.should_receive(:delete)
|
51
51
|
@uploader.remove!
|
52
52
|
end
|
53
53
|
|
@@ -206,11 +206,8 @@ describe CarrierWave::Uploader do
|
|
206
206
|
@uploader_class.storage.stub!(:store!).and_return(@base_stored_file)
|
207
207
|
@uploader_class.version(:thumb).storage.stub!(:store!).and_return(@thumb_stored_file)
|
208
208
|
|
209
|
-
@
|
210
|
-
@
|
211
|
-
|
212
|
-
@uploader_class.storage.stub!(:destroy!)
|
213
|
-
@uploader_class.version(:thumb).storage.stub!(:destroy!)
|
209
|
+
@base_stored_file.stub!(:delete)
|
210
|
+
@thumb_stored_file.stub!(:delete)
|
214
211
|
|
215
212
|
@uploader.store!(@file)
|
216
213
|
end
|
@@ -231,9 +228,9 @@ describe CarrierWave::Uploader do
|
|
231
228
|
@uploader.thumb.url.should be_nil
|
232
229
|
end
|
233
230
|
|
234
|
-
it "should
|
235
|
-
@
|
236
|
-
@
|
231
|
+
it "should delete all the files" do
|
232
|
+
@base_stored_file.should_receive(:delete)
|
233
|
+
@thumb_stored_file.should_receive(:delete)
|
237
234
|
@uploader.remove!
|
238
235
|
end
|
239
236
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jnicklas-carrierwave
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jonas Nicklas
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-
|
12
|
+
date: 2009-06-11 00:00:00 -07:00
|
13
13
|
default_executable:
|
14
14
|
dependencies: []
|
15
15
|
|