Skip to content
Snippets Groups Projects

Populate Mirador catalog with files from /data

Merged Loïs Poujade requested to merge 8-populate-manifest-catalog into main
All threads resolved!
2 files
+ 68
1
Compare changes
  • Side-by-side
  • Inline

Files

src/catalog.js 0 → 100644
+ 53
0
 
/*
 
* List .json files found in root or first subdirectory level and
 
* return them as a mirador catalog
 
* This code works for webpack dev server and caddy (as file server), and is not expected
 
* to function for other webservers
 
*/
 
 
export default {
 
// 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}/${subfolder}`, req_init)
 
.then(response => {
 
if (!response.ok) {
 
throw new Error(`failed to list manifests from ${url}, http response code: ${reponse.status}`);
 
}
 
return response.json();
 
})
 
.then(async (response_json) => {
 
// 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(/\/$/, ''));
 
}
 
 
// 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 => this.get_initial_catalog(url, depth - 1, folder))
 
);
 
 
// 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();
 
 
// prepend with original url only if we are at the root folder,
 
// else prepend only with current folder
 
return items
 
.map(e => subfolder == '' ? `${url}/${e}` : `${subfolder}/${e}`);
 
});
 
}
 
}
Loading