diff options
| author | Saiyed Shizain <saiyedshizain@gmail.com> | 2026-07-13 14:44:12 +0530 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2026-07-13 14:44:12 +0530 |
| commit | cc639337ed5ccb4634b2a338f764cf9c73b02e4f (patch) | |
| tree | eae608923098a07f1413988cf0c79fc8ce4211be /internal | |
| parent | ed70370d9f58d39c3b31f5506e449aeb5d562a77 (diff) | |
| parent | c2e38ad8c3cff2d3c6ea627855b2b4f02dc3e9ba (diff) | |
| download | galleryd-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')
| -rw-r--r-- | internal/gallery/builder_test.go | 8 | ||||
| -rw-r--r-- | internal/metadata/reader_test.go | 10 | ||||
| -rw-r--r-- | internal/testfixtures/Canon_40D.jpg (renamed from internal/metadata/testdata/Canon_40D.jpg) | bin | 7958 -> 7958 bytes | |||
| -rw-r--r-- | internal/testfixtures/Nikon_D70.jpg (renamed from internal/metadata/testdata/Nikon_D70.jpg) | bin | 14034 -> 14034 bytes | |||
| -rw-r--r-- | internal/testfixtures/README.md (renamed from internal/metadata/testdata/README.md) | 12 | ||||
| -rw-r--r-- | internal/testfixtures/fixtures.go | 16 | ||||
| -rw-r--r-- | internal/testfixtures/no-exif.png (renamed from internal/metadata/testdata/no-exif.png) | bin | 424 -> 424 bytes | |||
| -rw-r--r-- | internal/thumbnail/cache.go | 35 | ||||
| -rw-r--r-- | internal/thumbnail/cache_check.go | 20 | ||||
| -rw-r--r-- | internal/thumbnail/doc.go | 2 | ||||
| -rw-r--r-- | internal/thumbnail/generator.go | 58 | ||||
| -rw-r--r-- | internal/thumbnail/generator_test.go | 91 | ||||
| -rw-r--r-- | internal/thumbnail/image.go | 30 | ||||
| -rw-r--r-- | internal/thumbnail/init.go | 56 | ||||
| -rw-r--r-- | internal/thumbnail/save.go | 36 | ||||
| -rw-r--r-- | internal/thumbnail/thumbnail.go | 6 |
16 files changed, 366 insertions, 14 deletions
diff --git a/internal/gallery/builder_test.go b/internal/gallery/builder_test.go index 88b4533..634bd89 100644 --- a/internal/gallery/builder_test.go +++ b/internal/gallery/builder_test.go @@ -2,12 +2,12 @@ package gallery import ( "context" - "path/filepath" "testing" "time" "github.com/RanXom/galleryd/internal/metadata" "github.com/RanXom/galleryd/internal/scanner" + "github.com/RanXom/galleryd/internal/testfixtures" ) func TestBuilder(t *testing.T) { @@ -17,8 +17,8 @@ func TestBuilder(t *testing.T) { t.Run("builds gallery photos", func(t *testing.T) { files := []scanner.File{ { - Path: filepath.Join("..", "metadata", "testdata", "Canon_40D.jpg"), - Root: "testdata", + Path: testfixtures.Canon40D(), + Root: "testfixtures", RelativePath: "Canon_40D.jpg", ModTime: time.Now(), }, @@ -36,7 +36,7 @@ func TestBuilder(t *testing.T) { t.Run("generates deterministic ids", func(t *testing.T) { file := scanner.File{ - Path: filepath.Join("..", "metadata", "testdata", "Canon_40D.jpg"), + Path: testfixtures.Canon40D(), Root: "photos", RelativePath: "Canon_40D.jpg", ModTime: time.Now(), diff --git a/internal/metadata/reader_test.go b/internal/metadata/reader_test.go index ffbb587..772f078 100644 --- a/internal/metadata/reader_test.go +++ b/internal/metadata/reader_test.go @@ -2,11 +2,11 @@ package metadata import ( "context" - "path/filepath" "testing" "time" "github.com/RanXom/galleryd/internal/scanner" + "github.com/RanXom/galleryd/internal/testfixtures" ) func TestReader(t *testing.T) { @@ -14,7 +14,7 @@ func TestReader(t *testing.T) { t.Run("reads image dimensions", func(t *testing.T) { file := scanner.File{ - Path: filepath.Join("testdata", "Canon_40D.jpg"), + Path: testfixtures.Canon40D(), } metadata, err := reader.Read(context.Background(), file) @@ -29,7 +29,7 @@ func TestReader(t *testing.T) { t.Run("reads EXIF timestamp", func(t *testing.T) { file := scanner.File{ - Path: filepath.Join("testdata", "Canon_40D.jpg"), + Path: testfixtures.Canon40D(), } metadata, err := reader.Read(context.Background(), file) @@ -57,7 +57,7 @@ func TestReader(t *testing.T) { ) file := scanner.File{ - Path: filepath.Join("testdata", "no-exif.png"), + Path: testfixtures.NoEXIF(), ModTime: modTime, } @@ -77,7 +77,7 @@ func TestReader(t *testing.T) { t.Run("returns default orientation", func(t *testing.T) { file := scanner.File{ - Path: filepath.Join("testdata", "no-exif.png"), + Path: testfixtures.NoEXIF(), } metadata, err := reader.Read(context.Background(), file) diff --git a/internal/metadata/testdata/Canon_40D.jpg b/internal/testfixtures/Canon_40D.jpg Binary files differindex 6eb33f1..6eb33f1 100644 --- a/internal/metadata/testdata/Canon_40D.jpg +++ b/internal/testfixtures/Canon_40D.jpg diff --git a/internal/metadata/testdata/Nikon_D70.jpg b/internal/testfixtures/Nikon_D70.jpg Binary files differindex 8ea0bcc..8ea0bcc 100644 --- a/internal/metadata/testdata/Nikon_D70.jpg +++ b/internal/testfixtures/Nikon_D70.jpg diff --git a/internal/metadata/testdata/README.md b/internal/testfixtures/README.md index ba22efc..8e02dcc 100644 --- a/internal/metadata/testdata/README.md +++ b/internal/testfixtures/README.md @@ -1,6 +1,6 @@ -# Metadata Test Fixtures +# Shared Test Fixtures -These files are used by metadata package tests. +This directory contains image fixtures shared across multiple packages. ## Canon_40D.jpg @@ -23,8 +23,10 @@ Contains: ## no-exif.png -Generated by scripts/generate-test-images.sh +Generated using: -Contains: +```bash +magick -size 512x512 xc:white "$OUT/no-exif.png" +``` -- No EXIF metadata +Contains no EXIF metadata. diff --git a/internal/testfixtures/fixtures.go b/internal/testfixtures/fixtures.go new file mode 100644 index 0000000..57ebb61 --- /dev/null +++ b/internal/testfixtures/fixtures.go @@ -0,0 +1,16 @@ +// Package testfixtures holds shared fixture helpers +package testfixtures + +import "path/filepath" + +func Canon40D() string { + return filepath.Join("..", "testfixtures", "Canon_40D.jpg") +} + +func NikonD70() string { + return filepath.Join("..", "testfixtures", "Nikon_D70.jpg") +} + +func NoEXIF() string { + return filepath.Join("..", "testfixtures", "no-exif.png") +} diff --git a/internal/metadata/testdata/no-exif.png b/internal/testfixtures/no-exif.png Binary files differindex 3dcb4ef..3dcb4ef 100644 --- a/internal/metadata/testdata/no-exif.png +++ b/internal/testfixtures/no-exif.png 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 +} |