Files
lambda/cache.go

119 lines
2.3 KiB
Go
Raw Normal View History

2026-01-29 21:35:36 +00:00
package main
// This file caches images in memory so we are not loading them from disk every time we need them
// It also does the same for images that need to be grabbed from HTTP.
import (
"crypto/sha256"
"fmt"
2026-01-30 10:40:38 +00:00
"github.com/diamondburned/gotk4/pkg/gdk/v4"
"github.com/diamondburned/gotk4/pkg/gtk/v4"
2026-01-29 21:35:36 +00:00
"io"
2026-01-30 10:40:38 +00:00
"net/http"
2026-01-29 21:35:36 +00:00
"os"
2026-01-30 10:40:38 +00:00
"path/filepath"
"github.com/kirsle/configdir"
2026-01-29 21:35:36 +00:00
)
// global or app-level map/cache
var textureCache = make(map[string]gdk.Paintabler)
2026-01-30 10:40:38 +00:00
func ensureCache() (string, error) {
cachePath := configdir.LocalCache("lambda-im")
err := configdir.MakePath(cachePath) // Ensure it exists.
if err != nil {
return "", err
}
2026-01-29 21:35:36 +00:00
2026-01-30 10:40:38 +00:00
return cachePath, nil
}
2026-01-29 21:35:36 +00:00
func getTexture(path string) gdk.Paintabler {
if tex, exists := textureCache[path]; exists {
return tex
}
tex, err := gdk.NewTextureFromFilename(path) // load once
if err != nil {
panic(err)
}
textureCache[path] = tex
return tex
}
func newPictureFromPath(path string) *gtk.Picture {
tex := getTexture(path)
img := gtk.NewPictureForPaintable(tex)
return img
}
func newImageFromPath(path string) *gtk.Image {
tex := getTexture(path)
img := gtk.NewImageFromPaintable(tex)
return img
}
func newPictureFromWeb(url string) *gtk.Picture {
2026-01-30 10:40:38 +00:00
pa, _ := ensureCache()
2026-01-29 21:35:36 +00:00
// step 1: get a sha256 sum of the URL
sum := fmt.Sprintf("%x", sha256.Sum256([]byte(url)))
p, ok := textureCache[sum]
if ok {
return gtk.NewPictureForPaintable(p)
}
// step 2: download it
resp, err := http.Get(url)
if err != nil {
return nil
}
b, err := io.ReadAll(resp.Body)
if err != nil {
return nil
}
2026-01-30 10:40:38 +00:00
fullpath := filepath.Join(pa, sum)
2026-01-29 21:35:36 +00:00
// step 3: save it
2026-01-30 10:40:38 +00:00
err = os.WriteFile(fullpath, b, 0644)
2026-01-29 21:35:36 +00:00
if err != nil {
return nil
}
2026-01-30 10:40:38 +00:00
return newPictureFromPath(fullpath)
2026-01-29 21:35:36 +00:00
}
func newImageFromWeb(url string) *gtk.Image {
2026-01-30 10:40:38 +00:00
pa, _ := ensureCache()
2026-01-29 21:35:36 +00:00
// step 1: get a sha256 sum of the URL
sum := fmt.Sprintf("%x", sha256.Sum256([]byte(url)))
p, ok := textureCache[sum]
if ok {
return gtk.NewImageFromPaintable(p)
}
// step 2: download it
resp, err := http.Get(url)
if err != nil {
return nil
}
b, err := io.ReadAll(resp.Body)
if err != nil {
return nil
}
2026-01-30 10:40:38 +00:00
fullpath := filepath.Join(pa, sum)
2026-01-29 21:35:36 +00:00
// step 3: save it
2026-01-30 10:40:38 +00:00
err = os.WriteFile(fullpath, b, 0644)
2026-01-29 21:35:36 +00:00
if err != nil {
return nil
}
2026-01-30 10:40:38 +00:00
return newImageFromPath(fullpath)
2026-01-29 21:35:36 +00:00
}