From f39e63963191608780cccdfc2ea6b80eb566859c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lo=C3=AFs=20Poujade?= <lois.poujade@tetras-libre.fr>
Date: Mon, 31 Oct 2022 14:42:44 +0100
Subject: [PATCH] Handle arbitrary depth, not only first level

---
 src/catalog.js | 60 +++++++++++++++++++++++++-------------------------
 src/index.js   |  2 +-
 2 files changed, 31 insertions(+), 31 deletions(-)

diff --git a/src/catalog.js b/src/catalog.js
index adf42a4..aaf7627 100644
--- a/src/catalog.js
+++ b/src/catalog.js
@@ -6,48 +6,48 @@
  */
 
 export default {
-  get_initial_catalog: function(url = '/data/') {
+  // url: relative or absolute path
+  // depth: maximum depth for search of .json (use 0 to search only at root, 1 for first-level folders, etc)
+  // subfolder: only for internal usage
+  get_initial_catalog: function(url = '/data', depth=1, subfolder='') {
+    if (depth < 0) {
+      return;
+    }
     const req_init = {headers: {'Accept': 'application/json'}};
-    return fetch(url, req_init)
-      // fetch root ressources & try to parse it to json
+    return fetch(`${url}/${subfolder}`, req_init)
       .then(response => {
-        if (!response.ok)
-          throw new Error(`failed to list manifests from '${document.location}/data', http response code: ${reponse.status}`);
+        if (!response.ok) {
+          throw new Error(`failed to list manifests from ${url}, http response code: ${reponse.status}`);
+        }
         return response.json();
       })
       .then(async (response_json) => {
-        let items = [['/', response_json]];
-        // handle caddy response, which is like:
-        // [ {name: file1, props: ...}, {name: file2, props: ...}, {name: dir1, props: ...} ]
-        if (typeof(response_json[0].name) != 'undefined')
+        // handling caddy response, which is like
+        // [{name: "first.json", ...}, {name: "second.json", ...}, ...]
+        if (response_json.length >= 1 && response_json[0].hasOwnProperty('name')) {
           response_json = response_json.map(e => e.name.replace(/\/$/, ''));
+        }
 
-        // try to GET on "folders", parse responses to json,
-        // and return them as an array [folder_name, json_responses_array]
-        // (assuming files not ending in .json are folder; errors are ignored)
-        let res = await Promise.allSettled(response_json
+        // assume all files which don't end in .json are folder
+        let subfolders_content = await Promise.allSettled(response_json
           .filter(e => !e.endsWith('.json'))
-          .map(folder => fetch(url + folder, req_init)
-            .then(e => e.json())
-            .then(e => [folder, e])
-          )
+          .map(folder => this.get_initial_catalog(url, depth - 1, folder))
         );
 
-        // extract only successful responses values
-        items = items.concat(res
-          .filter(p => p.status === "fulfilled")
-          .map(p => p.value));
+        // filter files from current folder
+        // & append files from subfolders
+        let items = response_json
+          .filter(e => e.endsWith('.json'))
+          .concat(subfolders_content
+            // get only successfull queries & with a content
+            .filter(p => p.status === "fulfilled" && p.value != null)
+            .map(p => p.value)
+          ).flat();
 
-        // return each entry ending in .json with current location,
-        // and folder if needed
+        // prepend with original url only if we are at the root folder,
+        // else prepend only with current folder
         return items
-          .flatMap(([folder, entries]) => entries
-            .map(e => typeof(e.name) != 'undefined' ? e.name : e) // handling caddy response
-            .filter(e => e.endsWith('.json'))
-            .map(e => {
-              return {'manifestId': document.location + 'data' + (folder == '/' ? '/' : `/${folder}/`) + e};
-            })
-          );
+          .map(e => subfolder == '' ? `${url}/${e}` : `${subfolder}/${e}`);
       });
   }
 }
diff --git a/src/index.js b/src/index.js
index ec55786..0e7931e 100644
--- a/src/index.js
+++ b/src/index.js
@@ -37,6 +37,6 @@ LocalCatalog.get_initial_catalog()
 
     catalog.forEach(manifest => 
       // setTimeout avoid UI freeze
-      setTimeout(() => store.dispatch(actions.addResource(manifest.manifestId)), 0)
+      setTimeout(() => store.dispatch(actions.addResource(manifest)), 0)
     );
   })
-- 
GitLab