mirror of
https://github.com/discourse/discourse.git
synced 2025-05-30 15:28:37 +08:00
FIX: allow upload recovery to recover uploads with sha mismatch
Filename on disk may mismatch sha of file in some old 1X setups. This will attempt to recover file even if sha1 mismatches. We had an old bug that caused this. This also adds `uploads:fix_relative_upload_links` which attempts to replace urls of the format `/upload/default/...` with `upload://`
This commit is contained in:
@ -886,3 +886,36 @@ task "uploads:recover" => :environment do
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def inline_uploads(post)
|
||||
replaced = false
|
||||
|
||||
original_raw = post.raw
|
||||
|
||||
post.raw = post.raw.gsub(/(\((\/uploads\S+).*\))/) do
|
||||
upload = Upload.find_by(url: $2)
|
||||
result = $1
|
||||
|
||||
if upload&.id
|
||||
result.sub!($2, upload.short_url)
|
||||
replaced = true
|
||||
else
|
||||
puts "Upload not found #{$2} in Post #{post.id} - #{post.url}"
|
||||
end
|
||||
result
|
||||
end
|
||||
|
||||
if replaced
|
||||
puts "Corrected image urls in #{post.url} raw backup stored in custom field"
|
||||
post.custom_fields["BACKUP_POST_RAW"] = original_raw
|
||||
post.save_custom_fields
|
||||
post.save!
|
||||
post.rebake!
|
||||
end
|
||||
end
|
||||
|
||||
task "uploads:fix_relative_upload_links" => :environment do
|
||||
Post.where('raw like ?', '%](/uploads%').find_each do |post|
|
||||
inline_uploads(post)
|
||||
end
|
||||
end
|
||||
|
Reference in New Issue
Block a user