attachs 0.4.0 → 0.4.1
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.
- checksums.yaml +4 -4
- data/README.md +35 -3
- data/lib/attachs/attachment.rb +6 -0
- data/lib/attachs/processors/base.rb +25 -0
- data/lib/attachs/processors/thumbnail.rb +69 -0
- data/lib/attachs/storages/base.rb +4 -8
- data/lib/attachs/storages/local.rb +11 -12
- data/lib/attachs/storages/s3.rb +14 -13
- data/lib/attachs/version.rb +1 -1
- data/lib/attachs.rb +6 -4
- data/lib/tasks/attachs.rake +2 -2
- data/test/dummy/log/test.log +7308 -0
- data/test/dummy/public/big/180x150.gif +0 -0
- data/test/dummy/public/medium/180x150.gif +0 -0
- data/test/dummy/public/original/180x150.gif +0 -0
- data/test/dummy/public/original/file.txt +1 -0
- data/test/dummy/public/resized.gif +0 -0
- data/test/dummy/public/small/180x150.gif +0 -0
- data/test/dummy/public/storage/image/5461/big/11/180x150.gif +0 -0
- data/test/dummy/public/storage/image/5461/big/9/180x150.gif +0 -0
- data/test/dummy/public/storage/image/5461/medium/11/180x150.gif +0 -0
- data/test/dummy/public/storage/image/5461/medium/9/180x150.gif +0 -0
- data/test/dummy/public/storage/image/5461/original/11/180x150.gif +0 -0
- data/test/dummy/public/storage/image/5461/original/9/180x150.gif +0 -0
- data/test/dummy/public/storage/image/5461/small/11/180x150.gif +0 -0
- data/test/dummy/public/storage/image/5461/small/9/180x150.gif +0 -0
- data/test/dummy/public/storage/text/11/original/11/file.txt +1 -0
- data/test/dummy/public/storage/text/11/original/9/file.txt +1 -0
- data/test/local_storage_test.rb +15 -15
- data/test/processor_test.rb +46 -0
- data/test/s3_storage_tes.rb +18 -22
- data/test/tasks_test.rb +3 -11
- metadata +38 -5
- data/lib/attachs/tools/magick.rb +0 -52
- data/test/magick_tool_test.rb +0 -42
Binary file
|
Binary file
|
Binary file
|
@@ -0,0 +1 @@
|
|
1
|
+
1234567890
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
Binary file
|
@@ -0,0 +1 @@
|
|
1
|
+
1234567890
|
@@ -0,0 +1 @@
|
|
1
|
+
1234567890
|
data/test/local_storage_test.rb
CHANGED
@@ -4,32 +4,32 @@ class LocalStorageTest < ActiveSupport::TestCase
|
|
4
4
|
|
5
5
|
test 'file url' do
|
6
6
|
medium = Medium.create(local_attach: file_upload)
|
7
|
-
assert_equal file_url, medium.local_attach.url
|
7
|
+
assert_equal file_url(:original, medium), medium.local_attach.url
|
8
8
|
end
|
9
9
|
|
10
10
|
test 'image url' do
|
11
11
|
medium = Medium.create(local_attach: image_upload)
|
12
|
-
assert_equal image_url, medium.local_attach.url
|
13
|
-
assert_equal image_url(:small), medium.local_attach.url(:small)
|
12
|
+
assert_equal image_url(:original, medium), medium.local_attach.url
|
13
|
+
assert_equal image_url(:small, medium), medium.local_attach.url(:small)
|
14
14
|
end
|
15
15
|
|
16
16
|
test 'crud' do
|
17
17
|
medium = Medium.create(local_attach: file_upload)
|
18
|
-
assert File.exist?(file_path)
|
18
|
+
assert File.exist?(file_path(:original))
|
19
19
|
medium.update_attributes! local_attach: image_upload
|
20
|
-
assert !File.exist?(file_path)
|
21
|
-
assert File.exist?(image_path)
|
20
|
+
assert !File.exist?(file_path(:original))
|
21
|
+
assert File.exist?(image_path(:original))
|
22
22
|
assert File.exist?(image_path(:small))
|
23
23
|
medium.destroy
|
24
|
-
assert !File.exist?(image_path)
|
24
|
+
assert !File.exist?(image_path(:original))
|
25
25
|
assert !File.exist?(image_path(:small))
|
26
26
|
end
|
27
27
|
|
28
28
|
test 'detroy attr' do
|
29
29
|
medium = Medium.create(local_attach: file_upload, destroy_attach: true)
|
30
|
-
assert File.exist?(file_path)
|
30
|
+
assert File.exist?(file_path(:original))
|
31
31
|
medium.update_attributes! destroy_local_attach: true
|
32
|
-
assert !File.exist?(file_path)
|
32
|
+
assert !File.exist?(file_path(:original))
|
33
33
|
end
|
34
34
|
|
35
35
|
private
|
@@ -38,19 +38,19 @@ class LocalStorageTest < ActiveSupport::TestCase
|
|
38
38
|
Time.zone.now.month
|
39
39
|
end
|
40
40
|
|
41
|
-
def file_url(style
|
42
|
-
"/storage/text/11/#{style}/#{month}/file.txt"
|
41
|
+
def file_url(style, record)
|
42
|
+
"/storage/text/11/#{style}/#{month}/file.txt?#{record.local_attach_updated_at.to_i}"
|
43
43
|
end
|
44
44
|
|
45
|
-
def image_url(style
|
46
|
-
"/storage/image/5461/#{style}/#{month}/180x150.gif"
|
45
|
+
def image_url(style, record)
|
46
|
+
"/storage/image/5461/#{style}/#{month}/180x150.gif?#{record.local_attach_updated_at.to_i}"
|
47
47
|
end
|
48
48
|
|
49
|
-
def file_path(style
|
49
|
+
def file_path(style)
|
50
50
|
Rails.root.join("public/storage/text/11/#{style}/#{month}/file.txt")
|
51
51
|
end
|
52
52
|
|
53
|
-
def image_path(style
|
53
|
+
def image_path(style)
|
54
54
|
Rails.root.join("public/storage/image/5461/#{style}/#{month}/180x150.gif")
|
55
55
|
end
|
56
56
|
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ProcessorTest < ActiveSupport::TestCase
|
4
|
+
|
5
|
+
teardown do
|
6
|
+
Dir[Rails.root.join('tmp', '*')].each do |file|
|
7
|
+
File.delete file
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
test 'force resize' do
|
12
|
+
Attachs::Processors::Thumbnail.new(nil, source).process(:big_force, resized)
|
13
|
+
assert_equal [160,130], dimensions(resized)
|
14
|
+
Attachs::Processors::Thumbnail.new(nil, source).process(:small_force, resized)
|
15
|
+
assert_equal [140,110], dimensions(resized)
|
16
|
+
end
|
17
|
+
|
18
|
+
test 'cover resize' do
|
19
|
+
Attachs::Processors::Thumbnail.new(nil, source).process(:big_cover, resized)
|
20
|
+
assert_equal [160,130], dimensions(resized)
|
21
|
+
Attachs::Processors::Thumbnail.new(nil, source).process(:small_cover, resized)
|
22
|
+
assert_equal [140,110], dimensions(resized)
|
23
|
+
end
|
24
|
+
|
25
|
+
test 'contain resize' do
|
26
|
+
Attachs::Processors::Thumbnail.new(nil, source).process(:big_contain, resized)
|
27
|
+
assert_equal [156,130], dimensions(resized)
|
28
|
+
Attachs::Processors::Thumbnail.new(nil, source).process(:small_contain, resized)
|
29
|
+
assert_equal [132,110], dimensions(resized)
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def source
|
35
|
+
image_upload.path
|
36
|
+
end
|
37
|
+
|
38
|
+
def resized
|
39
|
+
Rails.root.join('public', 'resized.gif')
|
40
|
+
end
|
41
|
+
|
42
|
+
def dimensions(source)
|
43
|
+
`identify -format %wx%h '#{source}'`.split('x').map(&:to_i)
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
data/test/s3_storage_tes.rb
CHANGED
@@ -4,35 +4,35 @@ class S3StorageTest < ActiveSupport::TestCase
|
|
4
4
|
|
5
5
|
test 'file url' do
|
6
6
|
medium = Medium.create(s3_attach: file_upload)
|
7
|
-
assert_equal file_url, medium.s3_attach.url
|
8
|
-
assert_equal file_url(
|
7
|
+
assert_equal file_url(:original, medium, false), medium.s3_attach.url
|
8
|
+
assert_equal file_url(:original, medium, true), medium.s3_attach.url(ssl: true)
|
9
9
|
end
|
10
10
|
|
11
11
|
test 'image url' do
|
12
12
|
medium = Medium.create(s3_attach: image_upload)
|
13
|
-
assert_equal image_url, medium.s3_attach.url
|
14
|
-
assert_equal image_url(
|
15
|
-
assert_equal image_url(:small), medium.s3_attach.url(:small)
|
16
|
-
assert_equal image_url(:small,
|
13
|
+
assert_equal image_url(:original, medium, false), medium.s3_attach.url
|
14
|
+
assert_equal image_url(:original, medium, true), medium.s3_attach.url(ssl: true)
|
15
|
+
assert_equal image_url(:small, medium, false), medium.s3_attach.url(:small)
|
16
|
+
assert_equal image_url(:small, medium, true), medium.s3_attach.url(:small, ssl: true)
|
17
17
|
end
|
18
18
|
|
19
19
|
test 'storage' do
|
20
20
|
medium = Medium.create(s3_attach: file_upload)
|
21
|
-
assert_url file_url
|
21
|
+
assert_url file_url(:original, medium, false)
|
22
22
|
medium.update_attributes! s3_attach: image_upload
|
23
|
-
assert_not_url file_url
|
24
|
-
assert_url image_url
|
25
|
-
assert_url image_url(:small)
|
23
|
+
assert_not_url file_url(:original, medium, false)
|
24
|
+
assert_url image_url(:original, medium, false)
|
25
|
+
assert_url image_url(:small, medium, false)
|
26
26
|
medium.destroy
|
27
|
-
assert_not_url image_url
|
28
|
-
assert_not_url image_url(:small)
|
27
|
+
assert_not_url image_url(:original, medium, false)
|
28
|
+
assert_not_url image_url(:small, medium, false)
|
29
29
|
end
|
30
30
|
|
31
31
|
test 'detroy attr' do
|
32
32
|
medium = Medium.create(s3_attach: file_upload, destroy_s3_attach: true)
|
33
|
-
assert_url file_url
|
33
|
+
assert_url file_url(:original, medium, false)
|
34
34
|
medium.update_attributes! destroy_s3_attach: true
|
35
|
-
assert_not_url file_url
|
35
|
+
assert_not_url file_url(:original, medium, false)
|
36
36
|
end
|
37
37
|
|
38
38
|
private
|
@@ -41,16 +41,12 @@ class S3StorageTest < ActiveSupport::TestCase
|
|
41
41
|
Time.zone.now.month
|
42
42
|
end
|
43
43
|
|
44
|
-
def file_url(
|
45
|
-
|
46
|
-
style = (args.first || :original)
|
47
|
-
"http#{'s' if options[:ssl]}://attachs-test.s3.amazonaws.com/storage/text/11/#{style}/#{month}/file.txt"
|
44
|
+
def file_url(style, record, ssl)
|
45
|
+
"http#{'s' if ssl}://attachs-test.s3.amazonaws.com/storage/text/11/#{style}/#{month}/file.txt?#{record.s3_attach_updated_at.to_i}"
|
48
46
|
end
|
49
47
|
|
50
|
-
def image_url(
|
51
|
-
|
52
|
-
style = (args.first || :original)
|
53
|
-
"http#{'s' if options[:ssl]}://attachs-test.s3.amazonaws.com/storage/image/5461/#{style}/#{month}/180x150.gif"
|
48
|
+
def image_url(style, record, ssl)
|
49
|
+
"http#{'s' if ssl}://attachs-test.s3.amazonaws.com/storage/image/5461/#{style}/#{month}/180x150.gif?#{record.s3_attach_updated_at.to_i}"
|
54
50
|
end
|
55
51
|
|
56
52
|
end
|
data/test/tasks_test.rb
CHANGED
@@ -38,24 +38,16 @@ class TasksTest < ActiveSupport::TestCase
|
|
38
38
|
|
39
39
|
private
|
40
40
|
|
41
|
-
def image_path(style
|
41
|
+
def image_path(style)
|
42
42
|
Rails.root.join("public/#{style}/180x150.gif")
|
43
43
|
end
|
44
44
|
|
45
|
-
def small_path
|
46
|
-
image_path(:small)
|
47
|
-
end
|
48
|
-
|
49
|
-
def big_path
|
50
|
-
image_path(:big)
|
51
|
-
end
|
52
|
-
|
53
45
|
def small_time
|
54
|
-
File.mtime(
|
46
|
+
File.mtime(image_path(:small))
|
55
47
|
end
|
56
48
|
|
57
49
|
def big_time
|
58
|
-
File.mtime(
|
50
|
+
File.mtime(image_path(:big))
|
59
51
|
end
|
60
52
|
|
61
53
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: attachs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Museways
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-11-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: aws-sdk
|
@@ -79,11 +79,12 @@ files:
|
|
79
79
|
- lib/attachs/attachment.rb
|
80
80
|
- lib/attachs/locales/en.yml
|
81
81
|
- lib/attachs/locales/es.yml
|
82
|
+
- lib/attachs/processors/base.rb
|
83
|
+
- lib/attachs/processors/thumbnail.rb
|
82
84
|
- lib/attachs/railtie.rb
|
83
85
|
- lib/attachs/storages/base.rb
|
84
86
|
- lib/attachs/storages/local.rb
|
85
87
|
- lib/attachs/storages/s3.rb
|
86
|
-
- lib/attachs/tools/magick.rb
|
87
88
|
- lib/attachs/types/base.rb
|
88
89
|
- lib/attachs/types/default.rb
|
89
90
|
- lib/attachs/types/regular.rb
|
@@ -129,13 +130,29 @@ files:
|
|
129
130
|
- test/dummy/public/404.html
|
130
131
|
- test/dummy/public/422.html
|
131
132
|
- test/dummy/public/500.html
|
133
|
+
- test/dummy/public/big/180x150.gif
|
132
134
|
- test/dummy/public/favicon.ico
|
135
|
+
- test/dummy/public/medium/180x150.gif
|
136
|
+
- test/dummy/public/original/180x150.gif
|
137
|
+
- test/dummy/public/original/file.txt
|
138
|
+
- test/dummy/public/resized.gif
|
139
|
+
- test/dummy/public/small/180x150.gif
|
140
|
+
- test/dummy/public/storage/image/5461/big/11/180x150.gif
|
141
|
+
- test/dummy/public/storage/image/5461/big/9/180x150.gif
|
142
|
+
- test/dummy/public/storage/image/5461/medium/11/180x150.gif
|
143
|
+
- test/dummy/public/storage/image/5461/medium/9/180x150.gif
|
144
|
+
- test/dummy/public/storage/image/5461/original/11/180x150.gif
|
145
|
+
- test/dummy/public/storage/image/5461/original/9/180x150.gif
|
146
|
+
- test/dummy/public/storage/image/5461/small/11/180x150.gif
|
147
|
+
- test/dummy/public/storage/image/5461/small/9/180x150.gif
|
148
|
+
- test/dummy/public/storage/text/11/original/11/file.txt
|
149
|
+
- test/dummy/public/storage/text/11/original/9/file.txt
|
133
150
|
- test/dummy/test/fixtures/180x150.gif
|
134
151
|
- test/dummy/test/fixtures/file.txt
|
135
152
|
- test/generator_test.rb
|
136
153
|
- test/local_storage_test.rb
|
137
|
-
- test/magick_tool_test.rb
|
138
154
|
- test/migration_test.rb
|
155
|
+
- test/processor_test.rb
|
139
156
|
- test/s3_storage_tes.rb
|
140
157
|
- test/tasks_test.rb
|
141
158
|
- test/test_helper.rb
|
@@ -202,15 +219,31 @@ test_files:
|
|
202
219
|
- test/dummy/public/404.html
|
203
220
|
- test/dummy/public/422.html
|
204
221
|
- test/dummy/public/500.html
|
222
|
+
- test/dummy/public/big/180x150.gif
|
205
223
|
- test/dummy/public/favicon.ico
|
224
|
+
- test/dummy/public/medium/180x150.gif
|
225
|
+
- test/dummy/public/original/180x150.gif
|
226
|
+
- test/dummy/public/original/file.txt
|
227
|
+
- test/dummy/public/resized.gif
|
228
|
+
- test/dummy/public/small/180x150.gif
|
229
|
+
- test/dummy/public/storage/image/5461/big/11/180x150.gif
|
230
|
+
- test/dummy/public/storage/image/5461/big/9/180x150.gif
|
231
|
+
- test/dummy/public/storage/image/5461/medium/11/180x150.gif
|
232
|
+
- test/dummy/public/storage/image/5461/medium/9/180x150.gif
|
233
|
+
- test/dummy/public/storage/image/5461/original/11/180x150.gif
|
234
|
+
- test/dummy/public/storage/image/5461/original/9/180x150.gif
|
235
|
+
- test/dummy/public/storage/image/5461/small/11/180x150.gif
|
236
|
+
- test/dummy/public/storage/image/5461/small/9/180x150.gif
|
237
|
+
- test/dummy/public/storage/text/11/original/11/file.txt
|
238
|
+
- test/dummy/public/storage/text/11/original/9/file.txt
|
206
239
|
- test/dummy/Rakefile
|
207
240
|
- test/dummy/README.rdoc
|
208
241
|
- test/dummy/test/fixtures/180x150.gif
|
209
242
|
- test/dummy/test/fixtures/file.txt
|
210
243
|
- test/generator_test.rb
|
211
244
|
- test/local_storage_test.rb
|
212
|
-
- test/magick_tool_test.rb
|
213
245
|
- test/migration_test.rb
|
246
|
+
- test/processor_test.rb
|
214
247
|
- test/s3_storage_tes.rb
|
215
248
|
- test/tasks_test.rb
|
216
249
|
- test/test_helper.rb
|
data/lib/attachs/tools/magick.rb
DELETED
@@ -1,52 +0,0 @@
|
|
1
|
-
module Attachs
|
2
|
-
module Tools
|
3
|
-
module Magick
|
4
|
-
class << self
|
5
|
-
|
6
|
-
def dimensions(source)
|
7
|
-
if output = run("identify -format %wx%h '#{source}'")
|
8
|
-
output.split('x').map(&:to_i)
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
def resize(source, style, destination=nil)
|
13
|
-
size = Attachs.config.styles[style]
|
14
|
-
new_width, new_height = size.scan(/[^x]+/).map(&:to_i)
|
15
|
-
case strategy = size[/!|#/]
|
16
|
-
when '#'
|
17
|
-
width, height = dimensions(source)
|
18
|
-
ratio = [new_width.to_f/width, new_height.to_f/height].max
|
19
|
-
options = "-resize #{(ratio*width).ceil}x#{(ratio*height).ceil} -gravity center -crop #{new_width}x#{new_height}+0+0"
|
20
|
-
when '!'
|
21
|
-
options = "-resize #{new_width}x#{new_height}\!"
|
22
|
-
else
|
23
|
-
options = "-resize #{new_width}x#{new_height}"
|
24
|
-
end
|
25
|
-
if global_options = Attachs.config.global_convert_options
|
26
|
-
options << " #{global_options}"
|
27
|
-
end
|
28
|
-
if style_options = Attachs.config.convert_options[style]
|
29
|
-
options << " #{style_options}"
|
30
|
-
end
|
31
|
-
if destination
|
32
|
-
run "convert '#{source}' #{options} '#{destination}'"
|
33
|
-
else
|
34
|
-
run "mogrify #{options} '#{source}'"
|
35
|
-
end
|
36
|
-
end
|
37
|
-
|
38
|
-
protected
|
39
|
-
|
40
|
-
def run(cmd)
|
41
|
-
stdout, stderr, status = Open3.capture3(cmd)
|
42
|
-
if status.success?
|
43
|
-
stdout.strip
|
44
|
-
else
|
45
|
-
false
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
data/test/magick_tool_test.rb
DELETED
@@ -1,42 +0,0 @@
|
|
1
|
-
require 'test_helper'
|
2
|
-
|
3
|
-
class MagickToolTest < ActiveSupport::TestCase
|
4
|
-
|
5
|
-
teardown do
|
6
|
-
Dir[Rails.root.join('tmp', '*')].each do |file|
|
7
|
-
File.delete file
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
test 'force resize' do
|
12
|
-
Attachs::Tools::Magick.resize(source, :big_force, resized)
|
13
|
-
assert_equal [160,130], Attachs::Tools::Magick.dimensions(resized)
|
14
|
-
Attachs::Tools::Magick.resize(resized, :small_force)
|
15
|
-
assert_equal [140,110], Attachs::Tools::Magick.dimensions(resized)
|
16
|
-
end
|
17
|
-
|
18
|
-
test 'cover resize' do
|
19
|
-
Attachs::Tools::Magick.resize(source, :big_cover, resized)
|
20
|
-
assert_equal [160, 130], Attachs::Tools::Magick.dimensions(resized)
|
21
|
-
Attachs::Tools::Magick.resize(resized, :small_cover)
|
22
|
-
assert_equal [140,110], Attachs::Tools::Magick.dimensions(resized)
|
23
|
-
end
|
24
|
-
|
25
|
-
test 'contain resize' do
|
26
|
-
Attachs::Tools::Magick.resize(source, :big_contain, resized)
|
27
|
-
assert_equal [156, 130], Attachs::Tools::Magick.dimensions(resized)
|
28
|
-
Attachs::Tools::Magick.resize(resized, :small_contain)
|
29
|
-
assert_equal [132,110], Attachs::Tools::Magick.dimensions(resized)
|
30
|
-
end
|
31
|
-
|
32
|
-
private
|
33
|
-
|
34
|
-
def source
|
35
|
-
image_upload.path
|
36
|
-
end
|
37
|
-
|
38
|
-
def resized
|
39
|
-
Rails.root.join('public', 'resized.gif')
|
40
|
-
end
|
41
|
-
|
42
|
-
end
|