From 0f1137dafae2380b1ff5df45d08b1635b6f43c76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9gis=20Hanol?= Date: Wed, 1 Aug 2018 22:58:46 +0200 Subject: [PATCH] FIX: 'migrate_from_s3' rake task wasn't handling short urls --- lib/tasks/uploads.rake | 30 +++++++++++++++++++++++++++--- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/lib/tasks/uploads.rake b/lib/tasks/uploads.rake index 6e5b7fcb3e2..e2a6972f9f8 100644 --- a/lib/tasks/uploads.rake +++ b/lib/tasks/uploads.rake @@ -121,9 +121,10 @@ def migrate_from_s3 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 AND raw LIKE '%.s3%.amazonaws.com/%'").find_each do |post| + Post + .where("user_id > 0") + .where("raw LIKE '%.s3%.amazonaws.com/%' OR raw LIKE '%(upload://%'") + .find_each do |post| begin updated = false @@ -154,6 +155,29 @@ def migrate_from_s3 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 post.save! post.rebake!