aboutsummaryrefslogtreecommitdiff
path: root/internal/thumbnail/init.go
diff options
context:
space:
mode:
authorRanXom <saiyedshizain@gmail.com>2026-07-13 14:11:53 +0530
committerRanXom <saiyedshizain@gmail.com>2026-07-13 14:11:53 +0530
commite48a9f9dafefef4844c49d417af8d13978455247 (patch)
tree4c1777ca27d38264bae5e6f01d96d63e494f3794 /internal/thumbnail/init.go
parent546751bda402887fe31f09768e29d1cf5f531f83 (diff)
downloadgalleryd-e48a9f9dafefef4844c49d417af8d13978455247.tar.xz
galleryd-e48a9f9dafefef4844c49d417af8d13978455247.zip
feat(thumbnail): initialize thumbnail cache
Diffstat (limited to 'internal/thumbnail/init.go')
-rw-r--r--internal/thumbnail/init.go56
1 files changed, 56 insertions, 0 deletions
diff --git a/internal/thumbnail/init.go b/internal/thumbnail/init.go
new file mode 100644
index 0000000..64f3b38
--- /dev/null
+++ b/internal/thumbnail/init.go
@@ -0,0 +1,56 @@
+package thumbnail
+
+import (
+ "fmt"
+ "os"
+ "path/filepath"
+ "strings"
+)
+
+// initializeCache prepares the thumbnail cache directory.
+//
+// It creates the current cache generation and removes obsolete cache
+// generations left behind by previous versions.
+func (g *Generator) initializeCache() error {
+ thumbsDir := filepath.Join(g.cacheDir, "thumbs")
+
+ if err := os.MkdirAll(
+ filepath.Join(thumbsDir, cacheVersion()),
+ 0o775,
+ ); err != nil {
+ return fmt.Errorf("create cache directory: %w", err)
+ }
+
+ entries, err := os.ReadDir(thumbsDir)
+ if err != nil {
+ return fmt.Errorf("read cache directory: %w", err)
+ }
+
+ current := cacheVersion()
+
+ for _, entry := range entries {
+ if !entry.IsDir() {
+ continue
+ }
+
+ name := entry.Name()
+
+ if strings.HasPrefix(name, "v") {
+ continue
+ }
+
+ if name == current {
+ continue
+ }
+
+ if err := os.RemoveAll(filepath.Join(thumbsDir, name)); err != nil {
+ return fmt.Errorf(
+ "remove obsolete cache %q: %w",
+ name,
+ err,
+ )
+ }
+ }
+
+ return nil
+}