Class: DecodeBase64Image

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

Instance Method Summary collapse

Instance Method Details

#call(env) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/decode_base64_image.rb', line 5

def call(env)
  request = Rack::Request.new(env)
  content_type = request['content_type'] || 'image/png'
  image_data = request['image_data']

  begin
    raise "Missing required parameter 'image_data'" if image_data.to_s.empty?      
    response = Rack::Response.new(Base64.decode64(image_data), 200, { 'Content-Type' => content_type })
    # set caching information
    response.headers['Cache-Control'] = "public, max-age=#{1.month}"
  rescue Exception => e
    response = Rack::Response.new(e.message, 500, { 'Content-Type' => 'text/plain' })
  end

  response.to_a
end