aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRanXom <saiyedshizain@gmail.com>2026-07-13 13:40:46 +0530
committerRanXom <saiyedshizain@gmail.com>2026-07-13 13:40:46 +0530
commitb6baa98390bd57357351395915f45234edc9bcbc (patch)
tree960b24dbf9157d5e0213f39aa85305554aae83fb
parent12226cf4b6b6b050a12ceb7c47f1b67c0cd16b78 (diff)
downloadgalleryd-b6baa98390bd57357351395915f45234edc9bcbc.tar.xz
galleryd-b6baa98390bd57357351395915f45234edc9bcbc.zip
feat(thumbnail): save generated thumbnails
-rw-r--r--go.mod8
-rw-r--r--go.sum6
-rw-r--r--internal/thumbnail/generator.go10
-rw-r--r--internal/thumbnail/save.go36
4 files changed, 55 insertions, 5 deletions
diff --git a/go.mod b/go.mod
index c671a7e..4395569 100644
--- a/go.mod
+++ b/go.mod
@@ -3,7 +3,9 @@ module github.com/RanXom/galleryd
go 1.26.5
require (
- github.com/disintegration/imaging v1.6.2 // indirect
- github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd // indirect
- golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 // indirect
+ github.com/disintegration/imaging v1.6.2
+ github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd
+ golang.org/x/image v0.0.0-20211028202545-6944b10bf410
)
+
+require github.com/chai2010/webp v1.4.0 // indirect
diff --git a/go.sum b/go.sum
index be15ef4..a7e23bf 100644
--- a/go.sum
+++ b/go.sum
@@ -1,7 +1,13 @@
+github.com/chai2010/webp v1.4.0 h1:6DA2pkkRUPnbOHvvsmGI3He1hBKf/bkRlniAiSGuEko=
+github.com/chai2010/webp v1.4.0/go.mod h1:0XVwvZWdjjdxpUEIf7b9g9VkHFnInUSYujwqTLEuldU=
github.com/disintegration/imaging v1.6.2 h1:w1LecBlG2Lnp8B3jk5zSuNqd7b4DXhcjwek1ei82L+c=
github.com/disintegration/imaging v1.6.2/go.mod h1:44/5580QXChDfwIclfc/PCwrr44amcmDAg8hxG0Ewe4=
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd h1:CmH9+J6ZSsIjUK3dcGsnCnO41eRBOnY12zwkn5qVwgc=
github.com/rwcarlsen/goexif v0.0.0-20190401172101-9e8deecbddbd/go.mod h1:hPqNNc0+uJM6H+SuU8sEs5K5IQeKccPqeSjfgcKGgPk=
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8 h1:hVwzHzIUGRjiF7EcUjqNxk3NCfkPxbDKRdnNE1Rpg0U=
golang.org/x/image v0.0.0-20191009234506-e7c1f5e7dbb8/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
+golang.org/x/image v0.0.0-20211028202545-6944b10bf410 h1:hTftEOvwiOq2+O8k2D5/Q7COC7k5Qcrgc2TFURJYnvQ=
+golang.org/x/image v0.0.0-20211028202545-6944b10bf410/go.mod h1:023OzeP/+EPmXeapQh35lcL3II3LrY8Ic+EFFKVhULM=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
+golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
+golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
diff --git a/internal/thumbnail/generator.go b/internal/thumbnail/generator.go
index 226fc0e..bc4edda 100644
--- a/internal/thumbnail/generator.go
+++ b/internal/thumbnail/generator.go
@@ -22,12 +22,18 @@ func (g *Generator) Generate(
ctx context.Context,
photo gallery.Photo,
) (Thumbnail, error) {
- _, err := generateImage(photo)
+ path := g.cachePath(photo)
+
+ img, err := generateImage(photo)
if err != nil {
return Thumbnail{}, err
}
+ if err := saveImage(path, img); err != nil {
+ return Thumbnail{}, err
+ }
+
return Thumbnail{
- Path: g.cachePath(photo),
+ Path: path,
}, 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
+}