Class: ScaleDown::Dispatcher
- Inherits:
-
Object
- Object
- ScaleDown::Dispatcher
- Defined in:
- lib/scale_down/dispatcher.rb
Class Method Summary collapse
-
.info(relative_path) ⇒ Object
TODO return a JSON response with a full set of image details.
- .process(params) ⇒ Object
Instance Method Summary collapse
- #crop? ⇒ Boolean
- #filename ⇒ Object
- #image_options ⇒ Object
-
#initialize(params) ⇒ Dispatcher
constructor
A new instance of Dispatcher.
- #redirect_code ⇒ Object
- #redirect_path ⇒ Object
- #root_file_exists? ⇒ Boolean
- #root_path ⇒ Object
- #scale ⇒ Object
- #scaled_extension ⇒ Object
- #scaled_file_exists? ⇒ Boolean
- #scaled_file_path ⇒ Object
- #scaled_filename ⇒ Object
- #target ⇒ Object
- #target_is_label? ⇒ Boolean
- #valid_hmac? ⇒ Boolean
Constructor Details
#initialize(params) ⇒ Dispatcher
Returns a new instance of Dispatcher.
35 36 37 |
# File 'lib/scale_down/dispatcher.rb', line 35 def initialize(params) @params = params end |
Class Method Details
.info(relative_path) ⇒ Object
TODO return a JSON response with a full set of image details
23 24 25 26 27 28 29 30 31 32 |
# File 'lib/scale_down/dispatcher.rb', line 23 def info(relative_path) path = [ScaleDown.public_folder, relative_path].join("/") if File.exists?(path) GC.start image = Magick::Image.read(path).first [image.columns, image.rows].join('x') else nil end end |
.process(params) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/scale_down/dispatcher.rb', line 7 def process(params) dispatcher = new(params) ScaleDown.logger.info "Dipatcher#process #{dispatcher.root_path}" $0= "scale_down Dipatcher#process #{dispatcher.root_path}" return ["Missing file #{dispatcher.root_path}", 404] unless dispatcher.root_file_exists? return [dispatcher.redirect_path, dispatcher.redirect_code] if dispatcher.scaled_file_exists? return ["Invalid HMAC signature", 403] unless dispatcher.valid_hmac? return ["File failed to scale. The file may be corrupt.", 500] unless dispatcher.scale [dispatcher.redirect_path, dispatcher.redirect_code] end |
Instance Method Details
#crop? ⇒ Boolean
64 65 66 |
# File 'lib/scale_down/dispatcher.rb', line 64 def crop? @crop ||= ! @params[:target].match(/-crop\Z/).nil? end |
#filename ⇒ Object
105 106 107 |
# File 'lib/scale_down/dispatcher.rb', line 105 def filename @params[:filename].split(".")[0...-1].join(".") end |
#image_options ⇒ Object
39 40 41 42 43 44 45 46 47 |
# File 'lib/scale_down/dispatcher.rb', line 39 def width, height = (target_is_label? ? ScaleDown.labels[target] : target).split("x") { :height => height.to_i, :width => width.to_i, :crop => crop? } end |
#redirect_code ⇒ Object
76 77 78 |
# File 'lib/scale_down/dispatcher.rb', line 76 def redirect_code @params[:filename].match(/(png|jpg)$/) ? 302 : 301 end |
#redirect_path ⇒ Object
80 81 82 |
# File 'lib/scale_down/dispatcher.rb', line 80 def redirect_path ["/"+@params[:path], @params[:target], scaled_filename].join("/") end |
#root_file_exists? ⇒ Boolean
84 85 86 |
# File 'lib/scale_down/dispatcher.rb', line 84 def root_file_exists? File.exists? root_path end |
#root_path ⇒ Object
92 93 94 95 |
# File 'lib/scale_down/dispatcher.rb', line 92 def root_path root = @params[:path].gsub(/\/scaled$/,"") File.join(ScaleDown.public_folder, root, @params[:filename]) end |
#scale ⇒ Object
49 50 51 52 53 54 |
# File 'lib/scale_down/dispatcher.rb', line 49 def scale ScaleDown::Image.scale \ :file => root_path, :out => scaled_file_path, :options => end |
#scaled_extension ⇒ Object
109 110 111 112 |
# File 'lib/scale_down/dispatcher.rb', line 109 def scaled_extension ext = @params[:filename].split(".").last ext == "png" ? ext : "jpg" end |
#scaled_file_exists? ⇒ Boolean
88 89 90 |
# File 'lib/scale_down/dispatcher.rb', line 88 def scaled_file_exists? File.exists? scaled_file_path end |
#scaled_file_path ⇒ Object
97 98 99 |
# File 'lib/scale_down/dispatcher.rb', line 97 def scaled_file_path File.join(ScaleDown.public_folder, redirect_path) end |
#scaled_filename ⇒ Object
101 102 103 |
# File 'lib/scale_down/dispatcher.rb', line 101 def scaled_filename "#{filename}.#{scaled_extension}" end |
#target ⇒ Object
56 57 58 |
# File 'lib/scale_down/dispatcher.rb', line 56 def target @dimensions ||= @params[:target].split(/-crop\Z/).first end |
#target_is_label? ⇒ Boolean
60 61 62 |
# File 'lib/scale_down/dispatcher.rb', line 60 def target_is_label? @target_is_label ||= ScaleDown.labels.keys.include?(target) end |
#valid_hmac? ⇒ Boolean
68 69 70 71 72 73 74 |
# File 'lib/scale_down/dispatcher.rb', line 68 def valid_hmac? if target_is_label? true else ScaleDown.valid_hmac?(@params) end end |