Skip to content

Commit

Permalink
DEV: improve uploads:recover job so it stores a map of old to new sha
Browse files Browse the repository at this point in the history
Previous attempt created broken images
  • Loading branch information
SamSaffron committed May 22, 2019
1 parent ebcb571 commit e8799f0
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
12 changes: 12 additions & 0 deletions lib/tasks/uploads.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions lib/upload_recovery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,14 @@ def ensure_upload!(post:, sha1:, upload:)
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!
Expand Down

0 comments on commit e8799f0

Please sign in to comment.