Skip to content

Instantly share code, notes, and snippets.

@jimsynz
Created November 25, 2010 20:48
Show Gist options
  • Save jimsynz/715896 to your computer and use it in GitHub Desktop.
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.
# 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
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
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
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
@jimsynz
Copy link
Author

jimsynz commented Dec 29, 2010

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.

@Roman2K
Copy link

Roman2K commented Dec 29, 2010

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