class Apache::ImageResizer

Constants

VERSION

Public Class Methods

new(options = {}) click to toggle source

Creates a new RubyHandler instance for the Apache web server. It is to be installed as a custom 404 ErrorDocument handler.

   # File lib/apache/image_resizer.rb
42 def initialize(options = {})
43   @verbosity   = options[:verbosity]   || DEFAULT_VERBOSITY
44   @max_dim     = options[:max_dim]     || DEFAULT_MAX_DIM
45   @prefix_re   = options[:prefix_re]   || DEFAULT_PREFIX_RE
46   @source_dir  = options[:source_dir]  || DEFAULT_SOURCE_DIR
47   @proxy_cache = options[:proxy_cache] || DEFAULT_PROXY_CACHE
48   @enlarge     = options[:enlarge]
49   @secret      = options[:secret]
50 end

Public Instance Methods

handler(request, &block) click to toggle source

If the current request asked for a resource that’s not there, it will be checked for resize directives and treated accordingly. If no matching resource could be found, the original error will be thrown.

   # File lib/apache/image_resizer.rb
56 def handler(request, &block)
57   request.add_common_vars  # REDIRECT_URL, REDIRECT_QUERY_STRING
58   env = request.subprocess_env
59 
60   url   = env['REDIRECT_URL']
61   query = env['REDIRECT_QUERY_STRING']
62   url  += "?#{query}" unless query.nil? || query.empty?
63 
64   block ||= lambda { |msg|
65     log(2) { "#{url} - Elapsed #{Time.now - request.request_time} [#{msg}]" }
66   }
67 
68   path, prefix, directives, dir = parse_url(url,
69     base = request.path_info, @prefix_re, &block)
70 
71   return DECLINED unless path
72 
73   log(2) {{ :Base => base, :Prefix => prefix, :Dir => dir, :Path => path }}
74 
75   source, target = get_paths(request, path, base,
76     prefix, @source_dir, dir, @proxy_cache, @secret)
77 
78   if source
79     log(2) {{ :Source => source, :Target => target, :Directives => directives }}
80     return DECLINED unless img = resize(source, target, directives, @enlarge, &block)
81   end
82 
83   return DECLINED unless send_image(request, target, img, &block)
84 
85   log { "#{url} - Elapsed #{Time.now - request.request_time}" }
86 
87   OK
88 end