[object Object]

← back to Photo Cleanup

server: add timeout to sips dimension probe + always clean up scratch BMP via finally

f1d92870b8b273ee36d6bcf74abbf02f08b4e175 · 2026-05-18 20:24:18 -0700 · SteveStudio2

Files touched

Diff

commit f1d92870b8b273ee36d6bcf74abbf02f08b4e175
Author: SteveStudio2 <steve@designerwallcoverings.com>
Date:   Mon May 18 20:24:18 2026 -0700

    server: add timeout to sips dimension probe + always clean up scratch BMP via finally
---
 server.js | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/server.js b/server.js
index 98a7e8e..712d1f1 100644
--- a/server.js
+++ b/server.js
@@ -46,12 +46,12 @@ function metrics(p) {
   if (cache.has(p)) return cache.get(p);
   const out = { pixelWidth: null, pixelHeight: null, bright: null, spread: null };
   try {
-    const info = cp.execSync(`sips -g pixelWidth -g pixelHeight ${JSON.stringify(p)}`, { stdio: ['ignore','pipe','ignore'] }).toString();
+    const info = cp.execSync(`sips -g pixelWidth -g pixelHeight ${JSON.stringify(p)}`, { stdio: ['ignore','pipe','ignore'], timeout: 5000 }).toString();
     const w = info.match(/pixelWidth:\s+(\d+)/); if (w) out.pixelWidth = Number(w[1]);
     const h = info.match(/pixelHeight:\s+(\d+)/); if (h) out.pixelHeight = Number(h[1]);
   } catch (_) {}
+  const bmpPath = path.join(TRASH_DIR, '__px_' + Math.random().toString(36).slice(2) + '.bmp');
   try {
-    const bmpPath = path.join(TRASH_DIR, '__px_' + Math.random().toString(36).slice(2) + '.bmp');
     cp.execSync(`sips -s format bmp -z 4 4 ${JSON.stringify(p)} --out ${JSON.stringify(bmpPath)}`, { stdio: ['ignore','ignore','ignore'], timeout: 5000 });
     const buf = fs.readFileSync(bmpPath);
     const offset = buf.readUInt32LE(10);
@@ -66,8 +66,11 @@ function metrics(p) {
       out.bright = +(all.reduce((a,b) => a+b, 0) / all.length).toFixed(1);
       out.spread = Math.max(...all) - Math.min(...all);
     }
-    fs.unlinkSync(bmpPath);
-  } catch (_) {}
+  } catch (_) {
+  } finally {
+    // Always remove the scratch BMP, even if sips partially wrote it before failing.
+    try { fs.unlinkSync(bmpPath); } catch (_) {}
+  }
   cache.set(p, out);
   return out;
 }

← a8c7c07 test: use ephemeral port + verify own server so stale proces  ·  back to Photo Cleanup  ·  (newest)