Download image from sso provider


(Dorianamouroux) #1

Hey there,

I’m creating a plugin to be able to authenticate my users through my own OAuth2 provider.
After login, I want to download his profile image and act like he uploaded it.
I took the sso avatar function

  def pull_image(user, avatar_url)
    begin
      tempfile = FileHelper.download(avatar_url, 1.megabyte, "sso-avatar", true)

      ext = FastImage.type(tempfile).to_s
      tempfile.rewind

      upload = Upload.create_for(user.id, tempfile, ("avatar"), File.size(tempfile.path), { origin: avatar_url })
      user.uploaded_avatar_id = upload.id

      if !user.user_avatar.contains_upload?(upload.id)
        user.user_avatar.custom_upload_id = upload.id
      end
    rescue SocketError
     # skip saving, we are not connected to the net
      Rails.logger.warn "Failed to download external avatar: #{avatar_url}, socket error - user id #{ user.id }"
    ensure
      tempfile.close! if tempfile && tempfile.respond_to?(:close!)
    end
  end

I always got this error wrong constant name #<Class:0x007ff12ab518a0> from this line

upload = Upload.create_for(user.id, tempfile, "avatar", File.size(tempfile.path), { origin: avatar_url })

I tried a bit of everything, but I can’t figure out why I have this issue

Thank you for your help,
Dorian