Created
August 16, 2011 02:25
-
-
Save zires/1148316 to your computer and use it in GitHub Desktop.
read grid files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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