Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serve avatar locally #8

Merged
merged 2 commits into from
Aug 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ class User < ApplicationRecord
validates :full_name, presence: true

before_validation :set_default_username, on: :create
after_save :generate_open_graph_image
after_save :download_and_store_avatar, if: :saved_change_to_avatar?

serialize :tags, JSON

after_save :generate_open_graph_image

def parsed_tags
if tags.is_a?(String)
begin
Expand All @@ -30,9 +30,34 @@ def generate_open_graph_image
OpenGraphImageGenerator.new(self).generate
end

def download_and_store_avatar
return unless avatar.present? && 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)}"
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}"
end
end

private

def set_default_username
self.username ||= email.split('@').first
end
end
end
7 changes: 6 additions & 1 deletion app/views/links/user_links.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +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">
<img src="<%= @user.avatar %>" alt="<%= @user.email %>" class="rounded-full border-4 border-white object-cover" style="width: 8rem; height: auto;">
<% if @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>
<div class="user-info mt-4">
<h1 class="text-xl font-bold text-white text-stroke"><%= @user.full_name %></h1>
Expand Down
9 changes: 9 additions & 0 deletions lib/tasks/download_avatars.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace :users do
desc "Download and store avatars for all users"
task download_avatars: :environment do
User.find_each do |user|
user.download_and_store_avatar
puts "Processed avatar for user #{user.id}"
end
end
end
Binary file added public/avatars/avatar_1_1723378561.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.