From 6c202448ade514cd5ecca7175555011c851735e9 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Tue, 2 Jul 2019 22:31:31 +0100 Subject: [PATCH] Move user images to Active Storage with paperclip as a fallback --- Gemfile | 3 ++ Gemfile.lock | 2 ++ .../images/large.png => avatar_large.png} | Bin .../images/small.png => avatar_small.png} | Bin .../images/user-icons.svg => avatars.svg} | 0 app/assets/javascripts/user.js | 4 +-- app/controllers/users_controller.rb | 8 ++--- app/helpers/user_helper.rb | 26 ++++++++++++--- app/mailers/notifier.rb | 15 +++++++-- app/models/user.rb | 5 +++ app/views/api/users/_user.builder | 2 +- app/views/users/account.html.erb | 30 +++++++++--------- test/controllers/users_controller_test.rb | 12 +++---- test/helpers/user_helper_test.rb | 12 +++---- 14 files changed, 78 insertions(+), 41 deletions(-) rename app/assets/images/{users/images/large.png => avatar_large.png} (100%) rename app/assets/images/{users/images/small.png => avatar_small.png} (100%) rename app/assets/images/{users/images/user-icons.svg => avatars.svg} (100%) diff --git a/Gemfile b/Gemfile index f41ac015b..06530b731 100644 --- a/Gemfile +++ b/Gemfile @@ -126,6 +126,9 @@ gem "mimemagic" # Used for browser detection gem "browser" +# Used to resize user images +gem "mini_magick" + # Gems useful for development group :development do gem "annotate" diff --git a/Gemfile.lock b/Gemfile.lock index 65c6f2a89..a29ad9989 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -241,6 +241,7 @@ GEM mime-types-data (~> 3.2015) mime-types-data (3.2019.0331) mimemagic (0.3.3) + mini_magick (4.9.3) mini_mime (1.0.1) mini_portile2 (2.4.0) minitest (5.11.3) @@ -482,6 +483,7 @@ DEPENDENCIES listen logstasher mimemagic + mini_magick minitest (~> 5.1) oauth-plugin (>= 0.5.1) omniauth diff --git a/app/assets/images/users/images/large.png b/app/assets/images/avatar_large.png similarity index 100% rename from app/assets/images/users/images/large.png rename to app/assets/images/avatar_large.png diff --git a/app/assets/images/users/images/small.png b/app/assets/images/avatar_small.png similarity index 100% rename from app/assets/images/users/images/small.png rename to app/assets/images/avatar_small.png diff --git a/app/assets/images/users/images/user-icons.svg b/app/assets/images/avatars.svg similarity index 100% rename from app/assets/images/users/images/user-icons.svg rename to app/assets/images/avatars.svg diff --git a/app/assets/javascripts/user.js b/app/assets/javascripts/user.js index 69cc259f0..7cbdb1e11 100644 --- a/app/assets/javascripts/user.js +++ b/app/assets/javascripts/user.js @@ -85,8 +85,8 @@ $(document).ready(function () { $("select#user_auth_provider").on("change", updateAuthUID); - $("input#user_image").on("change", function () { - $("#image_action_new").prop("checked", true); + $("input#user_avatar").on("change", function () { + $("#avatar_action_new").prop("checked", true); }); function enableAuth() { diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 17031848d..8c234006e 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -660,15 +660,15 @@ class UsersController < ApplicationController user.languages = params[:user][:languages].split(",") - case params[:image_action] + case params[:avatar_action] when "new" then - user.image = params[:user][:image] + user.avatar.attach(params[:user][:avatar]) user.image_use_gravatar = false when "delete" then - user.image = nil + user.avatar.purge user.image_use_gravatar = false when "gravatar" then - user.image = nil + user.avatar.purge user.image_use_gravatar = true end diff --git a/app/helpers/user_helper.rb b/app/helpers/user_helper.rb index 1fc8e3ed2..c22fef1ea 100644 --- a/app/helpers/user_helper.rb +++ b/app/helpers/user_helper.rb @@ -7,8 +7,12 @@ module UserHelper if user.image_use_gravatar user_gravatar_tag(user, options) - else + elsif user.avatar.attached? + image_tag user.avatar.variant(:resize => "100x100>"), options + elsif user.image.file? image_tag user.image.url(:large), options + else + image_tag "avatar_large.png", options end end @@ -18,8 +22,12 @@ module UserHelper if user.image_use_gravatar user_gravatar_tag(user, options) - else + elsif user.avatar.attached? + image_tag user.avatar.variant(:resize => "50x50>"), options + elsif user.image.file? image_tag user.image.url(:small), options + else + image_tag "avatar_small.png", options end end @@ -29,16 +37,24 @@ module UserHelper if user.image_use_gravatar user_gravatar_tag(user, options) - else + elsif user.avatar.attached? + image_tag user.avatar.variant(:resize => "50x50>"), options + elsif user.image.file? image_tag user.image.url(:small), options + else + image_tag "avatar_small.png", options end end def user_image_url(user, options = {}) if user.image_use_gravatar user_gravatar_url(user, options) - else + elsif user.avatar.attached? + url_for(user.avatar.variant(:resize => "100x100>")) + elsif user.image.file? image_url(user.image.url(:large)) + else + image_url("avatar_large.png") end end @@ -65,7 +81,7 @@ module UserHelper def user_gravatar_url(user, options = {}) size = options[:size] || 100 hash = Digest::MD5.hexdigest(user.email.downcase) - default_image_url = image_url("users/images/large.png") + default_image_url = image_url("avatar_large.png") "#{request.protocol}www.gravatar.com/avatar/#{hash}.jpg?s=#{size}&d=#{u(default_image_url)}" end diff --git a/app/mailers/notifier.rb b/app/mailers/notifier.rb index e705efb00..bd2c83b56 100644 --- a/app/mailers/notifier.rb +++ b/app/mailers/notifier.rb @@ -1,4 +1,6 @@ class Notifier < ActionMailer::Base + include ActionView::Helpers::AssetUrlHelper + default :from => Settings.email_from, :return_path => Settings.email_return_path, :auto_submitted => "auto-generated" @@ -177,7 +179,16 @@ class Notifier < ActionMailer::Base end def attach_user_avatar(user) - attachments.inline["avatar.png"] = File.read(user_avatar_file_path(user)) + attachments.inline["avatar.png"] = user_avatar_file(user) + end + + def user_avatar_file(user) + avatar = user&.avatar + if avatar&.attached? + return avatar.variant(:resize => "50x50>").blob.download + else + return File.read(user_avatar_file_path(user)) + end end def user_avatar_file_path(user) @@ -185,7 +196,7 @@ class Notifier < ActionMailer::Base if image&.file? return image.path(:small) else - return Rails.root.join("app", "assets", "images", "users", "images", "small.png") + return Rails.root.join("app", "assets", "images", "avatar_small.png") end end diff --git a/app/models/user.rb b/app/models/user.rb index 1d0089599..13248e4fb 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -85,6 +85,8 @@ class User < ActiveRecord::Base scope :active, -> { where(:status => %w[active confirmed]) } scope :identifiable, -> { where(:data_public => true) } + has_one_attached :avatar + has_attached_file :image, :default_url => "/assets/:class/:attachment/:style.png", :styles => { :large => "100x100>", :small => "50x50>" } @@ -267,6 +269,8 @@ class User < ActiveRecord::Base ## # delete a user - leave the account but purge most personal data def delete + avatar.purge + self.display_name = "user_#{id}" self.description = "" self.home_lat = nil @@ -277,6 +281,7 @@ class User < ActiveRecord::Base self.auth_provider = nil self.auth_uid = nil self.status = "deleted" + save end diff --git a/app/views/api/users/_user.builder b/app/views/api/users/_user.builder index 7bf8e18a5..638e8583f 100644 --- a/app/views/api/users/_user.builder +++ b/app/views/api/users/_user.builder @@ -8,7 +8,7 @@ xml.tag! "user", :id => user.id, else xml.tag! "contributor-terms", :agreed => user.terms_agreed.present? end - xml.tag! "img", :href => user_image_url(user) if user.image.file? || user.image_use_gravatar + xml.tag! "img", :href => user_image_url(user) if user.avatar.attached? || user.image.file? || user.image_use_gravatar xml.tag! "roles" do user.roles.each do |role| xml.tag! role.role diff --git a/app/views/users/account.html.erb b/app/views/users/account.html.erb index b0839ad56..7e8d533b0 100644 --- a/app/views/users/account.html.erb +++ b/app/views/users/account.html.erb @@ -104,40 +104,40 @@ <%= user_image current_user %>