From 900238b1e19db76f9ca1f3bef4e449a9a413dcf6 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Fri, 22 Feb 2013 16:58:41 -0500 Subject: [PATCH] clean some things up and add a readme --- README.md | 39 ++++++++---------- app/controllers/media_controller.rb | 10 +---- app/models/carrierwave_mongoid_media.rb | 40 +++++++++++++++++++ carrierwave-mongoid-media.gemspec | 3 ++ lib/carrierwave-mongoid-media.rb | 5 ++- lib/carrierwave-mongoid-media/engine.rb | 13 ++++-- lib/carrierwave/mongoid/media.rb | 1 + .../mongoid/media/controller_behavior.rb | 17 ++++++++ 8 files changed, 92 insertions(+), 36 deletions(-) create mode 100644 app/models/carrierwave_mongoid_media.rb create mode 100644 lib/carrierwave/mongoid/media.rb create mode 100644 lib/carrierwave/mongoid/media/controller_behavior.rb diff --git a/README.md b/README.md index 97fdf1a..889ee93 100644 --- a/README.md +++ b/README.md @@ -1,29 +1,24 @@ -# Carrierwave::Mongoid::Media +Make it very easy to accept Carrierwave uploads to a Mongoid GridFS-backed data store and then deliver them. -TODO: Write a gem description +Add this to your `config/routes.rb`: -## Installation +``` ruby +My::Application.routes.draw do + CarrierWave::Mongoid::Media.routes(self) +end +``` -Add this line to your application's Gemfile: +And you'll have a `/media/*path` route that just delivers whatever Carrierwave uploads to this app, as long as +you're using [carrierwave-mongoid](https://github.com/jnicklas/carrierwave-mongoid) to do the uploads. CarrierWave's +`grid_fs_access_url` will be set correctly for you, too. - gem 'carrierwave-mongoid-media' +You can specify if any of the paths should respond with `Content-Disposition: attachment` headers with an initializer: -And then execute: +``` ruby +# config/initializers/carrierwave_mongoid_media.rb - $ bundle +CarrierwaveMongoidMedia.force_downloads_on do |path| + # return true if the file should get Content-Disposition: attachment +end +``` -Or install it yourself as: - - $ gem install carrierwave-mongoid-media - -## Usage - -TODO: Write usage instructions here - -## Contributing - -1. Fork it -2. Create your feature branch (`git checkout -b my-new-feature`) -3. Commit your changes (`git commit -am 'Add some feature'`) -4. Push to the branch (`git push origin my-new-feature`) -5. Create new Pull Request diff --git a/app/controllers/media_controller.rb b/app/controllers/media_controller.rb index 01a4e22..c46bb70 100644 --- a/app/controllers/media_controller.rb +++ b/app/controllers/media_controller.rb @@ -1,12 +1,4 @@ class MediaController < ApplicationController - def show - path = params[:path] + '.' + params[:format] - obj = Mongoid::GridFS[path] - - raise Mongoid::Errors::DocumentNotFound.new(Mongoid::GridFS, :path => path) if !obj - - self.content_type = obj.content_type - self.response_body = obj - end + include CarrierWave::Mongoid::Media::ControllerBehavior end diff --git a/app/models/carrierwave_mongoid_media.rb b/app/models/carrierwave_mongoid_media.rb new file mode 100644 index 0000000..a863c9e --- /dev/null +++ b/app/models/carrierwave_mongoid_media.rb @@ -0,0 +1,40 @@ +require 'delegate' + +class CarrierwaveMongoidMedia < SimpleDelegator + def self.find(path) + obj = Mongoid::GridFS[path] + + raise Mongoid::Errors::DocumentNotFound.new(Mongoid::GridFS, :path => path) if !obj + + new(obj) + end + + def self.force_downloads_on(&block) + if block + @block = block + else + @block + end + end + + def attachment_filename + File.basename(self.filename) + end + + def initialize(gridfs_object) + @gridfs_object = gridfs_object + end + + def __getobj__ + @gridfs_object + end + + def force_download? + if block = self.class.force_downloads_on + block.call(self.filename) + else + false + end + end +end + diff --git a/carrierwave-mongoid-media.gemspec b/carrierwave-mongoid-media.gemspec index 2a4df41..199dbef 100644 --- a/carrierwave-mongoid-media.gemspec +++ b/carrierwave-mongoid-media.gemspec @@ -16,4 +16,7 @@ Gem::Specification.new do |gem| gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) gem.require_paths = ["lib"] + + gem.add_dependency 'carrierwave' + gem.add_dependency 'carrierwave-mongoid' end diff --git a/lib/carrierwave-mongoid-media.rb b/lib/carrierwave-mongoid-media.rb index 6fd7d9c..664c4d0 100644 --- a/lib/carrierwave-mongoid-media.rb +++ b/lib/carrierwave-mongoid-media.rb @@ -1,3 +1,6 @@ require "carrierwave-mongoid-media/version" -require 'carrierwave-mongoid-media/engine' if defined?(Rails::Engine) +if defined?(Rails::Engine) + require 'carrierwave-mongoid-media/engine' + require 'carrierwave/mongoid/media/controller_behavior' +end diff --git a/lib/carrierwave-mongoid-media/engine.rb b/lib/carrierwave-mongoid-media/engine.rb index 9456067..17df0fe 100644 --- a/lib/carrierwave-mongoid-media/engine.rb +++ b/lib/carrierwave-mongoid-media/engine.rb @@ -13,15 +13,20 @@ module CarrierWave @prefix ||= DEFAULT_PREFIX end - def self.routes(router, prefix = :media) - self.prefix = prefix + def self.routes(router, options = {}) + options = { + :prefix => :media, + :controller => :media + }.merge(options) + + self.prefix = options[:prefix] CarrierWave.configure do |c| - c.grid_fs_access_url = "/#{prefix}" + c.grid_fs_access_url = "/#{options[:prefix]}" end router.instance_exec do - get "#{prefix}/*path" => "media#show", :as => :media + get "#{options[:prefix]}/*path" => "#{options[:controller]}#show", :as => :media end end end diff --git a/lib/carrierwave/mongoid/media.rb b/lib/carrierwave/mongoid/media.rb new file mode 100644 index 0000000..c9ed20f --- /dev/null +++ b/lib/carrierwave/mongoid/media.rb @@ -0,0 +1 @@ +require 'carrierwave-mongoid-media' diff --git a/lib/carrierwave/mongoid/media/controller_behavior.rb b/lib/carrierwave/mongoid/media/controller_behavior.rb new file mode 100644 index 0000000..50597c9 --- /dev/null +++ b/lib/carrierwave/mongoid/media/controller_behavior.rb @@ -0,0 +1,17 @@ +module CarrierWave::Mongoid::Media::ControllerBehavior + def show + obj = CarrierwaveMongoidMedia.find(path) + + self.content_type = obj.content_type + self.response_body = obj + if obj.force_download? + response.headers['Content-Disposition'] = "attachment; filename='#{obj.attachment_filename}'" + end + end + + private + def path + @path ||= params[:path] + '.' + params[:format] + end +end +