aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Eric Janouch <p@janouch.name>2023-12-28 04:23:30 +0100
committerPřemysl Eric Janouch <p@janouch.name>2023-12-28 04:23:30 +0100
commit5b08043210b6d55681e3d98ee112ed2be36dbb17 (patch)
treeaaf7c5c450500d01fd0ccca0543842a5fe76d372
parenta70fc447652c92c3f19651c1ad27b5a31852b077 (diff)
downloadgallery-5b08043210b6d55681e3d98ee112ed2be36dbb17.tar.gz
gallery-5b08043210b6d55681e3d98ee112ed2be36dbb17.tar.xz
gallery-5b08043210b6d55681e3d98ee112ed2be36dbb17.zip
Teach the thumbnail command to load existing files
-rw-r--r--main.go27
1 files changed, 24 insertions, 3 deletions
diff --git a/main.go b/main.go
index 971391e..5dae14e 100644
--- a/main.go
+++ b/main.go
@@ -2147,7 +2147,28 @@ func cmdCheck(fs *flag.FlagSet, args []string) error {
// --- Thumbnailing ------------------------------------------------------------
-func makeThumbnail(pathImage, pathThumb string) (int, int, error) {
+func identifyThumbnail(path string) (w, h int, err error) {
+ f, err := os.Open(path)
+ if err != nil {
+ return
+ }
+ defer f.Close()
+
+ config, err := webp.DecodeConfig(f)
+ if err != nil {
+ return
+ }
+ return config.Width, config.Height, nil
+}
+
+func makeThumbnail(load bool, pathImage, pathThumb string) (
+ w, h int, err error) {
+ if load {
+ if w, h, err = identifyThumbnail(pathThumb); err == nil {
+ return
+ }
+ }
+
thumbDirname, _ := filepath.Split(pathThumb)
if err := os.MkdirAll(thumbDirname, 0755); err != nil {
return 0, 0, err
@@ -2176,13 +2197,13 @@ func makeThumbnail(pathImage, pathThumb string) (int, int, error) {
return 0, 0, err
}
- var w, h int
_, err = fmt.Fscanf(bytes.NewReader(out), "%d %d", &w, &h)
return w, h, err
}
// cmdThumbnail generates missing thumbnails, in parallel.
func cmdThumbnail(fs *flag.FlagSet, args []string) error {
+ load := fs.Bool("load", false, "try to load existing thumbnail files")
if err := fs.Parse(args); err != nil {
return err
}
@@ -2215,7 +2236,7 @@ func cmdThumbnail(fs *flag.FlagSet, args []string) error {
return parallelize(hexSHA1, func(sha1 string) (message string, err error) {
pathImage := imagePath(sha1)
pathThumb := thumbPath(sha1)
- w, h, err := makeThumbnail(pathImage, pathThumb)
+ w, h, err := makeThumbnail(*load, pathImage, pathThumb)
if err != nil {
if ee, ok := err.(*exec.ExitError); ok {
return string(ee.Stderr), nil