Created
December 27, 2010 22:50
-
-
Save arkan/756664 to your computer and use it in GitHub Desktop.
This file contains 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
# 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 |
This file contains 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
# 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 |
This file contains 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
-# 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