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) ⇒ Object

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

Parameters

new_file (File, IOString, Tempfile)

any kind of file object

Raises

CarrierWave::FormNotMultipart

if the assigned parameter is a string



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/carrierwave/uploader/cache.rb', line 101

def cache!(new_file)
  new_file = CarrierWave::SanitizedFile.new(new_file)

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

    with_callbacks(:cache, new_file) do
      self.cache_id = CarrierWave.generate_cache_id unless cache_id

      @filename = new_file.filename
      self.original_filename = new_file.filename

      @file = new_file.copy_to(cache_path, permissions)
    end
  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 YYYYMMDD-HHMM-PID-RND/filename.txt



86
87
88
# File 'lib/carrierwave/uploader/cache.rb', line 86

def cache_name
  File.join(cache_id, full_original_filename) if cache_id and original_filename
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.



72
73
74
75
76
77
# File 'lib/carrierwave/uploader/cache.rb', line 72

def cache_stored_file!
  sanitized = SanitizedFile.new :tempfile => StringIO.new(file.read),
    :filename => File.basename(path), :content_type => file.content_type

  cache! sanitized
end

#cached?Boolean

Returns true if the uploader has been cached

Returns

Bool

whether the current file is cached

Returns:

  • (Boolean)


62
63
64
# File 'lib/carrierwave/uploader/cache.rb', line 62

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.



129
130
131
132
133
134
135
# File 'lib/carrierwave/uploader/cache.rb', line 129

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)
    @filename = original_filename
    @file = CarrierWave::SanitizedFile.new(cache_path)
  end
end