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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
In answer to your first question, probably because I have three small kids and don't get enough sleep :)
You're right about yield, I tend to think of the block as a method argument so prefer to see it visually in the method signature. I'll change it ASAP.
These are dumb mistakes. So is your tone. I'm letting it slide.
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.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Your module Kimono::GridIO has got to be the most ridiculous implementation of a Ruby module I have ever seen. Surely you must know that modules, like classes, are supposed to hold instance methods, but unlike classes, they can be included into a class (or another module), adding those methods to the receiver. So why would you define an "included" hook that explicitely re-opens the class to define new methods instead of just defining methods directly in the module's body?
Also, still in Kimono::GridIO, but this time in #each: you should use "yield" instead of capturing the block in a "block" variable and then calling "block.call". The result will be the same, but it's just the right way to do it, less code noise and way more efficient.