Class: Visor::Image::GetImage

Inherits:
Goliath::API
  • Object
show all
Includes:
Common::Exception, Common::Util
Defined in:
lib/image/routes/get_image.rb

Overview

Get image data and metadata for the given id.

Instance Method Summary collapse

Instance Method Details

#exit_error(code, message) ⇒ Array

Produce an HTTP response with an error code and message.

Parameters:

  • code (Fixnum)

    The error code.

  • message (String)

    The error message.

Returns:

  • (Array)

    The HTTP response containing an error code and its message.



73
74
75
76
# File 'lib/image/routes/get_image.rb', line 73

def exit_error(code, message)
  logger.error message
  [code, {}, {code: code, message: message}]
end

#on_close(env) ⇒ Object

On connection close log a message.

Parameters:

  • env (Object)

    The Goliath environment variables.



62
63
64
# File 'lib/image/routes/get_image.rb', line 62

def on_close(env)
  logger.info 'Connection closed'
end

#on_headers(env, headers) ⇒ Object

Pre-process headers as they arrive and load them into a environment variable.

Parameters:

  • env (Object)

    The Goliath environment variables.

  • headers (Object)

    The incoming request HTTP headers.



18
19
20
21
# File 'lib/image/routes/get_image.rb', line 18

def on_headers(env, headers)
  logger.debug "Received headers: #{headers.inspect}"
  env['headers'] = headers
end

#response(env) ⇒ Object

Query database to retrieve the wanted image meta and return it in headers, along with the image file, if any, streaming it in request body.

Parameters:

  • env (Object)

    The Goliath environment variables.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/image/routes/get_image.rb', line 28

def response(env)
  begin
    authorize(env, vas)
    meta = vms.get_image(params[:id])
    uri  = meta[:location]
    if uri
      store = Visor::Image::Store.get_backend(uri, configs)
      store.file_exists?
    end
  rescue Forbidden => e
    return exit_error(403, e.message)
  rescue NotFound => e
    return exit_error(404, e.message)
  rescue InternalError => e
    exit_error(503, e.message)
  end

  custom  = {'Content-Type' => 'application/octet-stream', 'X-Stream' => 'Goliath'}
  headers = push_meta_into_headers(meta, custom)

  if uri
    EM.next_tick do
      store.get { |chunk| chunk ? env.chunked_stream_send(chunk) : env.chunked_stream_close }
    end
    chunked_streaming_response(200, headers)
  else
    [200, headers, nil]
  end
end