Skip to content

Commit

Permalink
fix avatar
Browse files Browse the repository at this point in the history
  • Loading branch information
loftwah committed Aug 11, 2024
1 parent a1d9990 commit 5f20ec7
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
24 changes: 12 additions & 12 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
6 changes: 4 additions & 2 deletions app/views/links/user_links.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
<div class="profile-header max-w-4xl mx-auto px-4 text-center">
<div class="avatar-container flex flex-col items-center">
<div class="avatar">
<% 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 %>
<!-- Provide a default avatar image or placeholder -->
<%= image_tag "greg.jpg", alt: @user.email, class: "rounded-full border-4 border-white object-cover", style: "width: 8rem; height: auto;" %>
<% end %>
</div>
Expand Down
Empty file added public/avatars/.keep
Empty file.
File renamed without changes
Binary file modified public/uploads/og_images/loftwah_og.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 5f20ec7

Please sign in to comment.