scale_down 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGES +5 -0
- data/README.rdoc +24 -11
- data/lib/scale_down/controller.rb +4 -3
- data/lib/scale_down/version.rb +1 -1
- data/lib/scale_down.rb +1 -1
- data/test/scale_down/controller_test.rb +3 -3
- data/test/scale_down_test.rb +5 -5
- metadata +6 -5
data/CHANGES
ADDED
data/README.rdoc
CHANGED
@@ -6,24 +6,37 @@ Supports cropping images and converts CMYK to RGB.
|
|
6
6
|
|
7
7
|
Images are scaled based upon their URL. An HMAC signature is used to prevent malicious scaling of images (ie: bring your server down.)
|
8
8
|
|
9
|
+
The schema is simple
|
10
|
+
|
11
|
+
http://:server/:geometry/:path_to_file/:filename?:hmac_signature
|
12
|
+
|
13
|
+
:server is the address running ScaleDown
|
14
|
+
|
15
|
+
:geometry is widtxheight. There can be an optional `-crop` flag attached.
|
16
|
+
|
17
|
+
:path_to_file is the public path to the file
|
18
|
+
|
19
|
+
:filename is the name of the image to scale
|
20
|
+
|
21
|
+
:hmac_signature is security measure to validate the request
|
22
|
+
|
9
23
|
For example
|
10
24
|
|
11
|
-
|
12
|
-
http://images.example.com/images/logo.png/400x300/A3SDACEDF
|
25
|
+
http://images.example.com/400x300/images/logo.png?A3SDACEDF
|
13
26
|
|
14
27
|
would 301 redirect to the scaled image
|
15
28
|
|
16
|
-
http://images.example.com/images/scaled/logo
|
29
|
+
http://images.example.com/images/scaled/400x300/logo.png
|
17
30
|
|
18
31
|
To crop an image include the 'cropped' option
|
19
32
|
|
20
|
-
http://images.example.com/images/logo.png
|
33
|
+
http://images.example.com/400x300-cropped/images/logo.png?A3SDACEDF
|
21
34
|
|
22
35
|
Use 'auto' to have an image scale to one dimension or another.
|
23
36
|
|
24
37
|
For example, to ensure an image is 300 pixels wide
|
25
38
|
|
26
|
-
http://images.example.com/images/logo.png
|
39
|
+
http://images.example.com/300xauto/images/logo.png/?A3SDACEDF
|
27
40
|
|
28
41
|
There is a very simple `/info` function for getting the image dimesions. It just returns a string with the WIDTHxHEIGHT.
|
29
42
|
|
@@ -63,20 +76,20 @@ Start the server using whatever server you want.
|
|
63
76
|
|
64
77
|
== Generating the URL
|
65
78
|
|
66
|
-
In your application
|
79
|
+
In your application you will need to generate the URL for the image source.
|
67
80
|
|
68
|
-
Filenames can have characters (# ? /) which may need to be escaped.
|
81
|
+
Filenames can have characters (# ? /) which may need to be escaped.
|
69
82
|
|
70
|
-
|
83
|
+
Filenames should be CGI escaped before the HMAC is generated, and only escape the filename, not the path or options.
|
71
84
|
|
72
85
|
# Example ruby function to generate signed URL
|
73
86
|
require 'HMAC::SHA1'
|
74
87
|
require 'cgi'
|
75
88
|
|
76
|
-
# ie signed_image_url('
|
89
|
+
# ie signed_image_url('images', 'picture.png', '400x300-cropped')
|
77
90
|
def signed_image_url(path, filename, options)
|
78
|
-
hmac = HMAC::SHA1.new("secret").update(
|
79
|
-
"http://images.myserver.com
|
91
|
+
hmac = HMAC::SHA1.new("secret").update("/#{options}/#{path}/#{filename}").to_s[0..8]
|
92
|
+
"http://images.myserver.com/#{options}/#{path}/#{CGI.escape(filename)}?#{hmac}"
|
80
93
|
end
|
81
94
|
|
82
95
|
Of course this could be done in PHP, Python, or whatever language your application is running.
|
@@ -17,17 +17,18 @@ class ScaleDown::Controller < Sinatra::Application
|
|
17
17
|
end
|
18
18
|
end
|
19
19
|
|
20
|
-
# get '/*/:filename/:geometry
|
20
|
+
# get '/*/:filename/:geometry?:hmac'
|
21
21
|
# is what I want, but this fails when the URL includes things like %23 (an encoded hash tag)
|
22
22
|
get '/*' do
|
23
23
|
parts = params[:splat].join("/").split("/")
|
24
24
|
|
25
25
|
params = {
|
26
|
-
:hmac =>
|
27
|
-
:geometry => parts.
|
26
|
+
:hmac => request.env["QUERY_STRING"],
|
27
|
+
:geometry => parts.shift,
|
28
28
|
:filename => parts.pop,
|
29
29
|
:splat => parts
|
30
30
|
}
|
31
|
+
|
31
32
|
path, status = dispatch(params)
|
32
33
|
|
33
34
|
# TODO Eh? Shouldn't it be if 301
|
data/lib/scale_down/version.rb
CHANGED
data/lib/scale_down.rb
CHANGED
@@ -16,14 +16,14 @@ class ScaleDown::Controller::Test < Test::Unit::TestCase
|
|
16
16
|
:hmac => "HMAC").
|
17
17
|
returns ["path","status"]
|
18
18
|
|
19
|
-
get '/user/path/filename.png
|
19
|
+
get '/400x300-cropped-grayscale/user/path/filename.png?HMAC'
|
20
20
|
end
|
21
21
|
end
|
22
22
|
|
23
23
|
context "a valid request" do
|
24
24
|
should "redirect to the image path" do
|
25
25
|
ScaleDown::Dispatcher.expects(:process).returns ["/image-path", 301]
|
26
|
-
get "/path/filename
|
26
|
+
get "/geo/path/filename?hmac"
|
27
27
|
|
28
28
|
assert_equal 301, last_response.status
|
29
29
|
assert_equal "/image-path", last_response["Location"]
|
@@ -35,7 +35,7 @@ class ScaleDown::Controller::Test < Test::Unit::TestCase
|
|
35
35
|
should "respond with a 403 and error message" do
|
36
36
|
ScaleDown::Dispatcher.expects(:process).returns ["Error description", 403]
|
37
37
|
|
38
|
-
get "/path/filename
|
38
|
+
get "/geo/path/filename?hmac"
|
39
39
|
|
40
40
|
assert_equal 403, last_response.status
|
41
41
|
assert_match "Error", last_response.body
|
data/test/scale_down_test.rb
CHANGED
@@ -8,7 +8,7 @@ class ScaleDown::Test < Test::Unit::TestCase
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def valid_get(path)
|
11
|
-
get "#{path}
|
11
|
+
get "#{path}?#{ScaleDown.hmac(path)}"
|
12
12
|
end
|
13
13
|
|
14
14
|
context "ScaleDown" do
|
@@ -21,7 +21,7 @@ class ScaleDown::Test < Test::Unit::TestCase
|
|
21
21
|
|
22
22
|
context "HMAC" do
|
23
23
|
setup do
|
24
|
-
hmac = HMAC::SHA1.new("secret").update("/file/path/filename.png
|
24
|
+
hmac = HMAC::SHA1.new("secret").update("/400x300-crop/file/path/filename.png").to_s
|
25
25
|
|
26
26
|
@params = {
|
27
27
|
:path => "file/path",
|
@@ -53,7 +53,7 @@ class ScaleDown::Test < Test::Unit::TestCase
|
|
53
53
|
end
|
54
54
|
|
55
55
|
should "get an image and scale it" do
|
56
|
-
valid_get '/test_images/example_1/graphic.png
|
56
|
+
valid_get '/400x300-cropped/test_images/example_1/graphic.png'
|
57
57
|
assert_equal 301, last_response.status
|
58
58
|
assert_equal "/test_images/example_1/scaled/graphic-400x300-cropped.png", last_response["Location"]
|
59
59
|
assert File.exists?("/tmp/scale_down/test_images/example_1/scaled/graphic-400x300-cropped.png")
|
@@ -65,10 +65,10 @@ class ScaleDown::Test < Test::Unit::TestCase
|
|
65
65
|
end
|
66
66
|
|
67
67
|
should "get an invalid image and return a 500" do
|
68
|
-
valid_get '/test_images/example_2/invalid_jpeg.jpg
|
68
|
+
valid_get '/400x300-cropped/test_images/example_2/invalid_jpeg.jpg'
|
69
69
|
|
70
70
|
assert_equal 500, last_response.status
|
71
|
-
assert !File.exists?("/tmp/scale_down/test_images/example_2/scaled/
|
71
|
+
assert !File.exists?("/tmp/scale_down/test_images/example_2/scaled/400x300-cropped/invalid_jpeg.jpg")
|
72
72
|
end
|
73
73
|
end
|
74
74
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scale_down
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 2
|
9
|
+
- 0
|
10
|
+
version: 0.2.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- John Weir
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-11-
|
18
|
+
date: 2010-11-19 00:00:00 -05:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -139,6 +139,7 @@ extra_rdoc_files: []
|
|
139
139
|
files:
|
140
140
|
- .bundle/config
|
141
141
|
- .gitignore
|
142
|
+
- CHANGES
|
142
143
|
- Gemfile
|
143
144
|
- Gemfile.lock
|
144
145
|
- README.rdoc
|