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 to s3_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 a Mongo::DB object, in which case that connection will be used, and other credentials will be ignored.

    • host: defaults to localhost

    • username and password: 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

Class Method Details

.extended(base) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/paperclip/storage/gridfs.rb', line 20

def self.extended base
  begin
    require 'mongo'
  rescue LoadError => e
    e.message << " (You may need to install the mongo gem)"
    raise e
  end

  base.instance_eval do
    @gridfs_connection = get_database_connection(parse_credentials(@options[:gridfs]))
    @gridfs            = Mongo::GridFileSystem.new(@gridfs_connection)
  end
end

Instance Method Details

#exists?(style = default_style) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
45
46
# File 'lib/paperclip/storage/gridfs.rb', line 40

def exists? style = default_style
  if original_filename
    !!@gridfs.exist?(:filename => path(style))
  else
    false
  end
end

#flush_deletesObject

:nodoc:



64
65
66
67
68
69
70
# File 'lib/paperclip/storage/gridfs.rb', line 64

def flush_deletes #:nodoc:
  @queued_for_delete.each do |path|
    log("deleting #{path}")
    @gridfs.delete(path)
  end
  @queued_for_delete = []
end

#flush_writesObject

:nodoc:



53
54
55
56
57
58
59
60
61
62
# File 'lib/paperclip/storage/gridfs.rb', line 53

def flush_writes #:nodoc:
  @queued_for_write.each do |style, file|
    log("saving #{path(style)}")
    @gridfs.open(path(style), '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

#parse_credentials(creds) ⇒ Object



34
35
36
37
38
# File 'lib/paperclip/storage/gridfs.rb', line 34

def parse_credentials creds
  creds = find_credentials(creds).stringify_keys
  env = Object.const_defined?(:Rails) ? Rails.env : nil
  (creds[env] || creds).symbolize_keys
end

#to_file(style = default_style) ⇒ Object

Returns a binary representation of the data of the file assigned to the given style



49
50
51
# File 'lib/paperclip/storage/gridfs.rb', line 49

def to_file style = default_style
  @queued_for_write[style] || (@gridfs.open(path(style), 'r') if exists?(style))
end