light_resizer 0.0.2 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/light_resizer/carrierwave_resize.rb +15 -4
- data/lib/light_resizer/image_loader/resize_image.rb +2 -1
- data/lib/light_resizer/middleware.rb +3 -4
- data/lib/light_resizer/middleware/path.rb +26 -12
- data/lib/light_resizer/version.rb +1 -1
- data/spec/integration/middleware_spec.rb +9 -9
- data/spec/spec_helper.rb +1 -0
- data/spec/unit/image_loader_resized_spec.rb +2 -2
- data/spec/unit/image_loader_spec.rb +1 -1
- data/spec/unit/middleware_path_spec.rb +6 -4
- data/spec/unit/middleware_resizer_spec.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 48f8da7f8cc63574dad6a1a0a3ef30299f576535
|
4
|
+
data.tar.gz: 293fef11bdb1a33eb4e3b808a7155e3fb1d779af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 33743ca407ad1198d4ac922543778f8579c0559c2169cf81f14d63820900a4b57530fcf1c661a071b92831b45d19534e23eea5a8da8c2fa43cdd8464f9bda442
|
7
|
+
data.tar.gz: 2806c2280ec857358c7899f056442a5863385a1c2d5f1c5a8454a0dd19688443b806c4877310e0f1435910b27e2702b46fa5f86e91e79af0f0cb5d9576505f59
|
@@ -1,13 +1,24 @@
|
|
1
1
|
module LightResizer
|
2
2
|
module CarrierWaveResize
|
3
3
|
def resize(width, height)
|
4
|
-
|
5
|
-
|
4
|
+
filename_prefix = "#{width}x#{height}_"
|
5
|
+
resized_image_path(filename_prefix)
|
6
6
|
end
|
7
7
|
|
8
8
|
def resize_crop(width, height)
|
9
|
-
|
10
|
-
|
9
|
+
filename_prefix = "#{width}x#{height}_crop_"
|
10
|
+
resized_image_path(filename_prefix)
|
11
11
|
end
|
12
|
+
|
13
|
+
private
|
14
|
+
def resized_image_path(prefix)
|
15
|
+
if url
|
16
|
+
path = File.dirname(url)
|
17
|
+
filename = File.basename(url)
|
18
|
+
new_filname = prefix + filename
|
19
|
+
|
20
|
+
File.join(path, 'light_resize', new_filname)
|
21
|
+
end
|
22
|
+
end
|
12
23
|
end
|
13
24
|
end
|
@@ -3,9 +3,10 @@ module LightResizer
|
|
3
3
|
class ImageLoader
|
4
4
|
class ResizeImage
|
5
5
|
|
6
|
-
STORE_RESIZE_DIR = '
|
6
|
+
STORE_RESIZE_DIR = 'light_resize' #todo move to config option
|
7
7
|
|
8
8
|
attr_accessor :original_filename, :original_relative_dir, :resize_prefix
|
9
|
+
attr_reader :root_dir
|
9
10
|
|
10
11
|
def initialize(root_dir)
|
11
12
|
@root_dir = root_dir
|
@@ -16,9 +16,9 @@ module LightResizer
|
|
16
16
|
def call(env)
|
17
17
|
@path.request_path = env['PATH_INFO']
|
18
18
|
|
19
|
-
|
19
|
+
resize if (@path.image_path? and resize_request?)
|
20
20
|
|
21
|
-
@
|
21
|
+
Rack::File.new(@image_loader.resized.root_dir).call(env)
|
22
22
|
end
|
23
23
|
|
24
24
|
def resize_request?
|
@@ -30,11 +30,10 @@ module LightResizer
|
|
30
30
|
|
31
31
|
private
|
32
32
|
|
33
|
-
def
|
33
|
+
def resize
|
34
34
|
unless @image_loader.resized_image_exist?
|
35
35
|
@resizer.resize(@path.dimensions, @image_loader.original_path, @image_loader.resize_path, @path.crop_path?)
|
36
36
|
end
|
37
|
-
@image_loader.resized_image_relative_path
|
38
37
|
end
|
39
38
|
|
40
39
|
end
|
@@ -3,6 +3,8 @@ module LightResizer
|
|
3
3
|
class Middleware
|
4
4
|
class Path
|
5
5
|
|
6
|
+
PREFIX_REGEXP = /^[0-9]+x[0-9]+(_crop)?_/
|
7
|
+
|
6
8
|
attr_reader :request_path
|
7
9
|
|
8
10
|
def request_path=(path)
|
@@ -12,35 +14,47 @@ module LightResizer
|
|
12
14
|
|
13
15
|
# {Bool} returns true if request path begins with 'image'
|
14
16
|
def image_path?
|
15
|
-
|
17
|
+
request_dir.end_with?('light_resize')
|
16
18
|
end
|
17
19
|
|
18
20
|
|
19
21
|
# {Bool} returns true if image should be croped on resize
|
20
22
|
def crop_path?
|
21
|
-
|
23
|
+
prefix.end_with?('crop')
|
22
24
|
end
|
23
25
|
|
24
26
|
# {String} last part of request – relative path
|
25
27
|
def image_path
|
26
|
-
|
28
|
+
dir = File.expand_path('..', request_dir)
|
29
|
+
File.join(dir, original_filename)
|
27
30
|
end
|
28
31
|
|
29
|
-
# {Array} returns required dimensions of image. Like a 200x200
|
30
|
-
def dimensions
|
31
|
-
#todo validate params!
|
32
|
-
segments[2]
|
33
|
-
end
|
34
32
|
|
35
33
|
# {String} returns prefix of resized image name
|
36
|
-
#
|
34
|
+
# 200x200_crop_image.png => 200x200_crop
|
37
35
|
def prefix
|
38
|
-
|
36
|
+
filename[PREFIX_REGEXP].chop if image_path?
|
37
|
+
end
|
38
|
+
|
39
|
+
def dimensions
|
40
|
+
prefix.split('_').first
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def request_dir
|
46
|
+
File.dirname(request_path)
|
47
|
+
end
|
48
|
+
|
49
|
+
def original_filename
|
50
|
+
filename.gsub(PREFIX_REGEXP, '')
|
51
|
+
end
|
39
52
|
|
40
|
-
|
53
|
+
def filename
|
54
|
+
File.basename(request_path)
|
41
55
|
end
|
42
56
|
|
43
|
-
def
|
57
|
+
def name_segments
|
44
58
|
@segments ||= request_path.split('/')
|
45
59
|
end
|
46
60
|
|
@@ -3,25 +3,25 @@ require 'spec_helper'
|
|
3
3
|
describe LightResizer::Middleware do
|
4
4
|
let(:app) { AppEmulator.new }
|
5
5
|
let(:resizer) { described_class.new(app, ROOT, 'fixtures') }
|
6
|
-
let(:env) { { 'PATH_INFO' =>
|
6
|
+
let(:env) { { 'PATH_INFO' => resize_relative_path} }
|
7
7
|
|
8
|
-
let(:resize_relative_path) { '/
|
8
|
+
let(:resize_relative_path) { '/light_resize/150x100_avatar.png' } #todo depends for many constant
|
9
9
|
let(:resize_full_path) { File.join(ROOT, 'fixtures', resize_relative_path) }
|
10
10
|
|
11
|
-
after do
|
12
|
-
File.delete
|
11
|
+
after(:each) do
|
12
|
+
File.delete(resize_full_path)
|
13
13
|
end
|
14
14
|
|
15
|
-
it 'should
|
15
|
+
it 'should approve resize request' do
|
16
16
|
resizer.call(env)
|
17
17
|
|
18
|
-
expect(resizer.resize_request?).to
|
18
|
+
expect(resizer.resize_request?).to be_true
|
19
19
|
end
|
20
20
|
|
21
|
-
it 'should
|
21
|
+
it 'should create resized image' do
|
22
|
+
expect(File.exist?(resize_full_path)).to be_false
|
22
23
|
resizer.call(env)
|
23
|
-
|
24
|
-
expect(app.env['PATH_INFO']).to eq( resize_relative_path )
|
24
|
+
expect(File.exist?(resize_full_path)).to be_true
|
25
25
|
end
|
26
26
|
|
27
27
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -22,7 +22,7 @@ describe LightResizer::ImageLoader::ResizeImage do
|
|
22
22
|
}
|
23
23
|
|
24
24
|
it {
|
25
|
-
expected_path = File.join(store_dir,
|
25
|
+
expected_path = File.join(store_dir, 'light_resize', resized_image.filename)
|
26
26
|
expect(resized_image.send(:relative_path)).to eq( expected_path )
|
27
27
|
}
|
28
28
|
|
@@ -32,7 +32,7 @@ describe LightResizer::ImageLoader::ResizeImage do
|
|
32
32
|
}
|
33
33
|
|
34
34
|
it {
|
35
|
-
expected_path = File.join(root_dir, store_dir,
|
35
|
+
expected_path = File.join(root_dir, store_dir, 'light_resize' )
|
36
36
|
expect(resized_image.send(:dir_path)).to eq( expected_path )
|
37
37
|
}
|
38
38
|
|
@@ -14,7 +14,7 @@ describe LightResizer::ImageLoader do
|
|
14
14
|
|
15
15
|
it { expect(image_loader.send(:original_image_exist?)).to eq(false) }
|
16
16
|
|
17
|
-
it { expect(image_loader.send(:resized_image_relative_path)).to eq('/some/dir/
|
17
|
+
it { expect(image_loader.send(:resized_image_relative_path)).to eq('/some/dir/light_resize/20x20_image.png') }
|
18
18
|
|
19
19
|
it { expect(image_loader.send(:resized_image_exist?)).to eq(false) }
|
20
20
|
|
@@ -7,10 +7,10 @@ describe LightResizer::Middleware::Path do
|
|
7
7
|
context 'valid path methods' do
|
8
8
|
|
9
9
|
before(:each) do
|
10
|
-
path.request_path = '/
|
10
|
+
path.request_path = '/some_dir/light_resize/150x150_image.png'
|
11
11
|
end
|
12
12
|
|
13
|
-
it { expect(path.image_path?).to
|
13
|
+
it { expect(path.image_path?).to be_true }
|
14
14
|
|
15
15
|
it { expect(path.image_path).to eq('/some_dir/image.png') }
|
16
16
|
|
@@ -22,11 +22,13 @@ describe LightResizer::Middleware::Path do
|
|
22
22
|
|
23
23
|
context 'resize with crop' do
|
24
24
|
before(:each) do
|
25
|
-
path.request_path = '/
|
25
|
+
path.request_path = '/some_dir/light_resize/150x150_crop_image.png'
|
26
26
|
end
|
27
27
|
|
28
28
|
it { expect(path.crop_path?).to be_true }
|
29
29
|
|
30
|
+
it { expect(path.image_path).to eq('/some_dir/image.png') }
|
31
|
+
|
30
32
|
it { expect(path.dimensions).to eq('150x150') }
|
31
33
|
|
32
34
|
it { expect(path.prefix).to eq('150x150_crop') }
|
@@ -38,7 +40,7 @@ describe LightResizer::Middleware::Path do
|
|
38
40
|
path.request_path = '/asdada/123acad/some_dir/imdage.png'
|
39
41
|
end
|
40
42
|
|
41
|
-
it { expect(path.image_path?).to
|
43
|
+
it { expect(path.image_path?).to be_false }
|
42
44
|
|
43
45
|
end
|
44
46
|
|
@@ -4,7 +4,7 @@ describe LightResizer::Middleware::Resizer do
|
|
4
4
|
|
5
5
|
let(:resizer) { described_class.new }
|
6
6
|
let(:image_path) { File.join(ROOT, 'fixtures', 'avatar.png') }
|
7
|
-
let(:resize_image_path) { File.join(ROOT, 'fixtures', '
|
7
|
+
let(:resize_image_path) { File.join(ROOT, 'fixtures', 'light_resize', image_name) }
|
8
8
|
|
9
9
|
context 'resize' do
|
10
10
|
|