clean some things up and add a readme
This commit is contained in:
parent
ba98341916
commit
900238b1e1
39
README.md
39
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
|
||||
|
@ -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
|
||||
|
||||
|
40
app/models/carrierwave_mongoid_media.rb
Normal file
40
app/models/carrierwave_mongoid_media.rb
Normal file
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
1
lib/carrierwave/mongoid/media.rb
Normal file
1
lib/carrierwave/mongoid/media.rb
Normal file
@ -0,0 +1 @@
|
||||
require 'carrierwave-mongoid-media'
|
17
lib/carrierwave/mongoid/media/controller_behavior.rb
Normal file
17
lib/carrierwave/mongoid/media/controller_behavior.rb
Normal file
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user