Several changes
This commit is contained in:
31
cache.go
31
cache.go
@@ -4,19 +4,29 @@ package main
|
||||
// It also does the same for images that need to be grabbed from HTTP.
|
||||
|
||||
import (
|
||||
"github.com/diamondburned/gotk4/pkg/gdk/v4"
|
||||
"github.com/diamondburned/gotk4/pkg/gtk/v4"
|
||||
"crypto/sha256"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"github.com/diamondburned/gotk4/pkg/gdk/v4"
|
||||
"github.com/diamondburned/gotk4/pkg/gtk/v4"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"github.com/kirsle/configdir"
|
||||
)
|
||||
|
||||
// global or app-level map/cache
|
||||
var textureCache = make(map[string]gdk.Paintabler)
|
||||
|
||||
func ensureCache() (string, error) {
|
||||
cachePath := configdir.LocalCache("lambda-im")
|
||||
err := configdir.MakePath(cachePath) // Ensure it exists.
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return cachePath, nil
|
||||
}
|
||||
|
||||
func getTexture(path string) gdk.Paintabler {
|
||||
if tex, exists := textureCache[path]; exists {
|
||||
@@ -43,6 +53,7 @@ func newImageFromPath(path string) *gtk.Image {
|
||||
}
|
||||
|
||||
func newPictureFromWeb(url string) *gtk.Picture {
|
||||
pa, _ := ensureCache()
|
||||
// step 1: get a sha256 sum of the URL
|
||||
sum := fmt.Sprintf("%x", sha256.Sum256([]byte(url)))
|
||||
|
||||
@@ -62,16 +73,19 @@ func newPictureFromWeb(url string) *gtk.Picture {
|
||||
return nil
|
||||
}
|
||||
|
||||
fullpath := filepath.Join(pa, sum)
|
||||
|
||||
// step 3: save it
|
||||
err = os.WriteFile(sum, b, 0644)
|
||||
err = os.WriteFile(fullpath, b, 0644)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return newPictureFromPath(sum)
|
||||
return newPictureFromPath(fullpath)
|
||||
}
|
||||
|
||||
func newImageFromWeb(url string) *gtk.Image {
|
||||
pa, _ := ensureCache()
|
||||
// step 1: get a sha256 sum of the URL
|
||||
sum := fmt.Sprintf("%x", sha256.Sum256([]byte(url)))
|
||||
|
||||
@@ -91,11 +105,14 @@ func newImageFromWeb(url string) *gtk.Image {
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
fullpath := filepath.Join(pa, sum)
|
||||
|
||||
// step 3: save it
|
||||
err = os.WriteFile(sum, b, 0644)
|
||||
err = os.WriteFile(fullpath, b, 0644)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
return newImageFromPath(sum)
|
||||
return newImageFromPath(fullpath)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user