Skip to content

Instantly share code, notes, and snippets.

@bitzesty
Created December 26, 2009 23:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save bitzesty/264077 to your computer and use it in GitHub Desktop.
Save bitzesty/264077 to your computer and use it in GitHub Desktop.
rails metal to be used with carrierwave (gridfs) and MongoMapper
config.metals = ["Gridfs"]
# rails metal to be used with carrierwave (gridfs) and MongoMapper
require 'mongo'
require 'mongo/gridfs'
# 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
if ::GridFS::GridStore.exist?(MongoMapper.database, key)
::GridFS::GridStore.open(MongoMapper.database, key, 'r') do |file|
[200, {'Content-Type' => file.content_type}, [file.read]]
end
else
[404, {'Content-Type' => 'text/plain'}, ['File not found.']]
end
else
[404, {'Content-Type' => 'text/plain'}, ['File not found.']]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment