From 10795871177c08f332675508273a040344746b91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oliver=20G=C3=BCnther?= Date: Thu, 30 Nov 2017 11:04:29 +0100 Subject: [PATCH] Move from LocalFileUploader to FileUploader mixin So that S3 Uploads benefit from the change as well [ci skip] --- app/uploaders/file_uploader.rb | 33 ++++++++++++++++++++++++++++ app/uploaders/local_file_uploader.rb | 33 ---------------------------- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/app/uploaders/file_uploader.rb b/app/uploaders/file_uploader.rb index 0abf9bcaaf..34a430d553 100644 --- a/app/uploaders/file_uploader.rb +++ b/app/uploaders/file_uploader.rb @@ -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') diff --git a/app/uploaders/local_file_uploader.rb b/app/uploaders/local_file_uploader.rb index 78d2f01103..e341dc2321 100644 --- a/app/uploaders/local_file_uploader.rb +++ b/app/uploaders/local_file_uploader.rb @@ -40,37 +40,4 @@ class LocalFileUploader < CarrierWave::Uploader::Base dir = "#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}" OpenProject::Configuration.attachments_storage_path.join(dir) 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 end