Skip to content

Instantly share code, notes, and snippets.

@zires
Created August 16, 2011 02:25
Show Gist options
  • Save zires/1148316 to your computer and use it in GitHub Desktop.
Save zires/1148316 to your computer and use it in GitHub Desktop.
read grid files
require 'mongo'
class GridfsController < ActionController::Metal
def serve
env["PATH_INFO"].match(/\/attach\/([\da-zA-Z]+)\//)
begin
grid = Mongo::Grid.new(Mongoid.database).get(BSON::ObjectId($1))
self.headers["Content-Length"] = grid.file_length.to_s
self.content_type = grid.content_type
self.response_body = grid.read
rescue
self.status = :file_not_found
self.content_type = 'text/plain'
self.response_body = ''
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment