Class: Rack::Grid

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

Defined Under Namespace

Classes: ConnectionError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app, options = {}) ⇒ Grid

Returns a new instance of Grid.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rack_grid.rb', line 9

def initialize(app, options = {})
  opts = {}
  options.each { |k,v|  opts[k.to_s] = v }
  options = {
    'host'    => 'localhost',
    'prefix'  => 'grid',
    'port'    => Mongo::Connection::DEFAULT_PORT
  }.merge(opts)

  @app        = app
  @host       = options['host']
  @port       = options['port']
  @database   = options['database']
  @prefix     = options['prefix']
  @username   = options['username']
  @password   = options['password']
  @db         = options['db']

  @cache_control = options['cache_control']
end

Instance Attribute Details

#databaseObject (readonly)

Returns the value of attribute database.



7
8
9
# File 'lib/rack_grid.rb', line 7

def database
  @database
end

#hostObject (readonly)

Returns the value of attribute host.



7
8
9
# File 'lib/rack_grid.rb', line 7

def host
  @host
end

#passwordObject (readonly)

Returns the value of attribute password.



7
8
9
# File 'lib/rack_grid.rb', line 7

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port.



7
8
9
# File 'lib/rack_grid.rb', line 7

def port
  @port
end

#prefixObject (readonly)

Returns the value of attribute prefix.



7
8
9
# File 'lib/rack_grid.rb', line 7

def prefix
  @prefix
end

#usernameObject (readonly)

Returns the value of attribute username.



7
8
9
# File 'lib/rack_grid.rb', line 7

def username
  @username
end

Instance Method Details

#call(env) ⇒ Object

Strip the _id out of the path. This allows the user to send something like /grid/4ba69fde8c8f369a6e000003/filename.jpg to find the file with an id of 4ba69fde8c8f369a6e000003.



40
41
42
43
44
45
46
47
48
# File 'lib/rack_grid.rb', line 40

def call(env)
  @env = env
  request = Rack::Request.new(@env)
  if request.path_info =~ /^\/#{prefix}\/(\w+).*$/
    grid_request($1)
  else
    @app.call(env)
  end
end

#dbObject



30
31
32
33
34
# File 'lib/rack_grid.rb', line 30

def db
  @db = @db.call()  if Proc === @db
  connect!  unless @db
  @db
end

#grid_request(id) ⇒ Object

Get file from GridFS or return a 404



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/rack_grid.rb', line 52

def grid_request(id)
  file = Mongo::Grid.new(db).get(BSON::ObjectId.from_string(id))
  etag, last_modified = file.instance_variable_get(:@md5), Time.at( file.upload_date.to_i )
  headers = {
    'ETag' => "\"#{etag}\"",
    'Last-Modified' => last_modified.httpdate,
    'Cache-Control' => cache_control_header,
  }
  if not_modified?( etag, last_modified )
    [304, headers, ['Not Modified']]
  else
    [200, headers.update('Content-Type' => file.content_type), [file.read]]
  end
rescue Mongo::GridError, BSON::InvalidObjectId
  [404, {'Content-Type' => 'text/plain'}, ['File not found.']]
end