From b547e7fc6a837e02a19bc24d29203140c12ad63e Mon Sep 17 00:00:00 2001 From: saint Date: Fri, 28 Jun 2024 00:46:16 +1000 Subject: [PATCH] v0.17 Profile picture / avatar code rewrite --- gossamer_forums.rb | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/gossamer_forums.rb b/gossamer_forums.rb index dd415b1..8afc7ac 100644 --- a/gossamer_forums.rb +++ b/gossamer_forums.rb @@ -1,5 +1,5 @@ # gossamer threads migration-import code -# v0.16.8 +# v0.17 require 'mysql2' require 'open-uri' @@ -404,6 +404,27 @@ class GossamerForumsImporter < ImportScripts::Base end end + # Create avatar for the user using the provided image path + def create_avatar(user, avatar_path) + tempfile = Tempfile.new(['avatar', File.extname(avatar_path)]) + FileUtils.copy_file(avatar_path, tempfile.path) + filename = "avatar#{File.extname(avatar_path)}" + upload = UploadCreator.new(tempfile, filename, type: "avatar").create_for(user.id) + + if upload.present? && upload.persisted? + user.create_user_avatar + user.user_avatar.update(custom_upload_id: upload.id) + user.update(uploaded_avatar_id: upload.id) + else + puts "Failed to upload avatar for user #{user.username}: #{avatar_path}" + puts upload.errors.inspect if upload + end + rescue StandardError => e + puts "Failed to create avatar for user #{user.username}: #{avatar_path}. Error: #{e.message}" + ensure + tempfile.close! if tempfile + end + # Import user files (profile images) from Gossamer Forums to Discourse def import_user_files(user) print "\rImporting files for user #{user.username}..." @@ -488,8 +509,12 @@ class GossamerForumsImporter < ImportScripts::Base if resized_upload # && UserAvatar.exists?(custom_upload_id: resized_upload.id) # Set the avatar using the resized image - user.user_avatar = UserAvatar.create!(user_id: user.id, custom_upload_id: resized_upload.id) - user.save! +# user.user_avatar = UserAvatar.create!(user_id: user.id, custom_upload_id: resized_upload.id) +# user.save! +# user.create_user_avatar +# user.user_avatar.update(custom_upload_id: resized_upload.id) +# user.update(uploaded_avatar_id: resized_upload.id) + create_avatar(user, resized_temp_file.path) puts "Avatar set for user #{user.username} with upload ID #{resized_upload.id}" else puts "Failed to set avatar for user #{user.username}"