Created
November 25, 2010 20:48
-
-
Save jimsynz/715896 to your computer and use it in GitHub Desktop.
Patch for Mongo::GridIO to respond to each as required by the Rack API.
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
# Patch Mongo::GridIO to contain an each method. | |
require File.join File.dirname(__FILE__), 'lib/kimono/grid_io' | |
Mongo::GridIO.send(:include, Kimono::GridIO) | |
# You probably want to implement something a bit smarter than just talking | |
# to localhost. | |
database = Mongo::Connection.new.db('kimono_production') | |
app = proc do |env| | |
begin | |
gridfs_file = Mongo::GridFileSystem.new(Mongoid.database).open(env["PATH_INFO"], 'r') | |
[ 200, { 'Content-Type' => gridfs_file.content_type, 'Content-Length' => gridfs_file.file_length.to_s }, gridfs_file ] | |
rescue | |
message = 'Grid file not found.' | |
[ 404, { 'Content-Type' => 'text/plain', 'Content-Length' => message.size.to_s }, message ] | |
end | |
end | |
run app |
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' | |
module Kimono | |
module GridIO | |
def size | |
(file_length / chunk_size) + (file_length % chunk_size > 0 ? 1 : 0) | |
end | |
def each | |
size.times { yield read(chunk_size) } | |
end | |
end | |
end |
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 | |
gridfs_path = env["PATH_INFO"].gsub("/images/", "") | |
begin | |
gridfs_file = Mongo::GridFileSystem.new(Mongoid.database).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 | |
end |
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
Kimono::Application.routes.draw do | |
# Patch Mongo::GridIO to contain an each method. | |
require File.join Rails.root, 'lib/kimono/grid_io' | |
Mongo::GridIO.send(:include, Kimono::GridIO) | |
match "/gridfs/*path", :via => :get, :to => proc { |env| | |
gridfs_path = env["PATH_INFO"].gsub("/gridfs/", "") | |
begin | |
gridfs_file = Mongo::GridFileSystem.new(Mongoid.database).open(gridfs_path, 'r') | |
[ 200, { 'Content-Type' => gridfs_file.content_type, 'Content-Length' => gridfs_file.file_length.to_s }, gridfs_file ] | |
rescue | |
message = 'Grid file not found.' | |
[ 404, { 'Content-Type' => 'text/plain', 'Content-Length' => message.size.to_s }, message ] | |
end | |
} | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OK.
I know it sounded harsh, I'm sorry. But these are the kind of mistakes that shouldn't happen anymore given how well Ruby is documented now.