From 4b12d4c4dcb6eadefedde280037263c6fe782011 Mon Sep 17 00:00:00 2001 From: David Berger Date: Mon, 17 Oct 2022 16:16:58 +0200 Subject: [PATCH] feat(store): Support loading file series in restoreAppState --- src/store/fileLoader.js | 2 +- src/store/index.js | 73 +++++++++++++++++++++++++++-------------- 2 files changed, 49 insertions(+), 26 deletions(-) diff --git a/src/store/fileLoader.js b/src/store/fileLoader.js index 624f25d6..8f7f77cc 100644 --- a/src/store/fileLoader.js +++ b/src/store/fileLoader.js @@ -17,7 +17,7 @@ function getSupportedExtensions() { // ---------------------------------------------------------------------------- -function getExtension(filename) { +export function getExtension(filename) { const i = filename.lastIndexOf('.'); if (i > -1) { return filename.substr(i + 1).toLowerCase(); diff --git a/src/store/index.js b/src/store/index.js index ea83b496..c7eb5f2a 100644 --- a/src/store/index.js +++ b/src/store/index.js @@ -11,7 +11,7 @@ import viewHelper from 'paraview-glance/src/components/core/VtkView/helper'; import ReaderFactory from 'paraview-glance/src/io/ReaderFactory'; import postProcessDataset from 'paraview-glance/src/io/postProcessing'; import Config from 'paraview-glance/src/config'; -import files from 'paraview-glance/src/store/fileLoader'; +import files, { getExtension } from 'paraview-glance/src/store/fileLoader'; import views from 'paraview-glance/src/store/views'; import widgets from 'paraview-glance/src/store/widgets'; import animations from 'paraview-glance/src/store/animations'; @@ -28,6 +28,10 @@ function typeOf(o) { return {}.toString.call(o).slice(8, -1).toLowerCase(); } +function extractFilenameFromUrl(url) { + return url.split('/').pop()?.split('?').shift(); +} + // quick object merge using Vue.set /* eslint-disable no-param-reassign */ function merge(dst, src) { @@ -247,17 +251,35 @@ function createStore(injected) { }; } - return ReaderFactory.downloadDataset(name, url, { - ...options, - progressCallback(progress) { - const percentage = progress.lengthComputable - ? progress.loaded / progress.total - : Infinity; - commit('files/setProgress', { id: name, percentage }); - }, - }) - .then((file) => ReaderFactory.loadFiles([file])) - .then((readers) => readers[0]) + let loadDataset; + + if (ds.seriesUrls) { + const extension = getExtension(ds.name); + const promises = ds.seriesUrls.map((u) => + ReaderFactory.downloadDataset( + extractFilenameFromUrl(u), + u, + options + ) + ); + loadDataset = Promise.all(promises).then((fileSeries) => + ReaderFactory.loadFileSeries(fileSeries, extension, name) + ); + } else { + loadDataset = ReaderFactory.downloadDataset(name, url, { + ...options, + progressCallback(progress) { + const percentage = progress.lengthComputable + ? progress.loaded / progress.total + : Infinity; + commit('files/setProgress', { id: name, percentage }); + }, + }) + .then((file) => ReaderFactory.loadFiles([file])) + .then((readers) => readers[0]); + } + + return loadDataset .then(({ dataset, reader }) => { let outDS = null; if (reader && reader.getOutputData) { @@ -269,20 +291,21 @@ function createStore(injected) { return null; } - if (outDS) { - if (ds.serializedType === 'girder') { - outDS = postProcessDataset(outDS, ds.meta); - restoreProxyKeys.set(outDS, { - girderProvenance: ds.provenance, - girderItem: ds.item, - meta: ds.meta, - }); - } else { - outDS.set(ds, true); // Attach remote data origin - } - return outDS; + if (!outDS) { + throw new Error('Invalid dataset'); + } + + if (ds.serializedType === 'girder') { + outDS = postProcessDataset(outDS, ds.meta); + restoreProxyKeys.set(outDS, { + girderProvenance: ds.provenance, + girderItem: ds.item, + meta: ds.meta, + }); + } else { + outDS.set(ds, true); // Attach remote data origin } - throw new Error('Invalid dataset'); + return outDS; }) .catch((e) => { // more meaningful error