From dc9cb0ba078360825db7846dff13ddd6d9615940 Mon Sep 17 00:00:00 2001
From: Jonathan Wilkes <jon.w.wilkes@gmail.com>
Date: Fri, 18 Mar 2016 11:30:20 -0400
Subject: [PATCH] fix problem with [draw image] disappearing when path isn't
 found

---
 pd/nw/pdgui.js | 51 ++++++++++++++++++++++++++------------------------
 1 file changed, 27 insertions(+), 24 deletions(-)

diff --git a/pd/nw/pdgui.js b/pd/nw/pdgui.js
index 02dc053d8..76bd68909 100644
--- a/pd/nw/pdgui.js
+++ b/pd/nw/pdgui.js
@@ -2767,33 +2767,36 @@ function gui_drawimage_new(obj_tag, file_path, canvasdir, flags) {
     }
     file_path = path.normalize(file_path);
     if (fs.existsSync(file_path) && fs.lstatSync(file_path).isDirectory()) {
-
-    }
-    files = fs.readdirSync(file_path)
-                .sort(); // Note that js's "sort" method doesn't do the
-                         // "right thing" for numbers. For that we'd need
-                         // to provide our own sorting function
-    drawimage_data[obj_tag] = []; // create empty array for base64 image data
-    for (i = 0; i < files.length && i < 1000; i++) {
-        ext = path.extname(files[i]);
-
-    // todo: tolower()
-
-        if (ext === ".gif" ||
-            ext === ".jpg" ||
-            ext === ".png" ||
-            ext === ".jpeg" ||
-            ext === ".svg") {
-
-            post("we got an image at index " + i + ": " + files[i]);
-            // Now add an element to that array with the image data
-            drawimage_data[obj_tag].push({
-                type: ext === ".jpeg" ? "jpg" : ext.slice(1),
-                data: fs.readFileSync(path.join(file_path, files[i]),"base64")
-            });
+        files = fs.readdirSync(file_path)
+                    .sort(); // Note that js's "sort" method doesn't do the
+                             // "right thing" for numbers. For that we'd need
+                             // to provide our own sorting function
+        drawimage_data[obj_tag] = []; // array for base64 image data
+        // todo: warn about image sequence with > 999
+        for (i = 0; i < files.length && i < 1000; i++) {
+            ext = path.extname(files[i]);
+
+        // todo: tolower()
+
+            if (ext === ".gif" ||
+                ext === ".jpg" ||
+                ext === ".png" ||
+                ext === ".jpeg" ||
+                ext === ".svg") {
+
+                post("we got an image at index " + i + ": " + files[i]);
+                // Now add an element to that array with the image data
+                drawimage_data[obj_tag].push({
+                    type: ext === ".jpeg" ? "jpg" : ext.slice(1),
+                    data: fs.readFileSync(path.join(file_path, files[i]),"base64")
+                });
+            }
         }
+    } else {
+        i = 0;
     }
     post("no of files: " + i);
+
     if (i > 0) {
         img = new pd_window.Image(); // create an image in the pd_window context
         img.onload = function() {
-- 
GitLab