-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathrestoreStateFile.ts
More file actions
199 lines (170 loc) · 5.54 KB
/
restoreStateFile.ts
File metadata and controls
199 lines (170 loc) · 5.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import {
DataSourceType,
Manifest,
ManifestSchema,
} from '@/src/io/state-file/schema';
import {
asErrorResult,
ImportHandler,
StateFileSetupResult,
} from '@/src/io/import/common';
import { MANIFEST, isStateFile } from '@/src/io/state-file/serialize';
import { partition, getURLBasename } from '@/src/utils';
import { useSegmentGroupStore } from '@/src/store/segmentGroups';
import { useToolStore } from '@/src/store/tools';
import { useLayersStore } from '@/src/store/datasets-layers';
import { extractFilesFromZip } from '@/src/io/zip';
import type { FileEntry } from '@/src/io/types';
import { Skip } from '@/src/utils/evaluateChain';
import { useViewStore } from '@/src/store/views';
import { useViewConfigStore } from '@/src/store/view-configs';
import { migrateManifest } from '@/src/io/state-file/migrations';
import { useMessageStore } from '@/src/store/messages';
type LeafSource =
| { type: 'uri'; uri: string; name: string; mime?: string }
| { type: 'file'; file: File; fileType: string };
function resolveToLeafSources(
id: number,
byId: Record<number, DataSourceType>,
datasetFilePath: Record<string, string> | undefined,
pathToFile: Record<string, File>
): LeafSource[] {
const src = byId[id];
if (!src) return [];
switch (src.type) {
case 'uri':
return [
{
type: 'uri',
uri: src.uri,
name: src.name ?? getURLBasename(src.uri) ?? src.uri,
mime: src.mime,
},
];
case 'file': {
const filePath = datasetFilePath?.[src.fileId];
const file = filePath ? pathToFile[filePath] : undefined;
if (file) {
return [{ type: 'file', file, fileType: src.fileType }];
}
const missingFile = filePath ?? String(src.fileId);
useMessageStore().addError('State file missing expected file', {
details: missingFile,
});
return [];
}
case 'archive':
return resolveToLeafSources(
src.parent,
byId,
datasetFilePath,
pathToFile
);
case 'collection':
return src.sources.flatMap((sourceId) =>
resolveToLeafSources(sourceId, byId, datasetFilePath, pathToFile)
);
default:
return [];
}
}
function prepareLeafDataSources(manifest: Manifest, datasetFiles: FileEntry[]) {
const byId: Record<number, DataSourceType> = Object.fromEntries(
manifest.dataSources.map((ds) => [ds.id, ds])
);
const pathToFile: Record<string, File> = Object.fromEntries(
datasetFiles.map((f) => [f.archivePath, f.file])
);
const datasets =
manifest.datasets ??
manifest.dataSources
.filter((ds) => ds.type === 'uri')
.map((ds) => ({ id: String(ds.id), dataSourceId: ds.id }));
return datasets.flatMap((ds) => {
const sources = resolveToLeafSources(
ds.dataSourceId,
byId,
manifest.datasetFilePath,
pathToFile
);
const seen = new Set<string>();
const uniqueSources = sources.filter((src) => {
if (src.type !== 'uri') return true;
if (seen.has(src.uri)) return false;
seen.add(src.uri);
return true;
});
return uniqueSources.map((src) => ({
...src,
stateFileLeaf: { stateID: ds.id },
}));
});
}
export async function completeStateFileRestore(
manifest: Manifest,
stateFiles: FileEntry[],
stateIDToStoreID: Record<string, string>
) {
const viewStore = useViewStore();
Object.entries(stateIDToStoreID).forEach(([stateID, storeID]) => {
viewStore.bindViewsToData(stateID, storeID, manifest);
});
if (!manifest.viewByID) {
const storeID = manifest.primarySelection
? stateIDToStoreID[manifest.primarySelection]
: Object.values(stateIDToStoreID)[0];
if (storeID) {
viewStore.setDataForAllViews(storeID);
}
}
useViewConfigStore().deserializeAll(manifest, stateIDToStoreID);
const segmentGroupIDMap = await useSegmentGroupStore().deserialize(
manifest,
stateFiles,
stateIDToStoreID
);
useLayersStore().deserialize(manifest, stateIDToStoreID);
useToolStore().deserialize(manifest, segmentGroupIDMap, stateIDToStoreID);
}
async function parseManifestFromZip(file: File) {
const stateFileContents = await extractFilesFromZip(file);
const [manifests, restOfStateFile] = partition(
(dataFile) => dataFile.file.name === MANIFEST,
stateFileContents
);
if (manifests.length !== 1) {
throw new Error('State file does not have exactly 1 manifest');
}
const manifestString = await manifests[0].file.text();
return { manifestString, stateFiles: restOfStateFile };
}
async function parseManifestFromJson(file: File) {
const manifestString = await file.text();
return { manifestString, stateFiles: [] as FileEntry[] };
}
export const restoreStateFile: ImportHandler = async (dataSource) => {
if (dataSource.type === 'file' && (await isStateFile(dataSource.file))) {
const isJson = dataSource.fileType === 'application/json';
const { manifestString, stateFiles } = isJson
? await parseManifestFromJson(dataSource.file)
: await parseManifestFromZip(dataSource.file);
const migrated = migrateManifest(manifestString);
let manifest: Manifest;
try {
manifest = ManifestSchema.parse(migrated);
} catch (e) {
return asErrorResult(
new Error(`Unsupported state file schema or version: ${e}`),
dataSource
);
}
useViewStore().deserializeLayout(manifest);
return {
type: 'stateFileSetup',
dataSources: prepareLeafDataSources(manifest, stateFiles),
manifest,
stateFiles,
} as StateFileSetupResult;
}
return Skip;
};