diff --git a/portal.go b/portal.go index 69b9796..c916be0 100644 --- a/portal.go +++ b/portal.go @@ -28,6 +28,7 @@ import ( _ "image/gif" "image/jpeg" "image/png" + "io" "math" "mime" "net/http" @@ -941,6 +942,24 @@ func (portal *Portal) SyncParticipants(source *User, metadata *types.GroupInfo) portal.kickExtraUsers(participantMap) } +func reuploadAvatar(intent *appservice.IntentAPI, url string) (id.ContentURI, error) { + getResp, err := http.DefaultClient.Get(url) + if err != nil { + return id.ContentURI{}, fmt.Errorf("failed to download avatar: %w", err) + } + data, err := io.ReadAll(getResp.Body) + _ = getResp.Body.Close() + if err != nil { + return id.ContentURI{}, fmt.Errorf("failed to read avatar bytes: %w", err) + } + + resp, err := intent.UploadBytes(data, http.DetectContentType(data)) + if err != nil { + return id.ContentURI{}, fmt.Errorf("failed to upload avatar to Matrix: %w", err) + } + return resp.ContentURI, nil +} + func (user *User) updateAvatar(jid types.JID, avatarID *string, avatarURL *id.ContentURI, avatarSet *bool, log log.Logger, intent *appservice.IntentAPI) bool { currentID := "" if *avatarSet && *avatarID != "remove" && *avatarID != "unauthorized" { diff --git a/puppet.go b/puppet.go index 6a474e3..e973398 100644 --- a/puppet.go +++ b/puppet.go @@ -18,8 +18,6 @@ package main import ( "fmt" - "io" - "net/http" "regexp" "sync" "time" @@ -226,25 +224,6 @@ func (puppet *Puppet) DefaultIntent() *appservice.IntentAPI { return puppet.bridge.AS.Intent(puppet.MXID) } -func reuploadAvatar(intent *appservice.IntentAPI, url string) (id.ContentURI, error) { - getResp, err := http.DefaultClient.Get(url) - if err != nil { - return id.ContentURI{}, fmt.Errorf("failed to download avatar: %w", err) - } - data, err := io.ReadAll(getResp.Body) - _ = getResp.Body.Close() - if err != nil { - return id.ContentURI{}, fmt.Errorf("failed to read avatar bytes: %w", err) - } - - mime := http.DetectContentType(data) - resp, err := intent.UploadBytes(data, mime) - if err != nil { - return id.ContentURI{}, fmt.Errorf("failed to upload avatar to Matrix: %w", err) - } - return resp.ContentURI, nil -} - func (puppet *Puppet) UpdateAvatar(source *User, forcePortalSync bool) bool { changed := source.updateAvatar(puppet.JID, &puppet.Avatar, &puppet.AvatarURL, &puppet.AvatarSet, puppet.log, puppet.DefaultIntent()) if !changed || puppet.Avatar == "unauthorized" {