Module: Paperclip::Storage::Gridfs
- Defined in:
- lib/paperclip/storage/gridfs.rb
Overview
MongoDB’s GridFS storage system (www.mongodb.org/display/DOCS/GridFS) uses a chunking strategy to store files in a mongodb database. Specific options for GridFS:
-
gridfs_credentials
: Similar tos3_credentials
, this can be a path, a File, or a Hash. Keys are as follows:-
database
: the name of the MongoDB database to connect to. This can also be aMongo::DB
object, in which case that connection will be used, and other credentials will be ignored. -
host
: defaults tolocalhost
-
username
andpassword
: optional authentication for the database server.
-
Note that, because files stored using the :gridfs
storage module are stored within the database rather than the file system, you’ll need to work out a method to extract the file data to serve it over HTTP. This is pretty trivial using Rails Metal.
Class Method Summary collapse
Instance Method Summary collapse
- #after_flush_writes ⇒ Object
- #exists?(style = default_style) ⇒ Boolean
-
#flush_deletes ⇒ Object
:nodoc:.
-
#flush_writes ⇒ Object
:nodoc:.
-
#to_file(style = default_style) ⇒ Object
Returns a binary representation of the data of the file assigned to the given style.
Class Method Details
.extended(base) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/paperclip/storage/gridfs.rb', line 21 def self.extended base begin require 'mongo' rescue LoadError => e e. << " (You may need to install the mongo gem)" raise e end base.instance_eval do @gridfs = ::TinySupport::GridFSClient.new(@options[:gridfs]).gridfs end end |
Instance Method Details
#after_flush_writes ⇒ Object
67 68 69 70 71 72 |
# File 'lib/paperclip/storage/gridfs.rb', line 67 def after_flush_writes @queued_for_write.each do |style, file| file.close unless file.closed? file.unlink if file.respond_to?(:unlink) && file.path.present? && File.exist?(file.path) end end |
#exists?(style = default_style) ⇒ Boolean
34 35 36 37 38 39 40 |
# File 'lib/paperclip/storage/gridfs.rb', line 34 def exists? style = default_style if original_filename !!@gridfs.exist?(:filename => _url_of_file_saved(style)) else false end end |
#flush_deletes ⇒ Object
:nodoc:
59 60 61 62 63 64 65 |
# File 'lib/paperclip/storage/gridfs.rb', line 59 def flush_deletes #:nodoc: @queued_for_delete.each do |url| log("deleting #{url}") @gridfs.delete(url) end @queued_for_delete = [] end |
#flush_writes ⇒ Object
:nodoc:
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/paperclip/storage/gridfs.rb', line 47 def flush_writes #:nodoc: @queued_for_write.each do |style, file| _url = _url_of_file_saved(style) log("saving #{_url}") @gridfs.open(_url, 'w', :content_type => content_type) do |f| f.write file.read end end after_flush_writes # allows attachment to clean up temp files @queued_for_write = {} end |
#to_file(style = default_style) ⇒ Object
Returns a binary representation of the data of the file assigned to the given style
43 44 45 |
# File 'lib/paperclip/storage/gridfs.rb', line 43 def to_file style = default_style @queued_for_write[style] || (@gridfs.open(_url_of_file_saved(style), 'r') if exists?(style)) end |