Add rake task to restore missing uploads from tombstone.

This commit is contained in:
Guo Xiang Tan 2016-09-21 16:50:08 +08:00
parent 7139538286
commit 3af5b19002

View file

@ -385,6 +385,49 @@ def list_missing_uploads
end end
end end
################################################################################
# Recover from tombstone #
################################################################################
task "uploads:recover_from_tombstone" => :environment do
if ENV["RAILS_DB"]
recover_from_tombstone
else
RailsMultisite::ConnectionManagement.each_connection { recover_from_tombstone }
end
end
def recover_from_tombstone
if Discourse.store.external?
puts "This task only works for internal storages."
return
end
public_path = Rails.root.join("public")
Post.find_each do |post|
doc = Nokogiri::HTML::fragment(post.raw)
doc.css("img[src]").each do |img|
url = img["src"]
upload = Upload.find_by(url: url)
if !upload
File.open(File.join(public_path, 'uploads', 'tombstone', url[9..-1])) do |file|
new_upload = Upload.create_for(Discourse::SYSTEM_USER_ID, file, File.basename(url), File.size(file))
if new_upload.persisted?
putc "."
DbHelper.remap(url, new_upload.url)
else
puts "Failed to create upload for #{url}."
end
end
end
end
end
end
################################################################################ ################################################################################
# regenerate_missing_optimized # # regenerate_missing_optimized #
################################################################################ ################################################################################