trying to recover old scheme uploads on 'posts:missing_uploads' rake task

This commit is contained in:
Vinoth Kannan 2019-04-25 01:16:18 +05:30
parent 505c5addca
commit b0bb1d95a5
1 changed files with 38 additions and 13 deletions

View File

@ -1,3 +1,5 @@
require 'file_store/local_store'
desc 'Update each post with latest markdown' desc 'Update each post with latest markdown'
task 'posts:rebake' => :environment do task 'posts:rebake' => :environment do
ENV['RAILS_DB'] ? rebake_posts : rebake_posts_all_sites ENV['RAILS_DB'] ? rebake_posts : rebake_posts_all_sites
@ -402,9 +404,7 @@ desc 'Finds missing post upload records from cooked HTML content'
task 'posts:missing_uploads' => :environment do task 'posts:missing_uploads' => :environment do
get_missing_uploads.delete_all get_missing_uploads.delete_all
missing_uploads = [] missing_uploads = []
found_uploads = []
old_scheme_upload_count = 0 old_scheme_upload_count = 0
unlinked_post_upload_count = 0
count = 0 count = 0
Post.have_uploads.select(:id, :cooked).find_in_batches do |posts| Post.have_uploads.select(:id, :cooked).find_in_batches do |posts|
@ -436,21 +436,47 @@ task 'posts:missing_uploads' => :environment do
end end
if sha1.blank? || sha1s.exclude?(sha1) if sha1.blank? || sha1s.exclude?(sha1)
missing_post_uploads << src upload_id = nil
if found_uploads.include?(src) if missing_uploads.exclude?(src)
unlinked_post_upload_count += 1
elsif missing_uploads.exclude?(src)
if sha1.blank? if sha1.blank?
old_scheme_upload_count += 1 # recovering old scheme upload.
missing_uploads << src local_store = FileStore::LocalStore.new
elsif Upload.exists?(sha1: sha1) public_path = "#{local_store.public_dir}#{path}"
unlinked_post_upload_count += 1 if File.exists?(public_path)
found_uploads << src tmp = Tempfile.new
tmp.write(File.read(public_path))
tmp.rewind
if upload = UploadCreator.new(tmp, File.basename(path)).create_for(Discourse.system_user.id)
upload_id = upload.id
DbHelper.remap(UrlHelper.absolute(src), upload.url)
post.raw.gsub!(src, upload.url)
post.cooked.gsub!(src, upload.url)
if post.changed?
post.save!(validate: false)
post.rebake!
end
end
FileUtils.rm(tmp, force: true)
else
old_scheme_upload_count += 1
end
else
upload_id = Upload.where(sha1: sha1).pluck(:id).first
end
if upload_id.present?
PostUpload.create!(post_id: post.id, upload_id: upload_id)
else else
missing_uploads << src missing_uploads << src
end end
end end
missing_post_uploads << src if upload_id.blank?
end end
end end
@ -464,10 +490,9 @@ task 'posts:missing_uploads' => :environment do
end end
end end
puts "", "#{count - unlinked_post_upload_count} post uploads are missing.", "" puts "", "#{count} post uploads are missing.", ""
if count > 0 if count > 0
puts "#{unlinked_post_upload_count} are unlinked post uploads." if unlinked_post_upload_count > 0
puts "#{missing_uploads.count} uploads are missing." puts "#{missing_uploads.count} uploads are missing."
puts "#{old_scheme_upload_count} of #{missing_uploads.count} are old scheme uploads." if old_scheme_upload_count > 0 puts "#{old_scheme_upload_count} of #{missing_uploads.count} are old scheme uploads." if old_scheme_upload_count > 0
puts "#{get_missing_uploads.count} of #{Post.count} posts are affected.", "" puts "#{get_missing_uploads.count} of #{Post.count} posts are affected.", ""