From 9e7d5a3cdfb3720effa771ae7f56c86e71f954c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Wed, 28 Mar 2018 15:51:47 +0200 Subject: [PATCH] FIX: 'uploads:recover_from_tombstone' rake task wasn't restoring attachments --- lib/tasks/uploads.rake | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/lib/tasks/uploads.rake b/lib/tasks/uploads.rake index 0332f896520..d69f3078a73 100644 --- a/lib/tasks/uploads.rake +++ b/lib/tasks/uploads.rake @@ -409,8 +409,14 @@ def recover_from_tombstone end begin - original_setting = SiteSetting.max_image_size_kb - SiteSetting.max_image_size_kb = 10240 + previous_image_size = SiteSetting.max_image_size_kb + previous_attachment_size = SiteSetting.max_attachment_size_kb + previous_extensions = SiteSetting.authorized_extensions + + SiteSetting.max_image_size_kb = 10 * 1024 + SiteSetting.max_attachment_size_kb = 10 * 1024 + SiteSetting.authorized_extensions = "*" + current_db = RailsMultisite::ConnectionManagement.current_db public_path = Rails.root.join("public") paths = Dir.glob(File.join(public_path, 'uploads', 'tombstone', current_db, '**', '*.*')) @@ -424,9 +430,10 @@ def recover_from_tombstone doc = Nokogiri::HTML::fragment(post.raw) updated = false - doc.css("img[src]").each do |img| - url = img["src"] + image_urls = doc.css("img[src]").map { |img| img["src"] } + attachment_urls = doc.css("a.attachment[href]").map { |a| a["href"] } + (image_urls + attachment_urls).each do |url| next if !url.start_with?("/uploads/") next if Upload.exists?(url: url) @@ -473,7 +480,9 @@ def recover_from_tombstone end end ensure - SiteSetting.max_image_size_kb = original_setting + SiteSetting.max_image_size_kb = previous_image_size + SiteSetting.max_attachment_size_kb = previous_attachment_size + SiteSetting.authorized_extensions = previous_extensions end end