A platform for community discussion. Free, open, simple.
FEATURE: use GIVE_UP=1 to inform rake posts:missing_uploads you are done
Sam Saffron9a232e1a0ac2
diff --git a/lib/tasks/posts.rake b/lib/tasks/posts.rakeindex 34e637f9ad5..7f79fed3d41 100644--- a/lib/tasks/posts.rake+++ b/lib/tasks/posts.rake@@ -459,6 +459,18 @@ def missing_uploadsputs "#{old_scheme_upload_count} of #{missing[:uploads].count} are old scheme uploads." if old_scheme_upload_count > 0puts "#{missing[:post_uploads].count} of #{Post.count} posts are affected.", ""+ if ENV['GIVE_UP'] == "1"+ missing[:post_uploads].each do |id, uploads|+ post = Post.with_deleted.find_by(id: id)+ if post+ puts "#{post.full_url} giving up on #{uploads.length} upload/s"+ PostCustomField.create!(post_id: post.id, name: Post::MISSING_UPLOADS_IGNORED, value: "t")+ else+ puts "could not find post #{id}"+ end+ end+ end+if ENV['VERBOSE'] == "1"puts "missing uploads!"missing[:uploads].each do |path|@@ -532,7 +544,7 @@ def recover_uploads_from_index(path)db = RailsMultisite::ConnectionManagement.current_dbcdn_path = SiteSetting.cdn_path("/uploads/#{db}").sub(/https?:/, "")Post.where("cooked LIKE '%#{cdn_path}%'").each do |post|- regex = Regexp.new("((https?)?#{Regexp.escape(cdn_path)}[^,;\t\n\s)\"\']+)")+ regex = Regexp.new("((https?:)?#{Regexp.escape(cdn_path)}[^,;\\]\\>\\t\\n\\s)\"\']+)")uploads = []post.raw.scan(regex).each do |match|uploads << match[0]@@ -540,6 +552,9 @@ def recover_uploads_from_index(path)if uploads.length > 0lookup << [post.id, uploads]+ else+ print "."+ post.rebake!endend@@ -557,6 +572,16 @@ def recover_uploads_from_index(path)if raw.scan(upload).length == 0upload = upload.sub(Discourse.base_url + "/", "/")end+ if raw.scan(upload).length == 0+ # last resort, try for sha+ sha = upload.split("/")[-1]+ sha = sha.split(".")[0]++ if sha.length == 40 && raw.scan(sha).length == 1+ raw.match(Regexp.new("([^\"'<\\s\\n]+#{sha}[^\"'>\\s\\n]+)"))+ upload = $1+ end+ endif raw.scan(upload).length == 0puts "can not find #{orig} in\n\n#{raw}"upload = nil@@ -579,9 +604,9 @@ def recover_uploads_from_index(path)nextend- name = File.basename(url).split("_")[0]+ name = File.basename(url).split("_")[0].split(".")[0]puts "Searching for #{url} (#{name}) in index"- if name.length < 40+ if name.length != 40puts "Skipping #{url} in #{post.full_url} cause it appears to have a short file name"nextend