Class: GridfsController

Inherits:
ActionController::Metal
  • Object
show all
Defined in:
app/controllers/gridfs_controller.rb

Instance Method Summary collapse

Instance Method Details

#serveObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/controllers/gridfs_controller.rb', line 4

def serve
  gridfs_path = env["PATH_INFO"].gsub("/files/", "")

  database_name   = Mongoid::Config.sessions[:default][:database]
  host_and_port   = Mongoid::Config.sessions[:default][:hosts][0].split(':')

  host            = host_and_port[0]
  port            = host_and_port[1]

  username        = Mongoid::Config.sessions[:default][:username]
  password        = Mongoid::Config.sessions[:default][:password]

  con = Mongo::MongoClient.new(host, port)
  
  if username
    con.add_auth(database_name, username, password)
  end
  
  db = con.db(database_name)
  
  begin
    gridfs_file = Mongo::GridFileSystem.new(db).open(gridfs_path, 'r')
    self.response_body = gridfs_file.read
    self.content_type = gridfs_file.content_type
  rescue
    self.status = :file_not_found
    self.content_type = 'text/plain'
    self.response_body = ''
  end
end