Skip to content

Instantly share code, notes, and snippets.

@shanlalit
Created May 28, 2010 19:48
Show Gist options
  • Save shanlalit/417635 to your computer and use it in GitHub Desktop.
Save shanlalit/417635 to your computer and use it in GitHub Desktop.
# config/initializers/carrierwave.rb
CarrierWave.configure do |config|
config.grid_fs_database = "database_name"
config.grid_fs_host = 'localhost'
config.grid_fs_access_url = "/uploads"
config.storage = :grid_fs
end
# app/metal/gridfs_serve_image.rb
require 'mongo'
require(File.dirname(__FILE__) + "/../../config/environment") unless defined?(Rails)
class GridfsServeImage
def self.call(env)
if env["PATH_INFO"] =~ /^\/uploads\/(.+)$/
begin
Mongo::GridFileSystem.new(Mongoid.database).open($1, 'r') do |file|
[200, { 'Content-Type' => file.content_type }, [file.read]]
end
rescue
[404, { 'Content-Type' => 'text/plain' }, ['File not found.']]
end
else
[404, { "Content-Type" => "text/html", "X-Cascade" => "pass" }, ["Not Found"]]
end
end
end
-# app/views/something/show.html.haml
= image_tag @something.photo.url
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment