aboutsummaryrefslogtreecommitdiff
path: root/internal/thumbnail/init.go
diff options
context:
space:
mode:
authorSaiyed Shizain <saiyedshizain@gmail.com>2026-07-13 14:44:12 +0530
committerGitHub <noreply@github.com>2026-07-13 14:44:12 +0530
commitcc639337ed5ccb4634b2a338f764cf9c73b02e4f (patch)
treeeae608923098a07f1413988cf0c79fc8ce4211be /internal/thumbnail/init.go
parented70370d9f58d39c3b31f5506e449aeb5d562a77 (diff)
parentc2e38ad8c3cff2d3c6ea627855b2b4f02dc3e9ba (diff)
downloadgalleryd-cc639337ed5ccb4634b2a338f764cf9c73b02e4f.tar.xz
galleryd-cc639337ed5ccb4634b2a338f764cf9c73b02e4f.zip
Merge pull request #5 from RanXom/feat/thumbnail
Feat/thumbnail: implement thumbnail generation and caching
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..4234960
--- /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
+}