glance/internal/assets/files.go

57 lines
888 B
Go
Raw Normal View History

2024-04-27 21:10:24 +02:00
package assets
import (
2024-06-11 13:10:11 +02:00
"crypto/md5"
2024-04-27 21:10:24 +02:00
"embed"
2024-06-11 13:10:11 +02:00
"encoding/hex"
"io"
2024-04-27 21:10:24 +02:00
"io/fs"
2024-06-11 13:10:11 +02:00
"log/slog"
"strconv"
"time"
2024-04-27 21:10:24 +02:00
)
//go:embed static
var _publicFS embed.FS
//go:embed templates
var _templateFS embed.FS
var PublicFS, _ = fs.Sub(_publicFS, "static")
var TemplateFS, _ = fs.Sub(_templateFS, "templates")
2024-06-11 13:10:11 +02:00
func getFSHash(files fs.FS) string {
hash := md5.New()
err := fs.WalkDir(files, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
file, err := files.Open(path)
if err != nil {
return err
}
if _, err := io.Copy(hash, file); err != nil {
return err
}
return nil
})
if err == nil {
return hex.EncodeToString(hash.Sum(nil))[:10]
}
slog.Warn("Could not compute assets cache", "err", err)
return strconv.FormatInt(time.Now().Unix(), 10)
}
var PublicFSHash = getFSHash(PublicFS)