DEV: improve uploads:recover job so it stores a map of old to new sha

DEV: improve uploads:recover job so it stores a map of old to new sha

Previous attempt created broken images

diff --git a/lib/tasks/uploads.rake b/lib/tasks/uploads.rake
index 8619991..1e23b83 100644
--- a/lib/tasks/uploads.rake
+++ b/lib/tasks/uploads.rake
@@ -894,6 +894,18 @@ def inline_uploads(post)
 
   post.raw = post.raw.gsub(/(\((\/uploads\S+).*\))/) do
     upload = Upload.find_by(url: $2)
+    if !upload
+      data = Upload.extract_url($2)
+      if data && sha1 = data[2]
+        upload = Upload.find_by(sha1: sha1)
+        if !upload
+          sha_map = JSON.parse(post.custom_fields["UPLOAD_SHA1_MAP"] || "{}")
+          if mapped_sha = sha_map[sha1]
+            upload = Upload.find_by(sha1: mapped_sha)
+          end
+        end
+      end
+    end
     result = $1
 
     if upload&.id
diff --git a/lib/upload_recovery.rb b/lib/upload_recovery.rb
index 136fbfd..5b3fd5b 100644
--- a/lib/upload_recovery.rb
+++ b/lib/upload_recovery.rb
@@ -67,9 +67,14 @@ class UploadRecovery
     return if !upload.persisted?
 
     if upload.sha1 != sha1
-      STDERR.puts "Warning #{post.url} had an incorrect sha, remapping #{sha1} to #{upload.sha1}"
-      post.raw = post.raw.gsub(sha1, upload.sha1)
-      post.save!
+      STDERR.puts "Warning #{post.url} had an incorrect #{sha1} should be #{upload.sha1} storing in custom field 'rake uploads:fix_relative_upload_links' can fix this"
+
+      sha_map = post.custom_fields["UPLOAD_SHA1_MAP"] || "{}"
+      sha_map = JSON.parse(sha_map)
+      sha_map[sha1] = upload.sha1
+
+      post.custom_fields["UPLOAD_SHA1_MAP"] = sha_map.to_json
+      post.save_custom_fields
     end
 
     post.rebake!

GitHub sha: e8799f0b

1 Like