Module: CarrierWave::Uploader::Cache
- Extended by:
- ActiveSupport::Concern
- Includes:
- Callbacks, Configuration
- Defined in:
- lib/carrierwave/uploader/cache.rb
Defined Under Namespace
Modules: ClassMethods
Instance Method Summary collapse
-
#cache!(new_file = sanitized_file) ⇒ Object
Caches the given file.
-
#cache_name ⇒ Object
Returns a String which uniquely identifies the currently cached file for later retrieval.
-
#cache_stored_file! ⇒ Object
Caches the remotely stored file.
-
#cached? ⇒ Boolean
Returns true if the uploader has been cached.
-
#retrieve_from_cache!(cache_name) ⇒ Object
Retrieves the file with the given cache_name from the cache.
- #sanitized_file ⇒ Object
Methods included from Callbacks
Instance Method Details
#cache!(new_file = sanitized_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
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/carrierwave/uploader/cache.rb', line 128 def cache!(new_file = sanitized_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 if move_to_cache @file = new_file.move_to(cache_path, , ) else @file = new_file.copy_to(cache_path, , ) end end end end |
#cache_name ⇒ Object
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
108 109 110 |
# File 'lib/carrierwave/uploader/cache.rb', line 108 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.
84 85 86 |
# File 'lib/carrierwave/uploader/cache.rb', line 84 def cache_stored_file! cache! end |
#cached? ⇒ Boolean
Returns true if the uploader has been cached
Returns
- Bool
-
whether the current file is cached
74 75 76 |
# File 'lib/carrierwave/uploader/cache.rb', line 74 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.
160 161 162 163 164 165 166 |
# File 'lib/carrierwave/uploader/cache.rb', line 160 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 |
#sanitized_file ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/carrierwave/uploader/cache.rb', line 88 def sanitized_file _content = file.read if _content.is_a?(File) # could be if storage is Fog sanitized = CarrierWave::Storage::Fog.new(self).retrieve!(File.basename(_content.path)) sanitized.read if sanitized.exists? else sanitized = SanitizedFile.new :tempfile => StringIO.new(file.read), :filename => File.basename(path), :content_type => file.content_type end sanitized end |