aboutsummaryrefslogtreecommitdiff
path: root/internal/thumbnail
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
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')
-rw-r--r--internal/thumbnail/cache.go35
-rw-r--r--internal/thumbnail/cache_check.go20
-rw-r--r--internal/thumbnail/doc.go2
-rw-r--r--internal/thumbnail/generator.go58
-rw-r--r--internal/thumbnail/generator_test.go91
-rw-r--r--internal/thumbnail/image.go30
-rw-r--r--internal/thumbnail/init.go56
-rw-r--r--internal/thumbnail/save.go36
-rw-r--r--internal/thumbnail/thumbnail.go6
9 files changed, 334 insertions, 0 deletions
diff --git a/internal/thumbnail/cache.go b/internal/thumbnail/cache.go
new file mode 100644
index 0000000..740268a
--- /dev/null
+++ b/internal/thumbnail/cache.go
@@ -0,0 +1,35 @@
+package thumbnail
+
+import (
+ "fmt"
+ "path/filepath"
+
+ "github.com/RanXom/galleryd/internal/gallery"
+)
+
+const (
+ thumbnailCacheVersion = 1
+ thumbnailExtension = ".webp"
+)
+
+func cacheVersion() string {
+ return fmt.Sprintf("v%d", thumbnailCacheVersion)
+}
+
+// cachePath returns the on-disk cache location for a photo thumbnail.
+//
+// Thumbnails are sharded by the first two characters of the photo ID to
+// avoid placing large numbers of files in a single directory.
+func (g *Generator) cachePath(photo gallery.Photo) string {
+ shard := photo.ID[:2]
+
+ filename := photo.ID + thumbnailExtension
+
+ return filepath.Join(
+ g.cacheDir,
+ "thumbs",
+ cacheVersion(),
+ shard,
+ filename,
+ )
+}
diff --git a/internal/thumbnail/cache_check.go b/internal/thumbnail/cache_check.go
new file mode 100644
index 0000000..9e26341
--- /dev/null
+++ b/internal/thumbnail/cache_check.go
@@ -0,0 +1,20 @@
+package thumbnail
+
+import (
+ "errors"
+ "os"
+)
+
+// thumbnailExists reports wether a cached thumbnail exists
+func thumbnailExists(path string) (bool, error) {
+ _, err := os.Stat(path)
+ if err == nil {
+ return true, nil
+ }
+
+ if errors.Is(err, os.ErrNotExist) {
+ return false, nil
+ }
+
+ return false, err
+}
diff --git a/internal/thumbnail/doc.go b/internal/thumbnail/doc.go
new file mode 100644
index 0000000..7bab376
--- /dev/null
+++ b/internal/thumbnail/doc.go
@@ -0,0 +1,2 @@
+// Package thumbnail generates and caches image thumbnails.
+package thumbnail
diff --git a/internal/thumbnail/generator.go b/internal/thumbnail/generator.go
new file mode 100644
index 0000000..ab4acc9
--- /dev/null
+++ b/internal/thumbnail/generator.go
@@ -0,0 +1,58 @@
+package thumbnail
+
+import (
+ "context"
+
+ "github.com/RanXom/galleryd/internal/gallery"
+)
+
+// Generator creates thumbnails for gallery photos
+type Generator struct {
+ cacheDir string
+}
+
+func New(cacheDir string) (*Generator, error) {
+ g := &Generator{
+ cacheDir: cacheDir,
+ }
+
+ if err := g.initializeCache(); err != nil {
+ return nil, err
+ }
+
+ return g, nil
+}
+
+// Generate creates (or retrieves) a thumbnail
+func (g *Generator) Generate(
+ ctx context.Context,
+ photo gallery.Photo,
+) (Thumbnail, error) {
+ select {
+ case <-ctx.Done():
+ return Thumbnail{}, ctx.Err()
+ default:
+ }
+
+ path := g.cachePath(photo)
+
+ exists, err := thumbnailExists(path)
+ if err != nil {
+ return Thumbnail{}, err
+ }
+
+ if !exists {
+ img, err := generateImage(photo)
+ if err != nil {
+ return Thumbnail{}, err
+ }
+
+ if err := saveImage(path, img); err != nil {
+ return Thumbnail{}, err
+ }
+ }
+
+ return Thumbnail{
+ Path: path,
+ }, nil
+}
diff --git a/internal/thumbnail/generator_test.go b/internal/thumbnail/generator_test.go
new file mode 100644
index 0000000..a5fbf67
--- /dev/null
+++ b/internal/thumbnail/generator_test.go
@@ -0,0 +1,91 @@
+package thumbnail
+
+import (
+ "context"
+ "os"
+ "testing"
+ "time"
+
+ "github.com/RanXom/galleryd/internal/gallery"
+ "github.com/RanXom/galleryd/internal/scanner"
+ "github.com/RanXom/galleryd/internal/testfixtures"
+)
+
+func TestGenerator(t *testing.T) {
+ cacheDir := t.TempDir()
+
+ generator, err := New(cacheDir)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ photo := gallery.Photo{
+ ID: "canon40d",
+
+ File: scanner.File{
+ Path: testfixtures.Canon40D(),
+ Root: "photos",
+ RelativePath: "Canon_40D.jpg",
+ ModTime: time.Now(),
+ },
+ }
+
+ t.Run("generates thumbnail", func(t *testing.T) {
+ thumb, err := generator.Generate(
+ context.Background(),
+ photo,
+ )
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ if _, err := os.Stat(thumb.Path); err != nil {
+ t.Fatalf("thumbnail was not created: %v", err)
+ }
+ })
+
+ t.Run("reuses cached thumbnail", func(t *testing.T) {
+ first, err := generator.Generate(
+ context.Background(),
+ photo,
+ )
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ info1, err := os.Stat(first.Path)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ time.Sleep(10 * time.Millisecond)
+
+ second, err := generator.Generate(
+ context.Background(),
+ photo,
+ )
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ info2, err := os.Stat(second.Path)
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ if !info1.ModTime().Equal(info2.ModTime()) {
+ t.Fatal("thumbnail was regenerated instead of reused")
+ }
+ })
+
+ t.Run("respects cancelled context", func(t *testing.T) {
+ ctx, cancel := context.WithCancel(context.Background())
+ cancel()
+
+ _, err := generator.Generate(ctx, photo)
+
+ if err == nil {
+ t.Fatal("expected context cancellation")
+ }
+ })
+}
diff --git a/internal/thumbnail/image.go b/internal/thumbnail/image.go
new file mode 100644
index 0000000..d844d53
--- /dev/null
+++ b/internal/thumbnail/image.go
@@ -0,0 +1,30 @@
+package thumbnail
+
+import (
+ "fmt"
+ "image"
+
+ "github.com/RanXom/galleryd/internal/gallery"
+ "github.com/disintegration/imaging"
+)
+
+const thumbnailSize = 512
+
+// generateImage decodes and resizes a photo.
+//
+// The returned image is not written to disk.
+func generateImage(photo gallery.Photo) (image.Image, error) {
+ src, err := imaging.Open(photo.Path)
+ if err != nil {
+ return nil, fmt.Errorf("open image: %w", err)
+ }
+
+ thumb := imaging.Fit(
+ src,
+ thumbnailSize,
+ thumbnailSize,
+ imaging.Lanczos,
+ )
+
+ return thumb, nil
+}
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
+}
diff --git a/internal/thumbnail/save.go b/internal/thumbnail/save.go
new file mode 100644
index 0000000..dba6dfa
--- /dev/null
+++ b/internal/thumbnail/save.go
@@ -0,0 +1,36 @@
+package thumbnail
+
+import (
+ "fmt"
+ "image"
+ "os"
+ "path/filepath"
+
+ "github.com/chai2010/webp"
+)
+
+// saveImage writes a thumbnail image to disk as WebP.
+func saveImage(path string, img image.Image) error {
+ if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
+ return fmt.Errorf("created cache directory: %w", err)
+ }
+
+ file, err := os.Create(path)
+ if err != nil {
+ return fmt.Errorf("create thumbnail: %w", err)
+ }
+ defer func() {
+ _ = file.Close()
+ }()
+
+ options := &webp.Options{
+ Lossless: false,
+ Quality: 80,
+ }
+
+ if err := webp.Encode(file, img, options); err != nil {
+ return fmt.Errorf("encode webp: %w", err)
+ }
+
+ return nil
+}
diff --git a/internal/thumbnail/thumbnail.go b/internal/thumbnail/thumbnail.go
new file mode 100644
index 0000000..0b9fdd6
--- /dev/null
+++ b/internal/thumbnail/thumbnail.go
@@ -0,0 +1,6 @@
+package thumbnail
+
+// Thumbnail represents a cached thumbnail.
+type Thumbnail struct {
+ Path string
+}