From 58c7165ce52ce1c5cdfe8a090466b82b3cb99e42 Mon Sep 17 00:00:00 2001 From: sunglocto Date: Sat, 2 May 2026 06:56:08 +0100 Subject: [PATCH] fix avatars --- cache.go | 60 +++++++++++++++++++++++++++++--------------------- gtk-message.go | 17 +++++++++----- main.go | 2 +- 3 files changed, 48 insertions(+), 31 deletions(-) diff --git a/cache.go b/cache.go index 73ff099..de20d70 100644 --- a/cache.go +++ b/cache.go @@ -13,10 +13,12 @@ import ( "net/http" "os" "path/filepath" + "sync" ) // global or app-level map/cache -var textureCache = make(map[string]gdk.Paintabler) +// var textureCache = make(map[string]gdk.Paintabler) +var textureCache sync.Map // Invalid images, if an image/avatar cannot be loaded on the system (e.g: incompatible format) it's put here var invalidImages = make(map[string]bool) @@ -31,49 +33,57 @@ func ensureCache() (string, error) { return cachePath, nil } -func getTexture(path string) gdk.Paintabler { - if tex, exists := textureCache[path]; exists { - return tex +func getTexture(path string) (gdk.Paintabler, error) { + tex, exists := textureCache.Load(path) + if exists { + return tex.(gdk.Paintabler), nil } + tex, err := gdk.NewTextureFromFilename(path) // load once if err != nil { - panic(err) + return nil, err } - textureCache[path] = tex - return tex + textureCache.Store(path, tex) + return tex.(gdk.Paintabler), nil } -func newPictureFromPath(path string) *gtk.Picture { - tex := getTexture(path) +func newPictureFromPath(path string) (*gtk.Picture, error) { + tex, err := getTexture(path) + if err != nil { + return nil, err + } img := gtk.NewPictureForPaintable(tex) - return img + return img, nil } -func newImageFromPath(path string) *gtk.Image { - tex := getTexture(path) +func newImageFromPath(path string) (*gtk.Image, error) { + tex, err := getTexture(path) + if err != nil { + return nil, err + } img := gtk.NewImageFromPaintable(tex) - return img + return img, nil } -func newPictureFromWeb(url string) *gtk.Picture { +func newPictureFromWeb(url string) (*gtk.Picture, error) { pa, _ := ensureCache() // step 1: get a sha256 sum of the URL sum := fmt.Sprintf("%x", sha256.Sum256([]byte(url))) - p, ok := textureCache[sum] + p, ok := textureCache.Load(sum) if ok { - return gtk.NewPictureForPaintable(p) + return gtk.NewPictureForPaintable(p.(gdk.Paintabler)), nil } // step 2: download it resp, err := http.Get(url) if err != nil { - return nil + return nil, err } b, err := io.ReadAll(resp.Body) if err != nil { - return nil + return nil, err } fullpath := filepath.Join(pa, sum) @@ -81,31 +91,31 @@ func newPictureFromWeb(url string) *gtk.Picture { // step 3: save it err = os.WriteFile(fullpath, b, 0644) if err != nil { - return nil + return nil, err } return newPictureFromPath(fullpath) } -func newImageFromWeb(url string) *gtk.Image { +func newImageFromWeb(url string) (*gtk.Image, error) { pa, _ := ensureCache() // step 1: get a sha256 sum of the URL sum := fmt.Sprintf("%x", sha256.Sum256([]byte(url))) - p, ok := textureCache[sum] + p, ok := textureCache.Load(sum) if ok { - return gtk.NewImageFromPaintable(p) + return gtk.NewImageFromPaintable(p.(gdk.Paintabler)), nil } // step 2: download it resp, err := http.Get(url) if err != nil { - return nil + return nil, err } b, err := io.ReadAll(resp.Body) if err != nil { - return nil + return nil, err } fullpath := filepath.Join(pa, sum) @@ -113,7 +123,7 @@ func newImageFromWeb(url string) *gtk.Image { // step 3: save it err = os.WriteFile(fullpath, b, 0644) if err != nil { - return nil + return nil, err } return newImageFromPath(fullpath) diff --git a/gtk-message.go b/gtk-message.go index 0bea5c9..b6479de 100644 --- a/gtk-message.go +++ b/gtk-message.go @@ -10,7 +10,6 @@ import ( "github.com/diamondburned/gotk4/pkg/glib/v2" "github.com/diamondburned/gotk4/pkg/gtk/v4" "github.com/google/uuid" - "github.com/jacoblockett/sanitizefilename" "github.com/jasonlovesdoggo/gopen" "gosrc.io/xmpp/stanza" "mellium.im/xmpp/jid" @@ -305,11 +304,15 @@ func getAvatar(j, hash string) *gtk.Image { // TODO: This function probably shou return createIdenticon(j) } - hash = filepath.Join(p, sanitizefilename.Sanitize(hash)) + hash = filepath.Join(p, hash) _, err = os.ReadFile(hash) if err == nil { - i := newImageFromPath(hash) + i, err := newImageFromPath(hash) + if err != nil { + invalidImages[oghash] = true + return createIdenticon(j) + } i.AddCSSClass(loadedConfig.CVD.String() + "_CVD") return i } @@ -318,7 +321,7 @@ func getAvatar(j, hash string) *gtk.Image { // TODO: This function probably shou Type: "get", From: clientroot.Session.BindJid, To: j, - Id: "vc2", + Id: uuid.New().String(), Lang: "en", }) @@ -356,7 +359,11 @@ func getAvatar(j, hash string) *gtk.Image { // TODO: This function probably shou panic(err) } - i := newImageFromPath(hash) + i, err := newImageFromPath(hash) + if err != nil { + invalidImages[oghash] = true + return createIdenticon(j) + } i.AddCSSClass(loadedConfig.CVD.String() + "_CVD") return i } diff --git a/main.go b/main.go index 678d090..ae63dad 100644 --- a/main.go +++ b/main.go @@ -272,7 +272,7 @@ func main() { composing := stanza.StateComposing{} ok = m.Get(&composing) if ok && current == JidMustParse(m.From).Bare() { - typingStatus.SetText(fmt.Sprintf("%s %s", m.From, loadedLocale["isTyping"])) + typingStatus.SetText(fmt.Sprintf("%s%s", m.From, loadedLocale["isTyping"])) return }