Class: CarrierWave::Storage::File

Inherits:
Abstract
  • Object
show all
Defined in:
vendor/carrierwave/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

#cache!(new_file) ⇒ Object

Stores given file to cache directory.

=== Parameters

[new_file (File, IOString, Tempfile)] any kind of file object

=== Returns

[CarrierWave::SanitizedFile] a sanitized file



63
64
65
66
67
68
69
70
71
72
73
# File 'vendor/carrierwave/lib/carrierwave/storage/file.rb', line 63

def cache!(new_file)
  new_file.move_to(::File.expand_path(uploader.cache_path, uploader.root), uploader.permissions, uploader.directory_permissions, true)
rescue Errno::EMLINK => e
  raise(e) if @cache_called
  @cache_called = true

  # NOTE: Remove cached files older than 10 minutes
  clean_cache!(600)

  cache!(new_file)
end

#clean_cache!(seconds) ⇒ Object



107
108
109
110
111
112
113
114
115
# File 'vendor/carrierwave/lib/carrierwave/storage/file.rb', line 107

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(&:to_i)
    time = Time.at(*time)
    if time < (Time.now.utc - seconds)
      FileUtils.rm_rf(dir)
    end
  end
end

#delete_dir!(path) ⇒ Object

Deletes a cache dir



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'vendor/carrierwave/lib/carrierwave/storage/file.rb', line 93

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

#retrieve!(identifier) ⇒ Object

Retrieve the file from its store path

=== Parameters

[identifier (String)] the filename of the file

=== Returns

[CarrierWave::SanitizedFile] a sanitized file



47
48
49
50
# File 'vendor/carrierwave/lib/carrierwave/storage/file.rb', line 47

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

#retrieve_from_cache!(identifier) ⇒ Object

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.



86
87
88
# File 'vendor/carrierwave/lib/carrierwave/storage/file.rb', line 86

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

#store!(file) ⇒ Object

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



27
28
29
30
31
32
33
34
# File 'vendor/carrierwave/lib/carrierwave/storage/file.rb', line 27

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