Class: Imageproxy::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/imageproxy/server.rb

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



11
12
13
# File 'lib/imageproxy/server.rb', line 11

def initialize
  @file_server = Rack::File.new(File.join(File.expand_path(File.dirname(__FILE__)), "..", "public"))
end

Instance Method Details

#call(env) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/imageproxy/server.rb', line 15

def call(env)
  request = Rack::Request.new(env)
  options = Options.new(request.path_info, request.params)
  user_agent = request.env["HTTP_USER_AGENT"]
  cachetime = config(:cache_time) ? config(:cache_time) : 86400

  case options.command
    when "convert", "process", nil
      check_signature request, options
      check_domain options
      check_size options

      file = convert_file(options, user_agent)
      class << file
        alias to_path path
      end

      file.open
      [200, {"Cache-Control" => "max-age=#{cachetime}, must-revalidate"}.merge(content_type(file, options)), file]
    when "identify"
      check_signature request, options
      check_domain options

      [200, {"Content-Type" => "text/plain"}, [Identify.new(options).execute(user_agent)]]
    when "selftest"
      [200, {"Content-Type" => "text/html"}, [Selftest.html(request, config?(:signature_required), config(:signature_secret))]]
    else
      @file_server.call(env)
  end
rescue
  STDERR.puts "Request failed: #{options}"
  STDERR.puts $!
  STDERR.puts $!.backtrace.join("\n") if config?(:verbose)
  [500, {"Content-Type" => "text/plain"}, ["Error (#{$!})"]]
end