Class: CarrierWave::Storage::GridFS

Inherits:
Abstract
  • Object
show all
Defined in:
lib/carrierwave/storage/grid_fs.rb

Overview

The GridFS store uses MongoDB’s GridStore file storage system to store files

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_access_url = "/system/uploads"
  end

In the above example your documents url will look like:

   http://your-app.com/system/uploads/:document-identifier-here

Defined Under Namespace

Classes: File

Instance Method Summary collapse

Instance Method Details

#cache!(file) ⇒ Object

Cache the file in MongoDB’s GridFS GridStore

Parameters

file (CarrierWave::SanitizedFile)

the file to store

Returns

CarrierWave::SanitizedFile

a sanitized file



123
124
125
126
127
# File 'lib/carrierwave/storage/grid_fs.rb', line 123

def cache!(file)
  stored = CarrierWave::Storage::GridFS::File.new(uploader, uploader.cache_path)
  stored.write(file)
  stored
end

#clean_cache!(seconds) ⇒ Object

Clean old caches

Parameters

seconds (Integer)

duration in seconds, caches older than this will be deleted



155
156
157
158
159
160
# File 'lib/carrierwave/storage/grid_fs.rb', line 155

def clean_cache!(seconds)
  File.grid.namespace.
    where(filename: /\d+-\d+-\d+(?:-\d+)?\/.+/).
    and(:filename.lt => (Time.now.utc - seconds).to_i.to_s).
    delete
end

#delete_dir!(path) ⇒ Object



144
145
146
# File 'lib/carrierwave/storage/grid_fs.rb', line 144

def delete_dir!(path)
  # do nothing, because there's no such things as 'empty directory'
end

#retrieve!(identifier) ⇒ Object

Retrieve the file from MongoDB’s GridFS GridStore

Parameters

identifier (String)

the filename of the file

Returns

CarrierWave::Storage::GridFS::File

a sanitized file



108
109
110
# File 'lib/carrierwave/storage/grid_fs.rb', line 108

def retrieve!(identifier)
  CarrierWave::Storage::GridFS::File.new(uploader, uploader.store_path(identifier))
end

#retrieve_from_cache!(identifier) ⇒ Object

Retrieve the cached file from MongoDB’s GridFS GridStore

Parameters

identifier (String)

uniquely identifies a cache file

Returns

CarrierWave::Storage::GridFS::File

a sanitized file



140
141
142
# File 'lib/carrierwave/storage/grid_fs.rb', line 140

def retrieve_from_cache!(identifier)
  CarrierWave::Storage::GridFS::File.new(uploader, uploader.cache_path(identifier))
end

#store!(file) ⇒ Object

Store the file in MongoDB’s GridFS GridStore

Parameters

file (CarrierWave::SanitizedFile)

the file to store

Returns

CarrierWave::SanitizedFile

a sanitized file



91
92
93
94
95
# File 'lib/carrierwave/storage/grid_fs.rb', line 91

def store!(file)
  stored = CarrierWave::Storage::GridFS::File.new(uploader, uploader.store_path)
  stored.write(file)
  stored
end