Module: CarrierWave::Uploader::Cache

Extended by:
ActiveSupport::Concern
Includes:
Callbacks, Configuration
Included in:
Base, Download, Store
Defined in:
lib/carrierwave/uploader/cache.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Methods included from Callbacks

#with_callbacks

Instance Method Details

#cache!(new_file = file) ⇒ Object

Caches the given file. Calls process! to trigger any process callbacks.

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

Parameters

new_file (File, IOString, Tempfile)

any kind of file object

Raises

CarrierWave::FormNotMultipart

if the assigned parameter is a string


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/carrierwave/uploader/cache.rb', line 124

def cache!(new_file = file)
  new_file = CarrierWave::SanitizedFile.new(new_file)
  return if new_file.empty?

  raise CarrierWave::FormNotMultipart if new_file.is_path? && ensure_multipart_form

  self.cache_id = CarrierWave.generate_cache_id unless cache_id

  @staged = true
  @filename = new_file.filename
  self.original_filename = new_file.filename

  begin
    # first, create a workfile on which we perform processings
    if move_to_cache
      @file = new_file.move_to(File.expand_path(workfile_path, root), permissions, directory_permissions)
    else
      @file = new_file.copy_to(File.expand_path(workfile_path, root), permissions, directory_permissions)
    end

    with_callbacks(:cache, @file) do
      @file = cache_storage.cache!(@file)
    end
  ensure
    FileUtils.rm_rf(workfile_path(''))
  end
end

#cache_nameObject

Returns a String which uniquely identifies the currently cached file for later retrieval

Returns

String

a cache name, in the format TIMEINT-PID-COUNTER-RND/filename.txt


104
105
106
# File 'lib/carrierwave/uploader/cache.rb', line 104

def cache_name
  File.join(cache_id, full_original_filename) if cache_id && original_filename
end

#cache_path(for_file = full_filename(original_filename)) ⇒ Object

Calculates the path where the cache file should be stored.

Parameters

for_file (String)

name of the file <optional>

Returns

String

the cache path


183
184
185
# File 'lib/carrierwave/uploader/cache.rb', line 183

def cache_path(for_file=full_filename(original_filename))
  File.join(*[cache_dir, @cache_id, for_file].compact)
end

#cache_stored_file!Object

Caches the remotely stored file

This is useful when about to process images. Most processing solutions require the file to be stored on the local filesystem.


88
89
90
# File 'lib/carrierwave/uploader/cache.rb', line 88

def cache_stored_file!
  cache!
end

#cached?Boolean

Returns true if the uploader has been cached

Returns

Bool

whether the current file is cached


78
79
80
# File 'lib/carrierwave/uploader/cache.rb', line 78

def cached?
  @cache_id
end

#retrieve_from_cache!(cache_name) ⇒ 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.


163
164
165
166
167
168
169
170
# File 'lib/carrierwave/uploader/cache.rb', line 163

def retrieve_from_cache!(cache_name)
  with_callbacks(:retrieve_from_cache, cache_name) do
    self.cache_id, self.original_filename = cache_name.to_s.split('/', 2)
    @staged = true
    @filename = original_filename
    @file = cache_storage.retrieve_from_cache!(full_filename(original_filename))
  end
end

#sanitized_fileObject


92
93
94
95
# File 'lib/carrierwave/uploader/cache.rb', line 92

def sanitized_file
  ActiveSupport::Deprecation.warn('#sanitized_file is deprecated, use #file instead.')
  file
end