Enhance avatar caching by adding versioning support and improving cache read/write logic

This commit is contained in:
Leon Bösche
2026-01-30 13:41:17 +01:00
parent 87bf4b8ca3
commit 1bc1dd8460
3 changed files with 129 additions and 33 deletions

View File

@@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
"os"
"path/filepath"
"go.b0esche.cloud/backend/internal/audit"
"go.b0esche.cloud/backend/internal/auth"
@@ -13,9 +14,43 @@ import (
"go.b0esche.cloud/backend/pkg/jwt"
)
// ensureAvatarCacheDir finds a writable, preferably persistent directory for avatar cache and updates cfg
func ensureAvatarCacheDir(cfg *config.Config) {
candidates := []string{
cfg.AvatarCacheDir,
"/var/lib/b0esche/avatars",
"./data/avatars",
filepath.Join(os.TempDir(), "b0esche_avatars"),
}
for _, d := range candidates {
if d == "" {
continue
}
if err := os.MkdirAll(d, 0755); err == nil {
// Try writing a small test file to confirm write permission
testPath := filepath.Join(d, ".write_test")
if err := os.WriteFile(testPath, []byte("ok"), 0644); err == nil {
os.Remove(testPath)
if d != cfg.AvatarCacheDir {
fmt.Printf("[WARN] Avatar cache dir %q not usable, using %q instead. Please set AVATAR_CACHE_DIR to a persistent, writable volume.\n", cfg.AvatarCacheDir, d)
}
cfg.AvatarCacheDir = d
fmt.Printf("[INFO] Avatar cache directory set to %q\n", d)
return
}
}
}
// If none usable, keep configured value and let runtime fallback handle it
fmt.Printf("[WARN] No writable persistent avatar cache directory found; falling back to tmp. Set AVATAR_CACHE_DIR to a persistent path.\n")
}
func main() {
cfg := config.Load()
// Ensure avatar cache directory is usable and persistent when possible
ensureAvatarCacheDir(cfg)
dbConn, err := database.Connect(cfg)
if err != nil {
fmt.Fprintf(os.Stderr, "Database connection error: %v\n", err)