]> git.openstreetmap.org Git - rails.git/blob - lib/tasks/upgrade_avatars.rake
Add a rake task to move user avatars to Active Storage
[rails.git] / lib / tasks / upgrade_avatars.rake
1 namespace "storage" do
2   task :upgrade_avatars => :environment do
3     User.active.where.not(:image_file_name => nil).in_batches.each_record do |user|
4       io = File.open(user.image.path)
5       filename = user.image.original_filename
6       content_type = if user.image.content_type.nil?
7                        MimeMagic.by_magic(io)&.type
8                      else
9                        user.image.content_type
10                      end
11
12       user.avatar.attach(:io => io, :filename => filename, :content_type => content_type)
13     end
14   end
15 end