FIX: 'migrate_from_s3' rake task wasn't handling short urls

This commit is contained in:
Régis Hanol 2018-08-01 22:58:46 +02:00
parent 04255d1dce
commit 0f1137dafa
1 changed files with 27 additions and 3 deletions

View File

@ -121,9 +121,10 @@ def migrate_from_s3
puts "Migrating uploads from S3 to local storage for '#{db}'..." puts "Migrating uploads from S3 to local storage for '#{db}'..."
max_file_size_kb = [SiteSetting.max_image_size_kb, SiteSetting.max_attachment_size_kb].max.kilobytes Post
.where("user_id > 0")
Post.where("user_id > 0 AND raw LIKE '%.s3%.amazonaws.com/%'").find_each do |post| .where("raw LIKE '%.s3%.amazonaws.com/%' OR raw LIKE '%(upload://%'")
.find_each do |post|
begin begin
updated = false updated = false
@ -154,6 +155,29 @@ def migrate_from_s3
end end
end end
post.raw.gsub!(/(upload:\/\/[0-9a-zA-Z]+\.\w+)/) do |url|
begin
if sha1 = Upload.sha1_from_short_url(url)
if upload = Upload.find_by(sha1: sha1)
file = FileHelper.download("http:#{url}", max_file_size: 20.megabytes, tmp_file_name: "from_s3", follow_redirect: true)
filename = upload.original_filename
origin = upload.origin
upload.destroy
new_upload = UploadCreator.new(file, filename, origin: origin).create_for(post.user_id || -1)
if new_upload&.save
updated = true
url = new_upload.url
end
end
end
url
rescue
url
end
end
if updated if updated
post.save! post.save!
post.rebake! post.rebake!