v0.17 Profile picture / avatar code rewrite

This commit is contained in:
David Sainty 2024-06-28 00:46:16 +10:00
parent 42f1f54876
commit b547e7fc6a

View File

@ -1,5 +1,5 @@
# gossamer threads migration-import code # gossamer threads migration-import code
# v0.16.8 # v0.17
require 'mysql2' require 'mysql2'
require 'open-uri' require 'open-uri'
@ -404,6 +404,27 @@ class GossamerForumsImporter < ImportScripts::Base
end end
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 # Import user files (profile images) from Gossamer Forums to Discourse
def import_user_files(user) def import_user_files(user)
print "\rImporting files for user #{user.username}..." print "\rImporting files for user #{user.username}..."
@ -488,8 +509,12 @@ class GossamerForumsImporter < ImportScripts::Base
if resized_upload if resized_upload
# && UserAvatar.exists?(custom_upload_id: resized_upload.id) # && UserAvatar.exists?(custom_upload_id: resized_upload.id)
# Set the avatar using the resized image # Set the avatar using the resized image
user.user_avatar = UserAvatar.create!(user_id: user.id, custom_upload_id: resized_upload.id) # user.user_avatar = UserAvatar.create!(user_id: user.id, custom_upload_id: resized_upload.id)
user.save! # 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}" puts "Avatar set for user #{user.username} with upload ID #{resized_upload.id}"
else else
puts "Failed to set avatar for user #{user.username}" puts "Failed to set avatar for user #{user.username}"