enhance 0.0.10 → 0.1.0

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.
data/README.md CHANGED
@@ -14,11 +14,15 @@ Rails
14
14
 
15
15
  In your Gemfile
16
16
 
17
- gem 'enhance', '~> 0.0.8'
17
+ gem 'enhance', '~> 0.1.0'
18
18
 
19
19
  In your application.rb file
20
20
 
21
- config.middleware.use "Enhance::Enhancer", Rails.root, :routes => [:attachments, :images]
21
+ config.middleware.use "Enhance::Enhancer" do |config|
22
+ config.match "images", "app/assets/images"
23
+ config.match "attachments", "app/public/attachments"
24
+ config.quality = 80
25
+ end
22
26
 
23
27
  In your views:
24
28
 
@@ -44,26 +48,18 @@ Example of matched URI:
44
48
  Config Options
45
49
  --------------
46
50
 
47
- Enhance::Enhancer.new next_in_chain, root, options
51
+ Enhance::Enhancer.new next_in_chain do |config|
52
+ # ...
53
+ end
48
54
 
49
55
  * next_in_chain: Next Rack Application to use
50
- * root: Where the paths are constructed from
51
- * options: described next
52
56
 
53
- Options
57
+ Config methods
54
58
  -------
55
59
 
56
- * extensions: list of supported extensions | [jpg, png, jpeg, gif]
57
- * routes: list of matched routes | images
58
- * quality: quality of output images | 100
59
- * folders: list of folders to look in | ["public"]
60
- * command_path: path for imagemagick if not in PATH | Paperclip config if available
61
- * cache: folder in which to cache enhanced images | "#{root}/tmp/enhanced"
62
- * max_side: maximum size of the enhanced image | 1024
63
- * file_root: root of the server if not the same as root | root
64
-
65
-
66
- Future changes
67
- --------------
68
-
69
- * I'll probably redo the config to use a block instead of a hash.
60
+ * match(route, path): Match the route and look in path for the images | "", "./"
61
+ * extensions=: list of supported extensions | [jpg, png, jpeg, gif]
62
+ * quality=: quality of output images | 100
63
+ * command_path=: path for imagemagick if not in PATH | Paperclip config if available
64
+ * cache=: folder in which to cache enhanced images | "./tmp/enhanced"
65
+ * max_side=: maximum size of the enhanced image | 1024
@@ -1,10 +1,9 @@
1
1
  require 'utilities'
2
2
  require 'fileutils'
3
+ require 'cgi'
3
4
 
4
- require File.join(File.dirname(__FILE__), 'enhance/urlhelper')
5
+ module Enhance; end
5
6
 
6
- module Enhance
7
- %w( Enhancer ).each do |class_name|
8
- autoload class_name.to_sym, File.join(File.dirname(__FILE__), "enhance/#{class_name.downcase}")
9
- end
7
+ %w( urlhelper enhancer config ).each do |file|
8
+ require File.join(File.dirname(__FILE__), "enhance/#{file}")
10
9
  end
@@ -0,0 +1,42 @@
1
+ class Enhance::Config
2
+
3
+ attr_reader :cache, :quality, :max_side, :command_path, :extensions, :routes, :server
4
+
5
+ def initialize
6
+ @routes = {}
7
+ @quality = 100
8
+ @max_side = 1024
9
+ @command_path = "#{Paperclip.options[:command_path] if defined?(Paperclip)}"
10
+ @extensions = %w( jpg png jpeg gif )
11
+
12
+ self.cache = File.join('tmp', 'enhance')
13
+ end
14
+
15
+ def match(route, path)
16
+ @routes[route] = path
17
+ end
18
+
19
+ def cache=(path)
20
+ FileUtils.mkdir_p(path)
21
+ path = File.realpath(path)
22
+ @cache = path
23
+ @server = Rack::File.new(@cache)
24
+ end
25
+
26
+ def quality=(q)
27
+ @quality = q
28
+ end
29
+
30
+ def max_side=(m)
31
+ @max_side = m
32
+ end
33
+
34
+ def command_path=(path)
35
+ @command_path = path
36
+ @command_path += "/" unless @command_path.empty?
37
+ end
38
+
39
+ def extensions=(*extensions)
40
+ @extensions = extensions
41
+ end
42
+ end
@@ -1,56 +1,60 @@
1
- require 'cgi'
2
1
  class Enhance::Enhancer
3
2
 
4
3
  Geometry = /^(?<geometry>(?<width>\d+)?x?(?<height>\d+)?([\>\<\@\%^!])?)(?<filter>sample)?$/
5
4
 
6
- ## Options
7
- # extensions : list of supported extensions
8
- # routes : list of matched routes
9
- # folders : list of folders to look in
10
- # quality : quality of output images
11
- # command_path : path for imagemagick if not in PATH
12
- # cache : folder in which to cache enhanced images
13
- # max_side : maximum size of the enhanced image
14
- # file_root : root of the server if not the same as root
15
- def initialize app, root, options = {}
16
- root = root.call if root.respond_to? :call
5
+ def initialize app = nil
17
6
  @app = app
18
- @extensions = [options[:extensions] || %w( jpg png jpeg gif )].flatten
19
- @routes = options[:routes] || %w( images )
20
- folders = options[:folders].respond_to?(:call) ? options[:folders].call : options[:folders]
21
- @folders = [folders || File.join(root, "public")].flatten.compact
22
- @quality = options[:quality] || 100
23
- @command_path = options[:convert_path] || "#{Paperclip.options[:command_path] if defined?(Paperclip)}"
24
- @command_path += "/" unless @command_path.empty?
25
- @cache = options[:cache] || File.join(root, "tmp", "enhanced")
26
- @max_side = options[:max_side] || 1024
27
- @file_root = (options[:file_root] || root).to_s
28
- @server = Rack::File.new(@file_root)
7
+ @config = Enhance::Config.new
8
+ yield @config if block_given?
29
9
  end
30
10
 
31
11
  def call env
