lib/carrierwave/uploader/cache.rb in carrierwave-1.3.4 vs lib/carrierwave/uploader/cache.rb in carrierwave-2.0.0.rc
- old
+ new
@@ -1,5 +1,7 @@
+require 'securerandom'
+
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."
@@ -21,23 +23,33 @@
#
# [String] a cache id in the format TIMEINT-PID-COUNTER-RND
#
def self.generate_cache_id
[Time.now.utc.to_i,
- Process.pid,
- '%04d' % (CarrierWave::CacheCounter.increment % 1000),
- '%04d' % rand(9999)
+ SecureRandom.random_number(1_000_000_000_000_000),
+ '%04d' % (CarrierWave::CacheCounter.increment % 10_000),
+ '%04d' % SecureRandom.random_number(10_000)
].map(&:to_s).join('-')
end
module Uploader
module Cache
extend ActiveSupport::Concern
include CarrierWave::Uploader::Callbacks
include CarrierWave::Uploader::Configuration
+ included do
+ prepend Module.new {
+ def initialize(*)
+ super
+ @staged = false
+ end
+ }
+ attr_accessor :staged
+ end
+
module ClassMethods
##
# Removes cached files which are older than one day. You could call this method
# from a rake task to clean out old cached files.
@@ -50,11 +62,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)
- cache_storage.new(CarrierWave::Uploader::Base.new).clean_cache!(seconds)
+ (cache_storage || storage).new(CarrierWave::Uploader::Base.new).clean_cache!(seconds)
end
end
##
# Returns true if the uploader has been cached
@@ -76,29 +88,23 @@
def cache_stored_file!
cache!
end
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))
- else
- sanitized = SanitizedFile.new :tempfile => StringIO.new(_content),
- :filename => File.basename(path), :content_type => file.content_type
- end
- sanitized
+ ActiveSupport::Deprecation.warn('#sanitized_file is deprecated, use #file instead.')
+ file
end
##
# 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
#
def cache_name
- File.join(cache_id, full_original_filename) if cache_id and original_filename
+ File.join(cache_id, full_original_filename) if cache_id && original_filename
end
##
# Caches the given file. Calls process! to trigger any process callbacks.
#
@@ -113,18 +119,19 @@
#
# === Raises
#
# [CarrierWave::FormNotMultipart] if the assigned parameter is a string
#
- def cache!(new_file = sanitized_file)
+ 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
@@ -154,10 +161,11 @@
# [CarrierWave::InvalidParameter] if the cache_name is incorrectly formatted.
#
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
@@ -198,10 +206,10 @@
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)
+ @cache_storage ||= (self.class.cache_storage || self.class.storage).new(self)
end
end # Cache
end # Uploader
end # CarrierWave