fix avatars
This commit is contained in:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user