Class: CarrierWave::Storage::File

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

Overview

File storage stores file to the Filesystem (surprising, no?). There's really not much to it, it uses the store_dir defined on the uploader as the storage location. That's pretty much it.

Instance Attribute Summary

Attributes inherited from Abstract

#uploader

Instance Method Summary (collapse)

Methods inherited from Abstract

#identifier, #initialize

Constructor Details

This class inherits a constructor from CarrierWave::Storage::Abstract

Instance Method Details

- (Object) cache!(new_file)

Stores given file to cache directory.

Parameters

new_file (File, IOString, Tempfile)

any kind of file object

Returns

CarrierWave::SanitizedFile

a sanitized file



65
66
67
# File 'lib/carrierwave/storage/file.rb', line 65

def cache!(new_file)
  new_file.move_to(::File.expand_path(uploader.cache_path, uploader.root), uploader.permissions, uploader.directory_permissions, true)
end

- (Object) clean_cache!(seconds)



101
102
103
104
105
106
107
108
109
# File 'lib/carrierwave/storage/file.rb', line 101

def clean_cache!(seconds)
  Dir.glob(::File.expand_path(::File.join(uploader.cache_dir, '*'), CarrierWave.root)).each do |dir|
    time = dir.scan(/(\d+)-\d+-\d+/).first.map { |t| t.to_i }
    time = Time.at(*time)
    if time < (Time.now.utc - seconds)
      FileUtils.rm_rf(dir)
    end
  end
end

- (Object) delete_dir!(path)

Deletes a cache dir



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/carrierwave/storage/file.rb', line 87

def delete_dir!(path)
  if path
    begin
      Dir.rmdir(::File.expand_path(path, uploader.root))
    rescue Errno::ENOENT
      # Ignore: path does not exist
    rescue Errno::ENOTDIR
      # Ignore: path is not a dir
    rescue Errno::ENOTEMPTY, Errno::EEXIST
      # Ignore: dir is not empty
    end
  end
end

- (Object) retrieve!(identifier)

Retrieve the file from its store path

Parameters

identifier (String)

the filename of the file

Returns

CarrierWave::SanitizedFile

a sanitized file



49
50
51
52
# File 'lib/carrierwave/storage/file.rb', line 49

def retrieve!(identifier)
  path = ::File.expand_path(uploader.store_path(identifier), uploader.root)
  CarrierWave::SanitizedFile.new(path)
end

- (Object) retrieve_from_cache!(identifier)

Retrieves the file with the given cache_name from the cache.

Parameters

cache_name (String)

uniquely identifies a cache file

Raises

CarrierWave::InvalidParameter

if the cache_name is incorrectly formatted.



80
81
82
# File 'lib/carrierwave/storage/file.rb', line 80

def retrieve_from_cache!(identifier)
  CarrierWave::SanitizedFile.new(::File.expand_path(uploader.cache_path(identifier), uploader.root))
end

- (Object) store!(file)

Move the file to the uploader's store path.

By default, store!() uses copy_to(), which operates by copying the file from the cache to the store, then deleting the file from the cache. If move_to_store() is overriden to return true, then store!() uses move_to(), which simply moves the file from cache to store. Useful for large files.

Parameters

file (CarrierWave::SanitizedFile)

the file to store

Returns

CarrierWave::SanitizedFile

a sanitized file



29
30
31
32
33
34
35
36
# File 'lib/carrierwave/storage/file.rb', line 29

def store!(file)
  path = ::File.expand_path(uploader.store_path, uploader.root)
  if uploader.move_to_store
    file.move_to(path, uploader.permissions, uploader.directory_permissions)
  else
    file.copy_to(path, uploader.permissions, uploader.directory_permissions)
  end
end