Merge pull request #9994 from opf/fix/attachments_cache_cleanup

Fix: attachments cache cleanup
pull/9997/head
Oliver Günther 3 years ago committed by GitHub
commit 1314126200
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 13
      app/models/attachment.rb
  2. 20
      app/workers/backup_job.rb
  3. 2
      lib/tasks/attachments.rake

@ -285,6 +285,19 @@ class Attachment < ApplicationRecord
end end
end end
##
# Deletes locally cached files. This is mostly relevant for remote attachments
# but would also apply for local attachments if things such as carrierwave
# filters were used.
#
# @param age_in_seconds [Integer] Delete all cached files older than this many seconds.
def self.clean_cached_files!(age_in_seconds: 60 * 60 * 24)
uploader = OpenProject::Configuration.file_uploader
cache_storage = uploader.cache_storage
cache_storage.new(uploader.new).clean_cache! age_in_seconds
end
def pending_direct_upload? def pending_direct_upload?
digest == "" && downloads == -1 digest == "" && downloads == -1
end end

@ -142,6 +142,9 @@ class BackupJob < ::ApplicationJob
end end
def create_backup_archive!(file_name:, db_dump_file_name:, attachments: attachments_to_include) def create_backup_archive!(file_name:, db_dump_file_name:, attachments: attachments_to_include)
paths_to_clean = []
clean_up = OpenProject::Configuration.remote_storage?
Zip::File.open(file_name, Zip::File::CREATE) do |zipfile| Zip::File.open(file_name, Zip::File::CREATE) do |zipfile|
attachments.each do |attachment| attachments.each do |attachment|
# If an attachment is destroyed on disk, skip i # If an attachment is destroyed on disk, skip i
@ -151,16 +154,33 @@ class BackupJob < ::ApplicationJob
path = diskfile.path path = diskfile.path
zipfile.add "attachment/file/#{attachment.id}/#{attachment[:file]}", path zipfile.add "attachment/file/#{attachment.id}/#{attachment[:file]}", path
paths_to_clean << get_cache_folder_path(attachment) if clean_up && a.file.cached?
end end
zipfile.get_output_stream("openproject.sql") { |f| f.write File.read(db_dump_file_name) } zipfile.get_output_stream("openproject.sql") { |f| f.write File.read(db_dump_file_name) }
end end
# delete locally cached files that were downloaded just for the backup
paths_to_clean.each do |path|
FileUtils.rm_rf path
end
@archived = true @archived = true
file_name file_name
end end
def get_cache_folder_path(attachment)
# expecting paths like /tmp/op_uploaded_files/1639754082-3468-0002-0911/file.ext
# just making extra sure so we don't delete anything wrong later on
unless attachment.diskfile.path =~ /#{attachment.file.cache_dir}\/[^\/]+\/[^\/]+/
raise "Unexpected cache path for attachment ##{attachment.id}: #{attachment.diskfile}"
end
Pathname(attachment.disfile.path).parent.to_s
end
def attachments_to_include def attachments_to_include
return Attachment.none if skip_attachments? return Attachment.none if skip_attachments?

@ -31,7 +31,7 @@
namespace :attachments do namespace :attachments do
desc 'Clear all attachments created before yesterday' desc 'Clear all attachments created before yesterday'
task clear: [:environment] do task clear: [:environment] do
CarrierWave.clean_cached_files! Attachment.clean_cached_files!
end end
desc 'Copies all attachments from the current to the given storage.' desc 'Copies all attachments from the current to the given storage.'

Loading…
Cancel
Save