file_column_with_s3 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +69 -0
- data/README +54 -0
- data/Rakefile +40 -0
- data/lib/file_column.rb +734 -0
- data/lib/file_column/attachement_store.rb +93 -0
- data/lib/file_column_helper.rb +150 -0
- data/lib/file_compat.rb +28 -0
- data/lib/magick_file_column.rb +265 -0
- data/lib/rails_file_column.rb +19 -0
- data/lib/test_case.rb +124 -0
- data/lib/validations.rb +112 -0
- data/s3_env.example +5 -0
- data/test/abstract_unit.rb +66 -0
- data/test/attachement_store_test.rb +96 -0
- data/test/connection.rb +11 -0
- data/test/file_column_helper_test.rb +98 -0
- data/test/file_column_test.rb +652 -0
- data/test/fixtures/entry.rb +28 -0
- data/test/fixtures/invalid-image.jpg +1 -0
- data/test/fixtures/kerb.jpg +0 -0
- data/test/fixtures/mysql.sql +25 -0
- data/test/fixtures/schema.rb +10 -0
- data/test/fixtures/skanthak.png +0 -0
- data/test/magick_test.rb +391 -0
- data/test/magick_view_only_test.rb +21 -0
- metadata +132 -0
@@ -0,0 +1,28 @@
|
|
1
|
+
class Entry < ActiveRecord::Base
|
2
|
+
attr_accessor :validation_should_fail
|
3
|
+
|
4
|
+
def validate
|
5
|
+
errors.add("image","some stupid error") if @validation_should_fail
|
6
|
+
end
|
7
|
+
|
8
|
+
def after_assign
|
9
|
+
@after_assign_called = true
|
10
|
+
end
|
11
|
+
|
12
|
+
def after_assign_called?
|
13
|
+
@after_assign_called
|
14
|
+
end
|
15
|
+
|
16
|
+
def after_save
|
17
|
+
@after_save_called = true
|
18
|
+
end
|
19
|
+
|
20
|
+
def after_save_called?
|
21
|
+
@after_save_called
|
22
|
+
end
|
23
|
+
|
24
|
+
def my_store_dir
|
25
|
+
# not really dynamic but at least it could be...
|
26
|
+
"my_store_dir"
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
this is certainly not a JPEG image
|
Binary file
|
@@ -0,0 +1,25 @@
|
|
1
|
+
-- MySQL dump 9.11
|
2
|
+
--
|
3
|
+
-- Host: localhost Database: file_column_test
|
4
|
+
-- ------------------------------------------------------
|
5
|
+
-- Server version 4.0.24
|
6
|
+
|
7
|
+
--
|
8
|
+
-- Table structure for table `entries`
|
9
|
+
--
|
10
|
+
|
11
|
+
DROP TABLE IF EXISTS entries;
|
12
|
+
CREATE TABLE entries (
|
13
|
+
id int(11) NOT NULL auto_increment,
|
14
|
+
image varchar(200) default NULL,
|
15
|
+
file varchar(200) NOT NULL,
|
16
|
+
PRIMARY KEY (id)
|
17
|
+
) TYPE=MyISAM;
|
18
|
+
|
19
|
+
DROP TABLE IF EXISTS movies;
|
20
|
+
CREATE TABLE movies (
|
21
|
+
id int(11) NOT NULL auto_increment,
|
22
|
+
movie varchar(200) default NULL,
|
23
|
+
PRIMARY KEY (id)
|
24
|
+
) TYPE=MyISAM;
|
25
|
+
|
Binary file
|
data/test/magick_test.rb
ADDED
@@ -0,0 +1,391 @@
|
|
1
|
+
begin
|
2
|
+
require 'RMagick'
|
3
|
+
require File.dirname(__FILE__) + '/abstract_unit'
|
4
|
+
require File.dirname(__FILE__) + '/fixtures/entry'
|
5
|
+
|
6
|
+
class Entry < ActiveRecord::Base
|
7
|
+
def load_image_with_rmagick(path)
|
8
|
+
Magick::Image::read(path).first
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class AbstractRMagickTest < Test::Unit::TestCase
|
13
|
+
def teardown
|
14
|
+
FileUtils.rm_rf File.dirname(__FILE__)+"/public/entry/"
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_truth
|
18
|
+
assert true
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def read_image(path)
|
24
|
+
Magick::Image::read(path).first
|
25
|
+
end
|
26
|
+
|
27
|
+
def assert_max_image_size(img, s)
|
28
|
+
assert img.columns <= s, "img has #{img.columns} columns, expected: #{s}"
|
29
|
+
assert img.rows <= s, "img has #{img.rows} rows, expected: #{s}"
|
30
|
+
assert_equal s, [img.columns, img.rows].max
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class RMagickSimpleTest < AbstractRMagickTest
|
35
|
+
def setup
|
36
|
+
Entry.file_column :image, :magick => { :geometry => "100x100" }
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_simple_resize_without_save
|
40
|
+
e = Entry.new
|
41
|
+
e.image = upload(f("kerb.jpg"))
|
42
|
+
|
43
|
+
img = read_image(e.image)
|
44
|
+
assert_max_image_size img, 100
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_simple_resize_with_save
|
48
|
+
e = Entry.new
|
49
|
+
e.image = upload(f("kerb.jpg"))
|
50
|
+
assert e.save
|
51
|
+
e.reload
|
52
|
+
|
53
|
+
img = read_image(e.image)
|
54
|
+
assert_max_image_size img, 100
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_resize_on_saved_image
|
58
|
+
Entry.file_column :image, :magick => { :geometry => "100x100" }
|
59
|
+
|
60
|
+
e = Entry.new
|
61
|
+
e.image = upload(f("skanthak.png"))
|
62
|
+
assert e.save
|
63
|
+
e.reload
|
64
|
+
old_path = e.image
|
65
|
+
|
66
|
+
e.image = upload(f("kerb.jpg"))
|
67
|
+
assert e.save
|
68
|
+
assert "kerb.jpg", File.basename(e.image)
|
69
|
+
assert !File.exists?(old_path), "old image '#{old_path}' still exists"
|
70
|
+
|
71
|
+
img = read_image(e.image)
|
72
|
+
assert_max_image_size img, 100
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_invalid_image
|
76
|
+
e = Entry.new
|
77
|
+
assert_nothing_raised { e.image = upload(f("invalid-image.jpg")) }
|
78
|
+
assert !e.valid?
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_serializable
|
82
|
+
e = Entry.new
|
83
|
+
e.image = upload(f("skanthak.png"))
|
84
|
+
assert_nothing_raised {
|
85
|
+
flash = Marshal.dump(e)
|
86
|
+
e = Marshal.load(flash)
|
87
|
+
}
|
88
|
+
assert File.exists?(e.image)
|
89
|
+
end
|
90
|
+
|
91
|
+
def test_imagemagick_still_usable
|
92
|
+
e = Entry.new
|
93
|
+
assert_nothing_raised {
|
94
|
+
img = e.load_image_with_rmagick(file_path("skanthak.png"))
|
95
|
+
assert img.kind_of?(Magick::Image)
|
96
|
+
}
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
class RMagickRequiresImageTest < AbstractRMagickTest
|
101
|
+
def setup
|
102
|
+
Entry.file_column :image, :magick => {
|
103
|
+
:size => "100x100>",
|
104
|
+
:image_required => false,
|
105
|
+
:versions => {
|
106
|
+
:thumb => "80x80>",
|
107
|
+
:large => {:size => "200x200>", :lazy => true}
|
108
|
+
}
|
109
|
+
}
|
110
|
+
end
|
111
|
+
|
112
|
+
def test_image_required_with_image
|
113
|
+
e = Entry.new(:image => upload(f("skanthak.png")))
|
114
|
+
assert_max_image_size read_image(e.image), 100
|
115
|
+
assert e.valid?
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_image_required_with_invalid_image
|
119
|
+
e = Entry.new(:image => upload(f("invalid-image.jpg")))
|
120
|
+
assert e.valid?, "did not ignore invalid image"
|
121
|
+
assert FileUtils.identical?(e.image, f("invalid-image.jpg")), "uploaded file has not been left alone"
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_versions_with_invalid_image
|
125
|
+
e = Entry.new(:image => upload(f("invalid-image.jpg")))
|
126
|
+
assert e.valid?
|
127
|
+
|
128
|
+
image_state = e.send(:image_state)
|
129
|
+
assert_nil image_state.create_magick_version_if_needed(:thumb)
|
130
|
+
assert_nil image_state.create_magick_version_if_needed(:large)
|
131
|
+
assert_nil image_state.create_magick_version_if_needed("300x300>")
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
class RMagickCustomAttributesTest < AbstractRMagickTest
|
136
|
+
def assert_image_property(img, property, value, text = nil)
|
137
|
+
assert File.exists?(img), "the image does not exist"
|
138
|
+
assert_equal value, read_image(img).send(property), text
|
139
|
+
end
|
140
|
+
|
141
|
+
def test_simple_attributes
|
142
|
+
Entry.file_column :image, :magick => { :attributes => { :quality => 20 } }
|
143
|
+
e = Entry.new("image" => upload(f("kerb.jpg")))
|
144
|
+
assert_image_property e.image, :quality, 20, "the quality was not set"
|
145
|
+
end
|
146
|
+
|
147
|
+
def test_version_attributes
|
148
|
+
Entry.file_column :image, :magick => {
|
149
|
+
:versions => {
|
150
|
+
:thumb => { :attributes => { :quality => 20 } }
|
151
|
+
}
|
152
|
+
}
|
153
|
+
e = Entry.new("image" => upload(f("kerb.jpg")))
|
154
|
+
assert_image_property e.image("thumb"), :quality, 20, "the quality was not set"
|
155
|
+
end
|
156
|
+
|
157
|
+
def test_lazy_attributes
|
158
|
+
Entry.file_column :image, :magick => {
|
159
|
+
:versions => {
|
160
|
+
:thumb => { :attributes => { :quality => 20 }, :lazy => true }
|
161
|
+
}
|
162
|
+
}
|
163
|
+
e = Entry.new("image" => upload(f("kerb.jpg")))
|
164
|
+
e.send(:image_state).create_magick_version_if_needed(:thumb)
|
165
|
+
assert_image_property e.image("thumb"), :quality, 20, "the quality was not set"
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
class RMagickVersionsTest < AbstractRMagickTest
|
170
|
+
def setup
|
171
|
+
Entry.file_column :image, :magick => {:geometry => "200x200",
|
172
|
+
:versions => {
|
173
|
+
:thumb => "50x50",
|
174
|
+
:medium => {:geometry => "100x100", :name => "100_100"},
|
175
|
+
:large => {:geometry => "150x150", :lazy => true}
|
176
|
+
}
|
177
|
+
}
|
178
|
+
end
|
179
|
+
|
180
|
+
|
181
|
+
def test_should_create_thumb
|
182
|
+
e = Entry.new("image" => upload(f("skanthak.png")))
|
183
|
+
|
184
|
+
assert File.exists?(e.image("thumb")), "thumb-nail not created"
|
185
|
+
|
186
|
+
assert_max_image_size read_image(e.image("thumb")), 50
|
187
|
+
end
|
188
|
+
|
189
|
+
def test_version_name_can_be_different_from_key
|
190
|
+
e = Entry.new("image" => upload(f("skanthak.png")))
|
191
|
+
|
192
|
+
assert File.exists?(e.image("100_100"))
|
193
|
+
assert !File.exists?(e.image("medium"))
|
194
|
+
end
|
195
|
+
|
196
|
+
def test_should_not_create_lazy_versions
|
197
|
+
e = Entry.new("image" => upload(f("skanthak.png")))
|
198
|
+
assert !File.exists?(e.image("large")), "lazy versions should not be created unless needed"
|
199
|
+
end
|
200
|
+
|
201
|
+
def test_should_create_lazy_version_on_demand
|
202
|
+
e = Entry.new("image" => upload(f("skanthak.png")))
|
203
|
+
|
204
|
+
e.send(:image_state).create_magick_version_if_needed(:large)
|
205
|
+
|
206
|
+
assert File.exists?(e.image("large")), "lazy version should be created on demand"
|
207
|
+
|
208
|
+
assert_max_image_size read_image(e.image("large")), 150
|
209
|
+
end
|
210
|
+
|
211
|
+
def test_generated_name_should_not_change
|
212
|
+
e = Entry.new("image" => upload(f("skanthak.png")))
|
213
|
+
|
214
|
+
name1 = e.send(:image_state).create_magick_version_if_needed("50x50")
|
215
|
+
name2 = e.send(:image_state).create_magick_version_if_needed("50x50")
|
216
|
+
name3 = e.send(:image_state).create_magick_version_if_needed(:geometry => "50x50")
|
217
|
+
assert_equal name1, name2, "hash value has changed"
|
218
|
+
assert_equal name1, name3, "hash value has changed"
|
219
|
+
end
|
220
|
+
|
221
|
+
def test_should_create_version_with_string
|
222
|
+
e = Entry.new("image" => upload(f("skanthak.png")))
|
223
|
+
|
224
|
+
name = e.send(:image_state).create_magick_version_if_needed("32x32")
|
225
|
+
|
226
|
+
assert File.exists?(e.image(name))
|
227
|
+
|
228
|
+
assert_max_image_size read_image(e.image(name)), 32
|
229
|
+
end
|
230
|
+
|
231
|
+
def test_should_create_safe_auto_id
|
232
|
+
e = Entry.new("image" => upload(f("skanthak.png")))
|
233
|
+
|
234
|
+
name = e.send(:image_state).create_magick_version_if_needed("32x32")
|
235
|
+
|
236
|
+
assert_match /^[a-zA-Z0-9]+$/, name
|
237
|
+
end
|
238
|
+
end
|
239
|
+
|
240
|
+
class RMagickCroppingTest < AbstractRMagickTest
|
241
|
+
def setup
|
242
|
+
Entry.file_column :image, :magick => {:geometry => "200x200",
|
243
|
+
:versions => {
|
244
|
+
:thumb => {:crop => "1:1", :geometry => "50x50"}
|
245
|
+
}
|
246
|
+
}
|
247
|
+
end
|
248
|
+
|
249
|
+
def test_should_crop_image_on_upload
|
250
|
+
e = Entry.new("image" => upload(f("skanthak.png")))
|
251
|
+
|
252
|
+
img = read_image(e.image("thumb"))
|
253
|
+
|
254
|
+
assert_equal 50, img.rows
|
255
|
+
assert_equal 50, img.columns
|
256
|
+
end
|
257
|
+
|
258
|
+
end
|
259
|
+
|
260
|
+
class UrlForImageColumnTest < AbstractRMagickTest
|
261
|
+
include FileColumnHelper
|
262
|
+
|
263
|
+
def setup
|
264
|
+
Entry.file_column :image, :magick => {
|
265
|
+
:versions => {:thumb => "50x50"}
|
266
|
+
}
|
267
|
+
@request = RequestMock.new
|
268
|
+
end
|
269
|
+
|
270
|
+
def test_should_use_version_on_symbol_option
|
271
|
+
e = Entry.new(:image => upload(f("skanthak.png")))
|
272
|
+
|
273
|
+
url = url_for_image_column(e, "image", :thumb)
|
274
|
+
assert_match %r{^/entry/image/tmp/.+/thumb/skanthak.png$}, url
|
275
|
+
end
|
276
|
+
|
277
|
+
def test_should_use_string_as_size
|
278
|
+
e = Entry.new(:image => upload(f("skanthak.png")))
|
279
|
+
|
280
|
+
url = url_for_image_column(e, "image", "50x50")
|
281
|
+
|
282
|
+
assert_match %r{^/entry/image/tmp/.+/.+/skanthak.png$}, url
|
283
|
+
|
284
|
+
url =~ /\/([^\/]+)\/skanthak.png$/
|
285
|
+
dirname = $1
|
286
|
+
|
287
|
+
assert_max_image_size read_image(e.image(dirname)), 50
|
288
|
+
end
|
289
|
+
|
290
|
+
def test_should_accept_version_hash
|
291
|
+
e = Entry.new(:image => upload(f("skanthak.png")))
|
292
|
+
|
293
|
+
url = url_for_image_column(e, "image", :size => "50x50", :crop => "1:1", :name => "small")
|
294
|
+
|
295
|
+
assert_match %r{^/entry/image/tmp/.+/small/skanthak.png$}, url
|
296
|
+
|
297
|
+
img = read_image(e.image("small"))
|
298
|
+
assert_equal 50, img.rows
|
299
|
+
assert_equal 50, img.columns
|
300
|
+
end
|
301
|
+
end
|
302
|
+
|
303
|
+
class RMagickPermissionsTest < AbstractRMagickTest
|
304
|
+
def setup
|
305
|
+
Entry.file_column :image, :magick => {:geometry => "200x200",
|
306
|
+
:versions => {
|
307
|
+
:thumb => {:crop => "1:1", :geometry => "50x50"}
|
308
|
+
}
|
309
|
+
}, :permissions => 0616
|
310
|
+
end
|
311
|
+
|
312
|
+
def check_permissions(e)
|
313
|
+
assert_equal 0616, (File.stat(e.image).mode & 0777)
|
314
|
+
assert_equal 0616, (File.stat(e.image("thumb")).mode & 0777)
|
315
|
+
end
|
316
|
+
|
317
|
+
def test_permissions_with_rmagick
|
318
|
+
e = Entry.new(:image => upload(f("skanthak.png")))
|
319
|
+
|
320
|
+
check_permissions e
|
321
|
+
|
322
|
+
assert e.save
|
323
|
+
|
324
|
+
check_permissions e
|
325
|
+
end
|
326
|
+
end
|
327
|
+
|
328
|
+
class Entry
|
329
|
+
def transform_grey(img)
|
330
|
+
img.quantize(256, Magick::GRAYColorspace)
|
331
|
+
end
|
332
|
+
end
|
333
|
+
|
334
|
+
class RMagickTransformationTest < AbstractRMagickTest
|
335
|
+
def assert_transformed(image)
|
336
|
+
assert File.exists?(image), "the image does not exist"
|
337
|
+
assert 256 > read_image(image).number_colors, "the number of colors was not changed"
|
338
|
+
end
|
339
|
+
|
340
|
+
def test_simple_transformation
|
341
|
+
Entry.file_column :image, :magick => { :transformation => Proc.new { |image| image.quantize(256, Magick::GRAYColorspace) } }
|
342
|
+
e = Entry.new("image" => upload(f("skanthak.png")))
|
343
|
+
assert_transformed(e.image)
|
344
|
+
end
|
345
|
+
|
346
|
+
def test_simple_version_transformation
|
347
|
+
Entry.file_column :image, :magick => {
|
348
|
+
:versions => { :thumb => Proc.new { |image| image.quantize(256, Magick::GRAYColorspace) } }
|
349
|
+
}
|
350
|
+
e = Entry.new("image" => upload(f("skanthak.png")))
|
351
|
+
assert_transformed(e.image("thumb"))
|
352
|
+
end
|
353
|
+
|
354
|
+
def test_complex_version_transformation
|
355
|
+
Entry.file_column :image, :magick => {
|
356
|
+
:versions => {
|
357
|
+
:thumb => { :transformation => Proc.new { |image| image.quantize(256, Magick::GRAYColorspace) } }
|
358
|
+
}
|
359
|
+
}
|
360
|
+
e = Entry.new("image" => upload(f("skanthak.png")))
|
361
|
+
assert_transformed(e.image("thumb"))
|
362
|
+
end
|
363
|
+
|
364
|
+
def test_lazy_transformation
|
365
|
+
Entry.file_column :image, :magick => {
|
366
|
+
:versions => {
|
367
|
+
:thumb => { :transformation => Proc.new { |image| image.quantize(256, Magick::GRAYColorspace) }, :lazy => true }
|
368
|
+
}
|
369
|
+
}
|
370
|
+
e = Entry.new("image" => upload(f("skanthak.png")))
|
371
|
+
e.send(:image_state).create_magick_version_if_needed(:thumb)
|
372
|
+
assert_transformed(e.image("thumb"))
|
373
|
+
end
|
374
|
+
|
375
|
+
def test_simple_callback_transformation
|
376
|
+
Entry.file_column :image, :magick => :transform_grey
|
377
|
+
e = Entry.new(:image => upload(f("skanthak.png")))
|
378
|
+
assert_transformed(e.image)
|
379
|
+
end
|
380
|
+
|
381
|
+
def test_complex_callback_transformation
|
382
|
+
Entry.file_column :image, :magick => { :transformation => :transform_grey }
|
383
|
+
e = Entry.new(:image => upload(f("skanthak.png")))
|
384
|
+
assert_transformed(e.image)
|
385
|
+
end
|
386
|
+
end
|
387
|
+
|
388
|
+
rescue LoadError
|
389
|
+
puts "Cannot find RMagick gem"
|
390
|
+
puts "Needs RMagick gem to run these tests and support image thumbnails with file_column"
|
391
|
+
end
|