From: Tom Hughes Date: Mon, 15 Jul 2019 16:29:39 +0000 (+0100) Subject: Add a rake task to move user avatars to Active Storage X-Git-Tag: live~3026 X-Git-Url: https://git.openstreetmap.org./rails.git/commitdiff_plain/6da536e5c8870cb30594a7397e074803080e2708?ds=sidebyside;hp=a47c0a4411825f48e447641094678b15df3b2f74 Add a rake task to move user avatars to Active Storage --- diff --git a/lib/tasks/upgrade_avatars.rake b/lib/tasks/upgrade_avatars.rake new file mode 100644 index 000000000..03939dad8 --- /dev/null +++ b/lib/tasks/upgrade_avatars.rake @@ -0,0 +1,15 @@ +namespace "storage" do + task :upgrade_avatars => :environment do + User.active.where.not(:image_file_name => nil).in_batches.each_record do |user| + io = File.open(user.image.path) + filename = user.image.original_filename + content_type = if user.image.content_type.nil? + MimeMagic.by_magic(io)&.type + else + user.image.content_type + end + + user.avatar.attach(:io => io, :filename => filename, :content_type => content_type) + end + end +end