Permalink
Browse files

added file path accessing.

  • Loading branch information...
1 parent 565d566 commit 85d48af5adf152179c4670f5252a7889071103aa @hsbt hsbt committed with ches Apr 27, 2010
Showing with 9 additions and 3 deletions.
  1. +9 −3 lib/rack/gridfs.rb
View
12 lib/rack/gridfs.rb
@@ -14,11 +14,13 @@ def initialize(app, options = {})
options = {
:hostname => 'localhost',
:prefix => 'gridfs',
- :port => Mongo::Connection::DEFAULT_PORT
+ :port => Mongo::Connection::DEFAULT_PORT,
+ :accessor => 'id'
}.merge(options)
@app = app
@prefix = options[:prefix]
+ @accessor = options[:accessor]
@db = nil
@hostname, @port, @database, @username, @password =
@@ -36,8 +38,12 @@ def call(env)
end
end
- def gridfs_request(id)
- file = Mongo::Grid.new(db).get(BSON::ObjectId.from_string(id))
+ def gridfs_request(path)
+ if @accessor == 'id'
+ file = Mongo::Grid.new(db).get(BSON::ObjectId.from_string(path))
+ elsif @accessor == 'path'
+ file = Mongo::GridFileSystem.new(db).open(path, "r")
+ end
[200, {'Content-Type' => file.content_type}, file]
rescue Mongo::GridFileNotFound, BSON::InvalidObjectId
[404, {'Content-Type' => 'text/plain'}, ['File not found.']]

0 comments on commit 85d48af

Please sign in to comment.