Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit e8f723a76f6c5429aa6d0f66b868a371f45dbdb3 Corin Langosch committed May 24, 2011
@@ -0,0 +1,6 @@
+pkg/*
+*.gem
+.bundle
+spec/public
+.rvmrc
+Gemfile.lock
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in carrierwave-mongoid.gemspec
+gemspec
@@ -0,0 +1,53 @@
+= CarrierWave for Mongoid
+
+This gem adds support for Mongoid and MongoDB's GridFS to CarrierWave, see the
+CarrierWave documentation for more detailed usage instructions.
+
+Install it like this:
+
+ gem install carrierwave-mongoid
+
+Use it like this:
+
+ require 'carrierwave/mongoid'
+
+Make sure to disable auto_validation on the mounted column.
+
+Using bundler:
+
+ gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid'
+
+This used to be part of CarrierWave but has been extracted.
+
+== Using MongoDB's GridFS store
+
+You'll need to configure the database and host to use:
+
+ CarrierWave.configure do |config|
+ config.grid_fs_database = 'my_mongo_database'
+ config.grid_fs_host = 'mongo.example.com'
+ end
+
+The defaults are 'carrierwave' and 'localhost'.
+
+And then in your uploader, set the storage to <code>:grid_fs</code>:
+
+ class AvatarUploader < CarrierWave::Uploader::Base
+ storage :grid_fs
+ end
+
+Since GridFS doesn't make the files available via HTTP, you'll need to stream
+them yourself. In Rails for example, you could use the +send_data+ method. You
+can tell CarrierWave the URL you will serve your images from, allowing it to
+generate the correct URL, by setting eg:
+
+ CarrierWave.configure do |config|
+ config.grid_fs_access_url = "/image/show"
+ end
+
+== Known issues/ limitations
+
+If using Mongoid, note that embedded documents files aren't saved when parent documents are saved.
+You must explicitly call save on embedded documents in order to save their attached files.
+You can read more about this {here}[https://github.com/jnicklas/carrierwave/issues#issue/81]
+
@@ -0,0 +1,12 @@
+require 'bundler'
+Bundler::GemHelper.install_tasks
+
+require 'rspec/core/rake_task'
+
+desc "Run all examples"
+RSpec::Core::RakeTask.new(:spec) do |t|
+ #t.rspec_path = 'bin/rspec'
+ t.rspec_opts = %w[--color]
+end
+
+task :default => :spec
@@ -0,0 +1,26 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "carrierwave/mongoid/version"
+
+Gem::Specification.new do |s|
+ s.name = "carrierwave-mongoid"
+ s.version = Carrierwave::Mongoid::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["Jonas Nicklas", "Trevor Turk"]
+ s.email = ["jonas.nicklas@gmail.com"]
+ s.homepage = "https://github.com/jnicklas/carrierwave-mongoid"
+ s.summary = %q{Mongoid support for CarrierWave}
+ s.description = %q{Mongoid support for CarrierWave}
+
+ s.rubyforge_project = "carrierwave-mongoid"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+
+ s.add_dependency "carrierwave"
+ s.add_dependency "mongoid"
+ s.add_development_dependency "rspec", ["~> 2.0"]
+ s.add_development_dependency "sqlite3"
+end
@@ -0,0 +1,33 @@
+# encoding: utf-8
+
+require 'mongoid'
+require 'carrierwave/validations/active_model'
+
+module CarrierWave
+ module Mongoid
+ include CarrierWave::Mount
+ ##
+ # See +CarrierWave::Mount#mount_uploader+ for documentation
+ #
+ def mount_uploader(column, uploader=nil, options={}, &block)
+ options[:mount_on] ||= "#{column}_filename"
+ field options[:mount_on]
+
+ super
+
+ alias_method :read_uploader, :read_attribute
+ alias_method :write_uploader, :write_attribute
+
+ include CarrierWave::Validations::ActiveModel
+
+ validates_integrity_of column if uploader_option(column.to_sym, :validate_integrity)
+ validates_processing_of column if uploader_option(column.to_sym, :validate_processing)
+
+ after_save "store_#{column}!".to_sym
+ before_save "write_#{column}_identifier".to_sym
+ after_destroy "remove_#{column}!".to_sym
+ end
+ end # Mongoid
+end # CarrierWave
+
+Mongoid::Document::ClassMethods.send(:include, CarrierWave::Mongoid)
@@ -0,0 +1,5 @@
+module Carrierwave
+ module Mongoid
+ VERSION = "0.1.0"
+ end
+end
@@ -0,0 +1,136 @@
+# encoding: utf-8
+require 'mongo'
+
+module CarrierWave
+ module Storage
+
+ ##
+ # The GridFS store uses MongoDB's GridStore file storage system to store files
+ #
+ # There are two ways of configuring the GridFS connection. Either you create a
+ # connection or you reuse an existing connection.
+ #
+ # Creating a connection looks something like this:
+ #
+ # CarrierWave.configure do |config|
+ # config.storage = :grid_fs
+ # config.grid_fs_host = "your-host.com"
+ # config.grid_fs_port = "27017"
+ # config.grid_fs_database = "your_dbs_app_name"
+ # config.grid_fs_username = "user"
+ # config.grid_fs_password = "verysecret"
+ # config.grid_fs_access_url = "/images"
+ # end
+ #
+ # In the above example your documents url will look like:
+ #
+ # http://your-app.com/images/:document-identifier-here
+ #
+ # When you already have a Mongo connection object (for example through Mongoid)
+ # you can also reuse this connection:
+ #
+ # CarrierWave.configure do |config|
+ # config.storage = :grid_fs
+ # config.grid_fs_connection = Mongoid.database
+ # config.grid_fs_access_url = "/images"
+ # end
+ #
+ class GridFS < Abstract
+
+ class File
+
+ def initialize(uploader, path)
+ @path = path
+ @uploader = uploader
+ end
+
+ def path
+ @path
+ end
+
+ def url
+ unless @uploader.grid_fs_access_url
+ nil
+ else
+ [@uploader.grid_fs_access_url, @path].join("/")
+ end
+ end
+
+ def read
+ grid.open(@path, 'r').data
+ end
+
+ def write(file)
+ grid.open(@uploader.store_path, 'w', :content_type => file.content_type) do |f|
+ f.write(file.read)
+ end
+ end
+
+ def delete
+ grid.delete(@path)
+ end
+
+ def content_type
+ grid.open(@path, 'r').content_type
+ end
+
+ def file_length
+ grid.open(@path, 'r').file_length
+ end
+
+ protected
+
+ def database
+ @connection ||= @uploader.grid_fs_connection || begin
+ host = @uploader.grid_fs_host
+ port = @uploader.grid_fs_port
+ database = @uploader.grid_fs_database
+ username = @uploader.grid_fs_username
+ password = @uploader.grid_fs_password
+ db = Mongo::Connection.new(host, port).db(database)
+ db.authenticate(username, password) if username && password
+ db
+ end
+ end
+
+ def grid
+ @grid ||= Mongo::GridFileSystem.new(database)
+ end
+
+ end
+
+ ##
+ # Store the file in MongoDB's GridFS GridStore
+ #
+ # === Parameters
+ #
+ # [file (CarrierWave::SanitizedFile)] the file to store
+ #
+ # === Returns
+ #
+ # [CarrierWave::SanitizedFile] a sanitized file
+ #
+ def store!(file)
+ stored = CarrierWave::Storage::GridFS::File.new(uploader, uploader.store_path)
+ stored.write(file)
+ stored
+ end
+
+ ##
+ # Retrieve the file from MongoDB's GridFS GridStore
+ #
+ # === Parameters
+ #
+ # [identifier (String)] the filename of the file
+ #
+ # === Returns
+ #
+ # [CarrierWave::Storage::GridFS::File] a sanitized file
+ #
+ def retrieve!(identifier)
+ CarrierWave::Storage::GridFS::File.new(uploader, uploader.store_path(identifier))
+ end
+
+ end # File
+ end # Storage
+end # CarrierWave
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.

0 comments on commit e8f723a

Please sign in to comment.