mirror of
https://github.com/discourse/discourse.git
synced 2025-01-31 21:36:15 +08:00
FEATURE: fix_relative_upload_links now multisite safety
This also finds `<img src="/uploads/xyz` HTML images in raw and corrects them. Also handles some cross multisite recovery and provides better output
This commit is contained in:
parent
96e58125a7
commit
a5ce9cb470
|
@ -471,7 +471,7 @@ def missing_uploads
|
||||||
missing[:post_uploads].each do |id, uploads|
|
missing[:post_uploads].each do |id, uploads|
|
||||||
post = Post.with_deleted.find_by(id: id)
|
post = Post.with_deleted.find_by(id: id)
|
||||||
if post
|
if post
|
||||||
puts "#{post.url} missing #{uploads.join(", ")}"
|
puts "#{post.full_url} missing #{uploads.join(", ")}"
|
||||||
else
|
else
|
||||||
puts "could not find post #{id}"
|
puts "could not find post #{id}"
|
||||||
end
|
end
|
||||||
|
|
|
@ -918,16 +918,70 @@ def inline_uploads(post)
|
||||||
end
|
end
|
||||||
|
|
||||||
if replaced
|
if replaced
|
||||||
puts "Corrected image urls in #{post.url} raw backup stored in custom field"
|
puts "Corrected image urls in #{post.full_url} raw backup stored in custom field"
|
||||||
post.custom_fields["BACKUP_POST_RAW"] = original_raw
|
post.custom_fields["BACKUP_POST_RAW"] = original_raw
|
||||||
post.save_custom_fields
|
post.save_custom_fields
|
||||||
post.save!
|
post.save!(validate: false)
|
||||||
post.rebake!
|
post.rebake!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
task "uploads:fix_relative_upload_links" => :environment do
|
def inline_img_tags(post)
|
||||||
|
replaced = false
|
||||||
|
|
||||||
|
original_raw = post.raw
|
||||||
|
post.raw = post.raw.gsub(/(<img\s+src=["'](\/uploads\/[^'"]*)["'].*>)/i) do
|
||||||
|
next if $2.include?("..")
|
||||||
|
|
||||||
|
upload = Upload.find_by(url: $2)
|
||||||
|
if !upload
|
||||||
|
data = Upload.extract_url($2)
|
||||||
|
if data && sha1 = data[2]
|
||||||
|
upload = Upload.find_by(sha1: sha1)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
if !upload
|
||||||
|
local_file = File.join(Rails.root, "public", $2)
|
||||||
|
if File.exist?(local_file)
|
||||||
|
File.open(local_file) do |f|
|
||||||
|
upload = UploadCreator.new(f,"image").create_for(post.user_id)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if upload
|
||||||
|
replaced = true
|
||||||
|
"![image](#{upload.short_url})"
|
||||||
|
else
|
||||||
|
puts "skipping missing upload in #{post.full_url} #{$1}"
|
||||||
|
$1
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if replaced
|
||||||
|
puts "Corrected image urls in #{post.full_url} raw backup stored in custom field"
|
||||||
|
post.custom_fields["BACKUP_POST_RAW"] = original_raw
|
||||||
|
post.save_custom_fields
|
||||||
|
post.save!(validate: false)
|
||||||
|
post.rebake!
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def fix_relative_links
|
||||||
Post.where('raw like ?', '%](/uploads%').find_each do |post|
|
Post.where('raw like ?', '%](/uploads%').find_each do |post|
|
||||||
inline_uploads(post)
|
inline_uploads(post)
|
||||||
end
|
end
|
||||||
|
Post.where("raw ilike ?", '%<img%src=%/uploads/%>%').find_each do |post|
|
||||||
|
inline_img_tags(post)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
task "uploads:fix_relative_upload_links" => :environment do
|
||||||
|
if RailsMultisite::ConnectionManagement.current_db != "default"
|
||||||
|
fix_relative_links
|
||||||
|
else
|
||||||
|
RailsMultisite::ConnectionManagement.each_connection do
|
||||||
|
fix_relative_links
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue
Block a user