32
- matches = env['PATH_INFO'].match /(?<filename>(#{@routes.join("|")}).*(#{@extensions.join("|")}))\/(?<geometry>.*)/i
12
+ matches = path_info(env).match regex
33
13
  if matches && !matches['filename'].include?("..")
34
14
  dup._call env, matches
35
15
  else
36
- @app.call env
16
+ _next env
37
17
  end
38
18
  end
39
19
 
40
20
  def _call env, matches
41
- request = @folders.collect_first do |f|
42
- f = File.join(f, matches['filename'])
43
- File.exists?(f) ? f : nil
44
- end
21
+ folder = @config.routes[matches[:folder]] || Dir.pwd
22
+
23
+ request = File.join(folder, matches['filename'])
24
+ destname = File.join(*[matches['folder'], matches['filename']].reject{|s| s.nil? || s.empty?}.compact)
45
25
 
46
- if request && (filename = convert(request, matches['filename'], CGI.unescape(matches['geometry']))) && filename.gsub!(/^#{@file_root}/, '')
26
+ if request && File.exists?(request) && (filename = convert(request, destname, CGI.unescape(matches['geometry'])))
27
+ filename.gsub!(@config.cache, '')
47
28
  env["PATH_INFO"] = filename
48
- @server.call env
29
+ @config.server.call env
49
30
  else
31
+ _next env
32
+ end
33
+ end
34
+
35
+ def _next(env)
36
+ if @app
50
37
  @app.call env
38
+ else
39
+ content = "Not Found"
40
+ [404, {'Content-Type' => 'text/html', 'Content-Length' => content.length.to_s}, [content]]
41
+ end
42
+ end
43
+
44
+ def path_info(env)
45
+ # Matches for Rails using no config
46
+ if @config.routes.empty? && (info = env["action_dispatch.request.path_parameters"])
47
+ [info[:path], info[:format]].compact.join(".")
48
+ else
49
+ env['PATH_INFO']
51
50
  end
52
51
  end
53
52
 
53
+ def regex
54
+ folders = @config.routes.keys
55
+ /^\/?(?<folder>#{folders.join('|')})\/?(?<filename>.*\.(#{@config.extensions.join('|')}))\/(?<geometry>.*)/i
56
+ end
57
+
54
58
  # Finds the image and resizes it if needs be
55
59
  def convert path, filename, geometry
56
60
  # Extract the width and height
@@ -59,37 +63,31 @@ class Enhance::Enhancer
59
63
  ow, oh = original_size path
60
64
 
61
65
  # Resize if needed
62
- if (w.nil? || w.to_i <= @max_side) && (h.nil? || h.to_i <= @max_side) && (ow != w || oh != h)
63
- new_name = File.join(@cache, filename, geometry) + File.extname(filename)
66
+ if (w.nil? || w.to_i <= @config.max_side) && (h.nil? || h.to_i <= @config.max_side) && (ow != w || oh != h)
67
+ new_name = File.join(@config.cache, filename, geometry) + File.extname(filename)
64
68
  resize path, new_name, geometry
65
- else
66
- path
67
69
  end
68
- else
69
- nil
70
70
  end
71
71
  end
72
72
 
73
73
  # Creates the path and resizes the images
74
- def resize source, destination, geometry
74
+ def resize source, destination, geometry
75
75
  FileUtils.mkdir_p File.dirname(destination)
76
76
 
77
77
  match = geometry.match Geometry
78
-
78
+
79
79
  method = match['filter'] || 'resize'
80
-
81
80
  unless File.exists?(destination) && File.mtime(destination) > File.mtime(source)
82
- command = "#{@command_path}convert \"#{source}\" -#{method} \"#{match['geometry']}\" -quality #{@quality} \"#{destination}\""
81
+ command = "#{@config.command_path}convert \"#{source}\" -#{method} \"#{match['geometry']}\" -quality #{@config.quality} \"#{destination}\""
83
82
  puts command
84
83
  `#{command}`
85
84
  end
86
-
87
85
  destination
88
86
  end
89
87
 
90
88
  # Finds the size of the original image
91
89
  def original_size filename
92
- `#{@command_path}identify -format '%w %h' #{filename}`.split(/\s/)
90
+ `#{@config.command_path}identify -format '%w %h' #{filename}`.split(/\s/)
93
91
  end
94
92
 
95
93
  end
@@ -1,3 +1,3 @@
1
1
  module Enhance
2
- VERSION = '0.0.10'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -15,10 +15,8 @@ class EnhanceTest < Test::Unit::TestCase
15
15
  include Rack::Test::Methods
16
16
 
17
17
  def app
18
- Enhance::Enhancer.new( FourOhFourApp.new, File.dirname(__FILE__),
19
- :cache => File.join(File.dirname(__FILE__), "tmp"),
20
- :folders => File.dirname(__FILE__)
21
- )
18
+ Enhance::Enhancer.new FourOhFourApp.new do |config|
19
+ end
22
20
  end
23
21
 
24
22
  def test_404
@@ -53,7 +51,7 @@ class EnhanceTest < Test::Unit::TestCase
53
51
 
54
52
  def test_image_oversize
55
53
  get "/images/test.jpg/2000"
56
- assert_size 2000, nil, :<
54
+ assert last_response.not_found?
57
55
  end
58
56
 
59
57
  def assert_size width, height, comparator = :==
@@ -62,7 +60,6 @@ class EnhanceTest < Test::Unit::TestCase
62
60
  f.write last_response.body
63
61
  end
64
62
  identify = `identify #{dump}`.match /\s(?<width>[0-9]+)x(?<height>[0-9]+)/
65
-
66
63
  assert identify[:width].to_i.send(comparator, width) if width
67
64
  assert identify[:height].to_i.send(comparator, height) if height
68
65
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enhance
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.10
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2011-08-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
16
- requirement: !ruby/object:Gem::Requirement
16
+ requirement: &70278122273260 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,15 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '0'
24
+ version_requirements: *70278122273260
30
25
  - !ruby/object:Gem::Dependency
31
26
  name: utilities
32
- requirement: !ruby/object:Gem::Requirement
27
+ requirement: &70278122271760 !ruby/object:Gem::Requirement
33
28
  none: false
34
29
  requirements:
35
30
  - - ! '>='
@@ -37,15 +32,10 @@ dependencies:
37
32
  version: '0'
38
33
  type: :runtime
39
34
  prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
45
- version: '0'
35
+ version_requirements: *70278122271760
46
36
  - !ruby/object:Gem::Dependency
47
37
  name: rack-test
48
- requirement: !ruby/object:Gem::Requirement
38
+ requirement: &70278122271120 !ruby/object:Gem::Requirement
49
39
  none: false
50
40
  requirements:
51
41
  - - ! '>='
@@ -53,12 +43,7 @@ dependencies:
53
43
  version: '0'
54
44
  type: :development
55
45
  prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
59
- - - ! '>='
60
- - !ruby/object:Gem::Version
61
- version: '0'
46
+ version_requirements: *70278122271120
62
47
  description: Middleware to "enhance" and resize images on the fly.
63
48
  email: gmalette@gmail.com
64
49
  executables: []
@@ -68,6 +53,7 @@ files:
68
53
  - README.md
69
54
  - enhance.gemspec
70
55
  - lib/enhance.rb
56
+ - lib/enhance/config.rb
71
57
  - lib/enhance/enhancer.rb
72
58
  - lib/enhance/urlhelper.rb
73
59
  - lib/enhance/version.rb
@@ -94,7 +80,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
94
80
  version: '0'
95
81
  requirements: []
96
82
  rubyforge_project:
97
- rubygems_version: 1.8.24
83
+ rubygems_version: 1.8.10
98
84
  signing_key:
99
85
  specification_version: 3
100
86
  summary: Image resizing on the fly