Improve restore from tombstone rake task.

This commit is contained in:
Guo Xiang Tan 2016-09-21 17:47:27 +08:00
parent 3af5b19002
commit b62b6ba12c

View file

@ -403,28 +403,44 @@ def recover_from_tombstone
return return
end end
public_path = Rails.root.join("public") begin
original_setting = SiteSetting.max_image_size_kb
SiteSetting.max_image_size_kb = 10240
Post.find_each do |post| public_path = Rails.root.join("public")
doc = Nokogiri::HTML::fragment(post.raw)
doc.css("img[src]").each do |img| Post.find_each do |post|
url = img["src"] doc = Nokogiri::HTML::fragment(post.raw)
upload = Upload.find_by(url: url)
if !upload doc.css("img[src]").each do |img|
File.open(File.join(public_path, 'uploads', 'tombstone', url[9..-1])) do |file| url = img["src"]
new_upload = Upload.create_for(Discourse::SYSTEM_USER_ID, file, File.basename(url), File.size(file))
if new_upload.persisted? next unless url =~ /^\/uploads\//
putc "."
DbHelper.remap(url, new_upload.url) upload = Upload.find_by(url: url)
if !upload && url
tombstone_path = File.join(public_path, 'uploads', 'tombstone', url.gsub(/^\/uploads\//, ""))
if File.exists?(tombstone_path)
File.open(tombstone_path) 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}: #{new_upload.errors.full_messages}."
end
end
else else
puts "Failed to create upload for #{url}." puts "Failed to find file (#{tombstone_path}) in tombstone."
end end
end end
end end
end end
ensure
SiteSetting.max_image_size_kb = original_setting
end end
end end