Skip to content

Instantly share code, notes, and snippets.

@matsimitsu
Forked from bitzesty/enviroment.rb
Created May 15, 2010 13:50
Show Gist options
  • Save matsimitsu/402202 to your computer and use it in GitHub Desktop.
Save matsimitsu/402202 to your computer and use it in GitHub Desktop.
# rails metal to be used with carrierwave (gridfs) and MongoMapper
# Now works with MongoMapper 0.7.5 and Mongo 1.0
# Begin/rescue is a bit lame, but mongo 1.0 doesn't have the exist? method yet (only from 1.0.1)
require 'mongo'
# Allow the metal piece to run in isolation
require(File.dirname(__FILE__) + "/../../config/environment") unless defined?(Rails)
class Gridfs
def self.call(env)
if env["PATH_INFO"] =~ /^\/images\/gridfs\/(.+)$/
key = $1
grid = Mongo::GridFileSystem.new(MongoMapper.database)
begin
grid.open(key, 'r') do |file|
[200, {'Content-Type' => file.content_type, "Expires" => Time.now.advance(:days => 1).httpdate, 'Cache-Control' => 'max-age=31536000' }, [file.read]]
end
rescue
[404, {'Content-Type' => 'text/plain'}, ['File not found.']]
end
else
[404, {'Content-Type' => 'text/plain'}, ['File not found.']]
end
end
end
@matthewford
Copy link

nice one, thanks for updating the old gist

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment