Move from LocalFileUploader to FileUploader mixin

So that S3 Uploads benefit from the change as well

[ci skip]
pull/6027/head
Oliver Günther 7 years ago
parent b64c828cca
commit 1079587117
No known key found for this signature in database
GPG Key ID: 88872239EB414F99
  1. 33
      app/uploaders/file_uploader.rb
  2. 33
      app/uploaders/local_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')

@ -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

Loading…
Cancel
Save