|
|
|
@ -47,6 +47,39 @@ module FileUploader |
|
|
|
|
file && File.readable?(local_file) |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
# Delete cache and old rack file after store |
|
|
|
|
# cf. https://github.com/carrierwaveuploader/carrierwave/wiki/How-to:-Delete-cache-garbage-directories |
|
|
|
|
|
|
|
|
|
before :store, :remember_cache_id |
|
|
|
|
after :store, :delete_tmp_dir |
|
|
|
|
after :store, :delete_old_tmp_file |
|
|
|
|
|
|
|
|
|
# store! nil's the cache_id after it finishes so we need to remember it for deletion |
|
|
|
|
def remember_cache_id(_new_file) |
|
|
|
|
@cache_id_was = cache_id |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def delete_tmp_dir(_new_file) |
|
|
|
|
# make sure we don't delete other things accidentally by checking the name pattern |
|
|
|
|
if @cache_id_was.present? && @cache_id_was =~ /\A[\d]{8}\-[\d]{4}\-[\d]+\-[\d]{4}\z/ |
|
|
|
|
FileUtils.rm_rf(File.join(cache_dir, @cache_id_was)) |
|
|
|
|
end |
|
|
|
|
rescue => e |
|
|
|
|
Rails.logger.error "Failed cleanup of upload file #{@cache_id_was}: #{e}" |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
# remember the tmp file |
|
|
|
|
def cache!(new_file) |
|
|
|
|
super |
|
|
|
|
@old_tmp_file = new_file |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
def delete_old_tmp_file(_dummy) |
|
|
|
|
@old_tmp_file.try :delete |
|
|
|
|
rescue => e |
|
|
|
|
Rails.logger.error "Failed cleanup of temporary upload file: #{e}" |
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
module ClassMethods |
|
|
|
|
def cache_dir |
|
|
|
|
@cache_dir ||= File.join(Dir.tmpdir, 'op_uploaded_files') |
|
|
|
|