Update gravatar methods to check for Active Storage images
authorTom Hughes <tom@compton.nu>
Tue, 16 Jul 2019 17:34:50 +0000 (18:34 +0100)
committerTom Hughes <tom@compton.nu>
Tue, 16 Jul 2019 21:29:07 +0000 (22:29 +0100)
app/controllers/users_controller.rb
lib/tasks/upgrade_avatars.rake [deleted file]
script/gravatar

index 9c918fe..a3c21cd 100644 (file)
@@ -779,7 +779,7 @@ class UsersController < ApplicationController
   # check if this user has a gravatar and set the user pref is true
   def gravatar_enable(user)
     # code from example https://en.gravatar.com/site/implement/images/ruby/
-    return false if user.image.present?
+    return false if user.avatar.attached?
 
     hash = Digest::MD5.hexdigest(user.email.downcase)
     url = "https://www.gravatar.com/avatar/#{hash}?d=404" # without d=404 we will always get an image back
diff --git a/lib/tasks/upgrade_avatars.rake b/lib/tasks/upgrade_avatars.rake
deleted file mode 100644 (file)
index 0466609..0000000
+++ /dev/null
@@ -1,17 +0,0 @@
-namespace "storage" do
-  task :upgrade_avatars => :environment do
-    User.active.where.not(:image_file_name => nil).in_batches.each_record do |user|
-      next if user.avatar.attached?
-
-      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
index 59ecff4..2c2c2bd 100755 (executable)
@@ -5,7 +5,7 @@
 start = 0
 User.where("image_use_gravatar AND id >=" + start.to_s).order("id").find_each do |user|
   p "checked up to id " + user.id.to_s if (user.id % 1000).zero? # just give a rough indication where we are for restarting
-  next if user.image.present?
+  next if user.avatar.attached?
 
   hash = Digest::MD5.hexdigest(user.email.downcase)
   url = "https://www.gravatar.com/avatar/#{hash}?d=404" # without d=404 we will always get an image back