rack-gridfs-thumb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,24 @@
1
+ Rack::GridFSThumb
2
+ =================
3
+
4
+ Rack::GridFS is Rack middleware for resizing images served via [Rack::GridFS][https://github.com/skinandbones/rack-gridfs]. Resizing is done via [MiniMagick] [https://github.com/probablycorey/mini_magick] and all the usual geometry strings are accepted. See http://www.imagemagick.org/script/command-line-processing.php#geometry for more details.
5
+
6
+ For example:
7
+
8
+ GET /gridfs/someobjectid/100x100
9
+
10
+ Installation
11
+ ------------
12
+
13
+ gem install rack-gridfs-thumb
14
+
15
+ Usage
16
+ -----
17
+
18
+ require 'rack/gridfs'
19
+ require 'rack/gridfs-thumb'
20
+
21
+ prefix = 'gridfs'
22
+
23
+ use Rack::GridFS, :prefix => prefix, :hostname => 'localhost', :port => 27017, :database => 'test'
24
+ use Rack::GridFSThumb, :prefix => prefix
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require 'bundler'
2
+ require 'rake/testtask'
3
+
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ Rake::TestTask.new(:test) do |test|
7
+ test.libs << 'lib' << 'test'
8
+ test.pattern = 'test/**/test_*.rb'
9
+ test.verbose = true
10
+ end
@@ -0,0 +1,80 @@
1
+ require 'mini_magick'
2
+
3
+ module Rack
4
+ class GridFSThumb
5
+ def initialize(app, options = {})
6
+ @app = app
7
+ @options = options
8
+ @options[:prefix] ||= 'gridfs'
9
+ end
10
+
11
+ def call(env)
12
+ @request = Rack::Request.new(env)
13
+
14
+ response = catch(:respond) do
15
+ match(@request.path_info) do |path, geometry|
16
+ status, @headers, image = request_image(path)
17
+ thumb = thumbnail(image, geometry)
18
+
19
+ response_for(thumb)
20
+ end
21
+ end
22
+
23
+ response || @app.call(env)
24
+ end
25
+
26
+ private
27
+
28
+ def route
29
+ %r{(/#{@options[:prefix]}/[^/]+)/([^/]+)/?$}
30
+ end
31
+
32
+ def match(path_info)
33
+ if match = path_info.match(route)
34
+ yield(match[1], match[2])
35
+ end
36
+ end
37
+
38
+ def thumbnail(image, geometry)
39
+ begin
40
+ MiniMagick::Image.read(image.read).tap do |thumb|
41
+ thumb.resize(geometry)
42
+ end
43
+ rescue MiniMagick::Error => e
44
+ throw :respond, bad_request
45
+ end
46
+ end
47
+
48
+ def request_image(path_info)
49
+ env = @request.env.merge('PATH_INFO' => path_info)
50
+ status, headers, body = @app.call(env)
51
+
52
+ unless status >= 200 && status < 300
53
+ throw :respond, [status, headers, body]
54
+ end
55
+
56
+ [status, headers, body]
57
+ end
58
+
59
+ def response_for(thumb)
60
+ headers = { 'Content-Length' => ::File.new(thumb.path).size.to_s }
61
+ throw :respond, [200, @headers.merge(headers), enumerator(thumb.path)]
62
+ end
63
+
64
+ def enumerator(path)
65
+ Enumerator.new do |yielder|
66
+ ::File.open(path) do |f|
67
+ while chunk = f.read(8192)
68
+ yielder << chunk
69
+ end
70
+ end
71
+ end
72
+ end
73
+
74
+ def bad_request
75
+ body = "Invalid geometry specified in #{@request.path_info}"
76
+ headers = { 'Content-Type' => 'text/plain', 'Content-Length' => body.length.to_s }
77
+ [400, headers, [body]]
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class GridFSThumb
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require 'rack/gridfs-thumb'
@@ -0,0 +1,24 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ require 'rack/version'
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = 'rack-gridfs-thumb'
7
+ s.version = Rack::GridFSThumb::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ['Scott Nelson']
10
+ s.email = ['scottbnel@gmail.com']
11
+ s.homepage = 'http://github.com/scttnlsn/rack-gridfs-thumb'
12
+ s.summary = "rack-gridfs-thumb-#{s.version}"
13
+ s.description = 'Rack middleware for resizing images served via Rack::GridFS'
14
+
15
+ s.add_dependency('rack')
16
+ s.add_dependency('mini_magick', '>= 3.3')
17
+
18
+ s.add_development_dependency('rack-test')
19
+
20
+ s.files = `git ls-files`.split("\n")
21
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
22
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
23
+ s.require_paths = ['lib']
24
+ end
data/test/image.png ADDED
Binary file
@@ -0,0 +1,74 @@
1
+ require 'mini_magick'
2
+ require 'rack/gridfs-thumb'
3
+ require 'rack/test'
4
+ require 'test/unit'
5
+
6
+ class Rack::GridFSThumbTest < Test::Unit::TestCase
7
+ include Rack::Test::Methods
8
+
9
+ def setup
10
+ @id = 'test_id'
11
+ @image_path = File.join(File.dirname(__FILE__), 'image.png')
12
+ @image_blob = File.new(@image_path, 'rb').read
13
+ end
14
+
15
+ def app
16
+ @app ||= image_app
17
+ Rack::GridFSThumb.new(@app, :prefix => 'gridfs')
18
+ end
19
+
20
+ def test_not_called_without_geometry
21
+ get "/gridfs/#{@id}"
22
+
23
+ assert_equal 200, last_response.status
24
+ assert_equal @image_blob.length.to_s, last_response.headers['Content-Length']
25
+ assert_equal @image_blob, last_response.body
26
+ end
27
+
28
+ def test_not_called_when_not_found
29
+ @app = not_found_app
30
+
31
+ get "/gridfs/#{@id}/100x100"
32
+
33
+ assert_equal 404, last_response.status
34
+ end
35
+
36
+ def test_bad_request
37
+ get "/gridfs/#{@id}/test"
38
+
39
+ expected_body = "Invalid geometry specified in #{last_request.path_info}"
40
+
41
+ assert_equal 400, last_response.status
42
+ assert_equal expected_body.length.to_s, last_response.headers['Content-Length']
43
+ assert_equal expected_body, last_response.body
44
+ end
45
+
46
+ def test_resize
47
+ get "/gridfs/#{@id}/100x100"
48
+
49
+ expected = MiniMagick::Image.open(@image_path)
50
+ expected.resize('100x100')
51
+ result = MiniMagick::Image.read(last_response.body)
52
+
53
+ assert_equal 200, last_response.status
54
+ assert_equal expected[:size].to_s, last_response.headers['Content-Length']
55
+ assert_equal expected[:dimensions], result[:dimensions]
56
+ assert_equal expected[:format], result[:format]
57
+ end
58
+
59
+ private
60
+
61
+ def image_app
62
+ image = File.new(@image_path, 'rb')
63
+ headers = { 'Content-Length' => image.size.to_s }
64
+ Proc.new do |env|
65
+ [200, headers, image]
66
+ end
67
+ end
68
+
69
+ def not_found_app
70
+ Proc.new do |env|
71
+ [404, { 'Content-Length' => 9 }, ['Not Found']]
72
+ end
73
+ end
74
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-gridfs-thumb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Scott Nelson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-14 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rack
16
+ requirement: &2152191760 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *2152191760
25
+ - !ruby/object:Gem::Dependency
26
+ name: mini_magick
27
+ requirement: &2152190700 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '3.3'
33
+ type: :runtime
34
+ prerelease: false
35
+ version_requirements: *2152190700
36
+ - !ruby/object:Gem::Dependency
37
+ name: rack-test
38
+ requirement: &2152189560 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ! '>='
42
+ - !ruby/object:Gem::Version
43
+ version: '0'
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: *2152189560
47
+ description: Rack middleware for resizing images served via Rack::GridFS
48
+ email:
49
+ - scottbnel@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - README.md
55
+ - Rakefile
56
+ - lib/rack-gridfs-thumb.rb
57
+ - lib/rack/gridfs-thumb.rb
58
+ - lib/rack/version.rb
59
+ - rack-gridfs-thumb.gemspec
60
+ - test/image.png
61
+ - test/test_gridfs_thumb.rb
62
+ homepage: http://github.com/scttnlsn/rack-gridfs-thumb
63
+ licenses: []
64
+ post_install_message:
65
+ rdoc_options: []
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ none: false
70
+ requirements:
71
+ - - ! '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ! '>='
78
+ - !ruby/object:Gem::Version
79
+ version: '0'
80
+ requirements: []
81
+ rubyforge_project:
82
+ rubygems_version: 1.8.10
83
+ signing_key:
84
+ specification_version: 3
85
+ summary: rack-gridfs-thumb-0.1.0
86
+ test_files:
87
+ - test/image.png
88
+ - test/test_gridfs_thumb.rb