Skip to content
Snippets Groups Projects
Verified Commit 32dde28b authored by Loïs Poujade's avatar Loïs Poujade
Browse files

Populate Mirador catalog with files from /data

Try to use all files ending in .json from $HTTP_FOLDER/ and from its first
subfolder level as manifests in mirador catalog

Closes #8
parent 9f1665bb
Branches
No related tags found
1 merge request!7Populate Mirador catalog with files from /data
/*
* 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 {
get_initial_catalog: function(url = '/data/') {
const req_init = {headers: {'Accept': 'application/json'}};
return fetch(url, req_init)
// fetch root ressources & try to parse it to json
.then(response => {
if (!response.ok)
throw new Error(`failed to list manifests from '${document.location}/data', 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')
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
.filter(e => !e.endsWith('.json'))
.map(folder => fetch(url + folder, req_init)
.then(e => e.json())
.then(e => [folder, e])
)
);
// extract only successful responses values
items = items.concat(res
.filter(p => p.status === "fulfilled")
.map(p => p.value));
// return each entry ending in .json with current location,
// and folder if needed
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};
})
);
});
}
}
import Mirador from 'mirador/dist/es/src/index';
import LocalCatalog from './catalog.js';
import * as actions from 'mirador/dist/es/src/state/actions';
const config = {
id: 'demo',
......@@ -16,5 +17,16 @@ const config = {
]
}
Mirador.viewer(config, [
]);
let viewer = Mirador.viewer(config, []);
var store = viewer.store;
console.info('store: ', store);
LocalCatalog.get_initial_catalog()
.then(catalog => {
console.debug('loading local catalog: ', catalog);
catalog.forEach(manifest =>
// setTimeout avoid UI freeze
setTimeout(() => store.dispatch(actions.addResource(manifest.manifestId)), 0)
);
})
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment