DEV: Safer replacement for old scheme uploads for `posts:missing_uploads`.

This commit is contained in:
Guo Xiang Tan 2019-06-25 11:34:26 +08:00
parent afb5ec811d
commit a0aeabbb94
1 changed files with 17 additions and 9 deletions

View File

@ -417,7 +417,6 @@ def missing_uploads
next if sha1.present?
puts "Fixing missing uploads: " if count_missing == 0
count_missing += 1
print "."
upload_id = nil
@ -434,20 +433,29 @@ def missing_uploads
end
if file_path.present?
if upload = UploadCreator.new(File.open(file_path), File.basename(path)).create_for(Discourse.system_user.id)
if (upload = UploadCreator.new(File.open(file_path), File.basename(path)).create_for(Discourse.system_user.id)).presisted?
upload_id = upload.id
DbHelper.remap(UrlHelper.absolute(src), upload.url)
post.reload
post.raw.gsub!(src, upload.url)
post.cooked.gsub!(src, upload.url)
new_raw = post.raw.dup
new_raw = new_raw.gsub(path, upload.url)
if post.changed?
post.save!(validate: false)
post.rebake!
end
PostRevisor.new(post, Topic.with_deleted.find_by(id: post.topic_id)).revise!(
Discourse.system_user,
{
raw: new_raw
},
skip_validations: true,
force_new_version: true,
bypass_bump: true
)
print "🆗"
else
print ""
end
else
print "🚫"
old_scheme_upload_count += 1
end