Issue with upload avatar during import

AI-generated summary

A user, Cara, is experiencing an issue with uploading avatars during import. They are using a Ruby script to import users and avatars, but the upload is failing even when the file is in the correct directory.

Cara initially suspected a problem with the Ruby code, but after some troubleshooting, they discovered that the issue was with the file path. pfaffman suggested adding a / at the start of the directory path, but this did not resolve the issue.

Cara eventually found a solution by using File.join to correctly construct the file path and checking if the file exists before attempting to upload it. They shared their updated code, which successfully uploads the avatars.

HI,
I’m trying to import avatar for users. I do this in the punbb.rb import file:

create_users(results, total: total_count, offset: offset) do |user|
        { id: user['id'],
          email: user['email'],
          username: user['username'],
          name: user['name'],
          created_at: Time.zone.at(user['created_at']),
          website: user['website'],
          registration_ip_address: user['registration_ip_address'],
          location: user['location'],
          moderator: user['group_id'] == 4,
          admin: user['group_id'] == 1 ,
          post_create_action: proc do |newuser|
            origname=user['avatar_file']
            path='var/www/discourse/avatars'
            upload = create_upload(newuser.id, path, origname)
            if @upload
              newuser.create_user_avatar
              newuser.user_avatar.update(custom_upload_id: upload.id)
              newuser.update(uploaded_avatar_id: upload.id)
            else
              puts "Upload failed."
            end
          end
        }

I’m noob regarding Ruby, I don’t understand where it doesn’t work. “Upload failed” even for user I know I have upload the correct file to ‘var/www/discourse/avatars’…

Thanks for your support

I think the error should be in the output. My guess is that the path is wrong. Perhaps you need a / at the start of the directory?

I tried, it didn’t work. Thanks :slight_smile:

I finally achieve the upload like this:

create_users(results, total: total_count, offset: offset) do |user|
        { id: user['id'],
          email: user['email'],
          username: user['username'],
          name: user['name'],
          created_at: Time.zone.at(user['created_at']),
          website: user['website'],
          registration_ip_address: user['registration_ip_address'],
          location: user['location'],
          #moderator: user['group_id'] == 4,
          #admin: user['group_id'] == 1 ,
          post_create_action: proc do |newuser|
            path=File.join("/var/www/discourse/avatars", user['avatar_file'])
            if File.exists?(path)
              upload = create_upload(newuser.id, path, File.basename(path))
              if upload.nil?
                puts "Upload failed. Path #{path} Id New #{newuser.id}"
              else
                newuser.create_user_avatar
                newuser.user_avatar.update(custom_upload_id: upload.id)
                newuser.update(uploaded_avatar_id: upload.id)
              end
            end
          end
        }

Thanks for the help and best regards! :slight_smile: