Skip to content

A platform for community discussion. Free, open, simple.

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

Régis Hanol0f1137dafae2
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!