From 1791d8067d00dc1381b9cdc3cc4ad44c1f6c7197 Mon Sep 17 00:00:00 2001 From: erdgeist Date: Tue, 21 Jul 2026 21:01:30 +0200 Subject: Add storage isolation for tests, preventing live data destruction --- app/models/concerns/file_attachment.rb | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) (limited to 'app') diff --git a/app/models/concerns/file_attachment.rb b/app/models/concerns/file_attachment.rb index e7a33c5..548531f 100644 --- a/app/models/concerns/file_attachment.rb +++ b/app/models/concerns/file_attachment.rb @@ -66,12 +66,23 @@ module FileAttachment @upload = UploadProxy.new(self) end + class_methods do + def upload_root + Rails.env.test? ? Rails.root.join("tmp", "test_uploads") : Rails.root.join("public", "system", "uploads") + end + end + + def upload_root + self.class.upload_root + end + def process_upload return unless @pending_upload uploaded_file = @pending_upload @pending_upload = nil - old_dir = Rails.root.join("public", "system", "uploads", id.to_s) + old_dir = upload_root.join(id.to_s) + FileUtils.rm_rf(old_dir) if Dir.exist?(old_dir) original_path = file_path(:original) @@ -102,15 +113,12 @@ module FileAttachment end def delete_upload_files - dir = Rails.root.join("public", "system", "uploads", id.to_s) + dir = upload_root.join(id.to_s) FileUtils.rm_rf(dir) if Dir.exist?(dir) end def file_path(style) - Rails.root.join( - "public", "system", "uploads", - id.to_s, style.to_s, upload_file_name - ).to_s + upload_root.join(id.to_s, style.to_s, variant_filename(style)).to_s end def sanitize_filename(filename) -- cgit v1.3