From 5f20ec7fa91bc336cde14d76b07ea95055f3b1a5 Mon Sep 17 00:00:00 2001 From: Dean Lofts Date: Sun, 11 Aug 2024 23:40:22 +1000 Subject: [PATCH] fix avatar --- app/models/user.rb | 24 +++++++++--------- app/views/links/user_links.html.erb | 6 +++-- public/avatars/.keep | 0 ...ar_1_1723378561.jpg => loftwah_avatar.jpg} | Bin public/uploads/og_images/loftwah_og.png | Bin 95032 -> 95032 bytes 5 files changed, 16 insertions(+), 14 deletions(-) create mode 100644 public/avatars/.keep rename public/avatars/{avatar_1_1723378561.jpg => loftwah_avatar.jpg} (100%) diff --git a/app/models/user.rb b/app/models/user.rb index 585a608..9b373e1 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -10,7 +10,7 @@ class User < ApplicationRecord before_validation :set_default_username, on: :create after_save :generate_open_graph_image - after_save :download_and_store_avatar, if: :saved_change_to_avatar? + after_save :download_and_store_avatar serialize :tags, JSON @@ -31,27 +31,27 @@ def generate_open_graph_image end def download_and_store_avatar - return unless avatar.present? && avatar.start_with?('http') - + return if avatar.blank? || !avatar.start_with?('http') + require 'open-uri' require 'fileutils' - + begin avatar_dir = Rails.root.join('public', 'avatars') FileUtils.mkdir_p(avatar_dir) unless File.directory?(avatar_dir) - + file = URI.open(avatar) - - filename = "avatar_#{id}_#{Time.now.to_i}#{File.extname(avatar)}" + + filename = "#{username}_avatar#{File.extname(avatar)}" filepath = File.join(avatar_dir, filename) - + File.open(filepath, 'wb') do |local_file| local_file.write(file.read) end - - update_column(:avatar, "/avatars/#{filename}") - rescue OpenURI::HTTPError, SocketError => e - Rails.logger.error "Failed to download avatar for user #{id}: #{e.message}" + + Rails.logger.info "Avatar downloaded for user #{username}" + rescue StandardError => e + Rails.logger.error "Failed to download avatar for user #{username}: #{e.message}" end end diff --git a/app/views/links/user_links.html.erb b/app/views/links/user_links.html.erb index b7273a1..4ab2761 100644 --- a/app/views/links/user_links.html.erb +++ b/app/views/links/user_links.html.erb @@ -3,10 +3,12 @@
- <% if @user.avatar.present? %> + <% local_avatar_path = "/avatars/#{@user.username}_avatar#{File.extname(@user.avatar)}" %> + <% if File.exist?(Rails.root.join('public' + local_avatar_path)) %> + <%= image_tag local_avatar_path, alt: @user.email, class: "rounded-full border-4 border-white object-cover", style: "width: 8rem; height: auto;" %> + <% elsif @user.avatar.present? %> <%= image_tag @user.avatar, alt: @user.email, class: "rounded-full border-4 border-white object-cover", style: "width: 8rem; height: auto;" %> <% else %> - <%= image_tag "greg.jpg", alt: @user.email, class: "rounded-full border-4 border-white object-cover", style: "width: 8rem; height: auto;" %> <% end %>
diff --git a/public/avatars/.keep b/public/avatars/.keep new file mode 100644 index 0000000..e69de29 diff --git a/public/avatars/avatar_1_1723378561.jpg b/public/avatars/loftwah_avatar.jpg similarity index 100% rename from public/avatars/avatar_1_1723378561.jpg rename to public/avatars/loftwah_avatar.jpg diff --git a/public/uploads/og_images/loftwah_og.png b/public/uploads/og_images/loftwah_og.png index 7c6f6bf5a43dae1d55562d1276d3c60d75556ceb..874d6f5dcefefc46278381cbd5a3979b55e2c2d7 100644 GIT binary patch delta 85 zcmdn-jCIE|)(QO_+`MY0KJ41J8>erb&gfqxYG@c@Xl!L{VP$HpZD3$!V6b=<0#8>zmvv4FO#tyq8yo-t