lib/carrierwave/uploader/cache.rb in carrierwave-0.11.2 vs lib/carrierwave/uploader/cache.rb in carrierwave-1.0.0.beta
- old
+ new
@@ -1,7 +1,5 @@
-# encoding: utf-8
-
module CarrierWave
class FormNotMultipart < UploadError
def message
"You tried to assign a String or a Pathname to an uploader, for security reasons, this is not allowed.\n\n If this is a file upload, please check that your upload form is multipart encoded."
@@ -52,17 +50,11 @@
#
# This only works as long as you haven't done anything funky with your cache_dir.
# It's recommended that you keep cache files in one place only.
#
def clean_cached_files!(seconds=60*60*24)
- Dir.glob(File.expand_path(File.join(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
+ cache_storage.new(CarrierWave::Uploader::Base.new).clean_cache!(seconds)
end
end
##
# Returns true if the uploader has been cached
@@ -87,14 +79,12 @@
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),
+ sanitized = SanitizedFile.new :tempfile => StringIO.new(_content),
:filename => File.basename(path), :content_type => file.content_type
end
sanitized
end
@@ -125,26 +115,32 @@
#
# [CarrierWave::FormNotMultipart] if the assigned parameter is a string
#
def cache!(new_file = sanitized_file)
new_file = CarrierWave::SanitizedFile.new(new_file)
+ return if new_file.empty?
- unless new_file.empty?
- raise CarrierWave::FormNotMultipart if new_file.is_path? && ensure_multipart_form
+ 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
+ self.cache_id = CarrierWave.generate_cache_id unless cache_id
- @filename = new_file.filename
- self.original_filename = new_file.filename
+ @filename = new_file.filename
+ self.original_filename = new_file.filename
- if move_to_cache
- @file = new_file.move_to(cache_path, permissions, directory_permissions)
- else
- @file = new_file.copy_to(cache_path, permissions, directory_permissions)
- end
+ 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
##
# Retrieves the file with the given cache_name from the cache.
@@ -159,18 +155,33 @@
#
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)
+ @file = cache_storage.retrieve_from_cache!(full_filename(original_filename))
end
end
+ ##
+ # Calculates the path where the cache file should be stored.
+ #
+ # === Parameters
+ #
+ # [for_file (String)] name of the file <optional>
+ #
+ # === Returns
+ #
+ # [String] the cache path
+ #
+ def cache_path(for_file=full_filename(original_filename))
+ File.join(*[cache_dir, @cache_id, for_file].compact)
+ end
+
private
- def cache_path
- File.expand_path(File.join(cache_dir, cache_name), root)
+ def workfile_path(for_file=original_filename)
+ File.join(CarrierWave.tmp_path, @cache_id, version_name.to_s, for_file)
end
attr_reader :cache_id, :original_filename
# We can override the full_original_filename method in other modules
@@ -186,8 +197,11 @@
def original_filename=(filename)
raise CarrierWave::InvalidParameter, "invalid filename" if filename =~ CarrierWave::SanitizedFile.sanitize_regexp
@original_filename = filename
end
+ def cache_storage
+ @cache_storage ||= self.class.cache_storage.new(self)
+ end
end # Cache
end # Uploader
end # CarrierWave