aboutsummaryrefslogtreecommitdiff
path: root/internal/gallery
diff options
context:
space:
mode:
Diffstat (limited to 'internal/gallery')
-rw-r--r--internal/gallery/builder.go58
-rw-r--r--internal/gallery/builder_test.go69
-rw-r--r--internal/gallery/doc.go5
-rw-r--r--internal/gallery/id.go21
-rw-r--r--internal/gallery/photo.go17
-rw-r--r--internal/gallery/sort.go17
6 files changed, 187 insertions, 0 deletions
diff --git a/internal/gallery/builder.go b/internal/gallery/builder.go
new file mode 100644
index 0000000..038acb4
--- /dev/null
+++ b/internal/gallery/builder.go
@@ -0,0 +1,58 @@
+package gallery
+
+import (
+ "context"
+
+ "github.com/RanXom/galleryd/internal/metadata"
+ "github.com/RanXom/galleryd/internal/scanner"
+)
+
+// Builder constructs gallery photos from scanned files.
+type Builder struct {
+ reader *metadata.Reader
+}
+
+func New(reader *metadata.Reader) *Builder {
+ return &Builder{
+ reader: reader,
+ }
+}
+
+// Build converts scanned files into gallery photos.
+//
+// Photos are sorted by capture date in descending order.
+func (b *Builder) Build(
+ ctx context.Context,
+ files []scanner.File,
+) ([]Photo, error) {
+ photos := make([]Photo, 0, len(files))
+
+ select {
+ case <-ctx.Done():
+ return nil, ctx.Err()
+ default:
+ }
+
+ for _, file := range files {
+ select {
+ case <-ctx.Done():
+ return nil, ctx.Err()
+ default:
+ }
+
+ md, err := b.reader.Read(ctx, file)
+ if err != nil {
+ return nil, err
+ }
+
+ photos = append(photos, Photo{
+ ID: generateID(file),
+ File: file,
+ Metadata: md,
+ })
+ }
+
+ sortPhotos(photos)
+
+ return photos, nil
+}
diff --git a/internal/gallery/builder_test.go b/internal/gallery/builder_test.go
new file mode 100644
index 0000000..88b4533
--- /dev/null
+++ b/internal/gallery/builder_test.go
@@ -0,0 +1,69 @@
+package gallery
+
+import (
+ "context"
+ "path/filepath"
+ "testing"
+ "time"
+
+ "github.com/RanXom/galleryd/internal/metadata"
+ "github.com/RanXom/galleryd/internal/scanner"
+)
+
+func TestBuilder(t *testing.T) {
+ reader := metadata.New()
+ builder := New(reader)
+
+ t.Run("builds gallery photos", func(t *testing.T) {
+ files := []scanner.File{
+ {
+ Path: filepath.Join("..", "metadata", "testdata", "Canon_40D.jpg"),
+ Root: "testdata",
+ RelativePath: "Canon_40D.jpg",
+ ModTime: time.Now(),
+ },
+ }
+
+ photos, err := builder.Build(context.Background(), files)
+ if err != nil {
+ t.Fatalf("build gallery: %v", err)
+ }
+
+ if len(photos) != 1 {
+ t.Fatalf("expected 1 photo, got %d", len(photos))
+ }
+ })
+
+ t.Run("generates deterministic ids", func(t *testing.T) {
+ file := scanner.File{
+ Path: filepath.Join("..", "metadata", "testdata", "Canon_40D.jpg"),
+ Root: "photos",
+ RelativePath: "Canon_40D.jpg",
+ ModTime: time.Now(),
+ }
+
+ first, err := builder.Build(context.Background(), []scanner.File{file})
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ second, err := builder.Build(context.Background(), []scanner.File{file})
+ if err != nil {
+ t.Fatal(err)
+ }
+
+ if first[0].ID != second[0].ID {
+ t.Fatal("photo IDs should be deterministic")
+ }
+ })
+
+ t.Run("respects context cancellation", func(t *testing.T) {
+ ctx, cancel := context.WithCancel(context.Background())
+ cancel()
+
+ _, err := builder.Build(ctx, nil)
+ if err == nil {
+ t.Fatal("expected context cancellation error")
+ }
+ })
+}
diff --git a/internal/gallery/doc.go b/internal/gallery/doc.go
new file mode 100644
index 0000000..3dcd52e
--- /dev/null
+++ b/internal/gallery/doc.go
@@ -0,0 +1,5 @@
+// Package gallery combines scanned files and extracted metadata
+// into gallery photos.
+//
+// It defines the core domain model used throughout galleryd.
+package gallery
diff --git a/internal/gallery/id.go b/internal/gallery/id.go
new file mode 100644
index 0000000..edb5be2
--- /dev/null
+++ b/internal/gallery/id.go
@@ -0,0 +1,21 @@
+package gallery
+
+import (
+ "crypto/sha256"
+ "encoding/hex"
+ "path/filepath"
+
+ "github.com/RanXom/galleryd/internal/scanner"
+)
+
+// generateID returns a deterministic identifier for a photo.
+//
+// IDs are generated from the cleaned root and relative path to avoid
+// collisions when multiple roots contain the same directory layout.
+func generateID(file scanner.File) string {
+ canonical := filepath.Join(file.Root, file.RelativePath)
+
+ sum := sha256.Sum256([]byte(canonical))
+
+ return hex.EncodeToString(sum[:])
+}
diff --git a/internal/gallery/photo.go b/internal/gallery/photo.go
new file mode 100644
index 0000000..73a6103
--- /dev/null
+++ b/internal/gallery/photo.go
@@ -0,0 +1,17 @@
+package gallery
+
+import (
+ "github.com/RanXom/galleryd/internal/metadata"
+ "github.com/RanXom/galleryd/internal/scanner"
+)
+
+// Photo represents an image in the gallery.
+//
+// A Photo combines filesystem information with extracted metadata
+// and a stable identifier.
+type Photo struct {
+ ID string
+
+ scanner.File
+ metadata.Metadata
+}
diff --git a/internal/gallery/sort.go b/internal/gallery/sort.go
new file mode 100644
index 0000000..6c97ee3
--- /dev/null
+++ b/internal/gallery/sort.go
@@ -0,0 +1,17 @@
+package gallery
+
+import "sort"
+
+// sortPhotos orders photos by date taken (newest first).
+//
+// Photos with identical timestamps are ordered by relative path to
+// provide deterministic output.
+func sortPhotos(photos []Photo) {
+ sort.SliceStable(photos, func(i, j int) bool {
+ if !photos[i].DateTaken.Equal(photos[j].DateTaken) {
+ return photos[i].DateTaken.After(photos[j].DateTaken)
+ }
+
+ return photos[i].RelativePath < photos[j].RelativePath
+ })
+}