Skip to content

Commit

Permalink
added file path accessing.
Browse files Browse the repository at this point in the history
  • Loading branch information
hsbt authored and ches committed Jan 6, 2011
1 parent 565d566 commit 85d48af
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions lib/rack/gridfs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand All @@ -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.']]
Expand Down

0 comments on commit 85d48af

Please sign in to comment.