aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.go79
1 files changed, 47 insertions, 32 deletions
diff --git a/main.go b/main.go
index 9601164..abb5994 100644
--- a/main.go
+++ b/main.go
@@ -2176,22 +2176,6 @@ func makeThumbnail(pathImage, pathThumb string) (int, int, error) {
return w, h, err
}
-func makeThumbnailFor(sha1 string) (message string, err error) {
- pathImage := imagePath(sha1)
- pathThumb := thumbPath(sha1)
- w, h, err := makeThumbnail(pathImage, pathThumb)
- if err != nil {
- if ee, ok := err.(*exec.ExitError); ok {
- return string(ee.Stderr), nil
- }
- return "", err
- }
-
- _, err = db.Exec(`UPDATE image SET thumbw = ?, thumbh = ?
- WHERE sha1 = ?`, w, h, sha1)
- return "", err
-}
-
// cmdThumbnail generates missing thumbnails, in parallel.
func cmdThumbnail(fs *flag.FlagSet, args []string) error {
if err := fs.Parse(args); err != nil {
@@ -2214,7 +2198,31 @@ func cmdThumbnail(fs *flag.FlagSet, args []string) error {
return err
}
}
- return parallelize(hexSHA1, makeThumbnailFor)
+
+ stmt, err := db.Prepare(
+ `UPDATE image SET thumbw = ?, thumbh = ? WHERE sha1 = ?`)
+ if err != nil {
+ return err
+ }
+ defer stmt.Close()
+
+ var mu sync.Mutex
+ return parallelize(hexSHA1, func(sha1 string) (message string, err error) {
+ pathImage := imagePath(sha1)
+ pathThumb := thumbPath(sha1)
+ w, h, err := makeThumbnail(pathImage, pathThumb)
+ if err != nil {
+ if ee, ok := err.(*exec.ExitError); ok {
+ return string(ee.Stderr), nil
+ }
+ return "", err
+ }
+
+ mu.Lock()
+ defer mu.Unlock()
+ _, err = stmt.Exec(w, h, sha1)
+ return "", err
+ })
}
// --- Perceptual hash ---------------------------------------------------------
@@ -2336,20 +2344,6 @@ func makeDhash(sha1 string) (uint64, error) {
return dhashWebP(f)
}
-func makeDhashFor(sha1 string) (message string, err error) {
- hash, err := makeDhash(sha1)
- if errors.Is(err, errIsAnimation) {
- // Ignoring this common condition.
- return "", nil
- } else if err != nil {
- return err.Error(), nil
- }
-
- _, err = db.Exec(
- `UPDATE image SET dhash = ? WHERE sha1 = ?`, int64(hash), sha1)
- return "", err
-}
-
// cmdDhash computes perceptual hashes from thumbnails.
func cmdDhash(fs *flag.FlagSet, args []string) error {
if err := fs.Parse(args); err != nil {
@@ -2371,7 +2365,28 @@ func cmdDhash(fs *flag.FlagSet, args []string) error {
return err
}
}
- return parallelize(hexSHA1, makeDhashFor)
+
+ stmt, err := db.Prepare(`UPDATE image SET dhash = ? WHERE sha1 = ?`)
+ if err != nil {
+ return err
+ }
+ defer stmt.Close()
+
+ var mu sync.Mutex
+ return parallelize(hexSHA1, func(sha1 string) (message string, err error) {
+ hash, err := makeDhash(sha1)
+ if errors.Is(err, errIsAnimation) {
+ // Ignoring this common condition.
+ return "", nil
+ } else if err != nil {
+ return err.Error(), nil
+ }
+
+ mu.Lock()
+ defer mu.Unlock()
+ _, err = stmt.Exec(int64(hash), sha1)
+ return "", err
+ })
}
// --- Main --------------------------------------------------------------------