feat: 添加数据集加载功能,支持从文件读取元数据和图像元数据
This commit is contained in:
@@ -1,3 +1,8 @@
|
|||||||
|
import type { DatasetMeta, ImageMeta } from "$lib/types/meta";
|
||||||
import { writable } from "svelte/store";
|
import { writable } from "svelte/store";
|
||||||
|
|
||||||
export const openedDatasetDir = writable<string | null>(null);
|
export const openedDatasetDir = writable<string | null>(null);
|
||||||
|
|
||||||
|
export const activeDatasetMeta = writable<DatasetMeta | null>(null);
|
||||||
|
|
||||||
|
export const activeDatasetImageMetas = writable<ImageMeta[]>([]);
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
import { goto } from '$app/navigation';
|
||||||
|
import { activeDatasetImageMetas, activeDatasetMeta, openedDatasetDir } from '$lib/stores/dataset';
|
||||||
|
import { isDatasetMeta, type ImageMeta } from '$lib/types/meta';
|
||||||
|
import { join } from '@tauri-apps/api/path';
|
||||||
|
import { readDir, readTextFile } from '@tauri-apps/plugin-fs';
|
||||||
|
import { isNil } from 'es-toolkit';
|
||||||
|
import { get } from 'svelte/store';
|
||||||
|
import type { PageLoad } from './$types';
|
||||||
|
|
||||||
|
export const load = (async () => {
|
||||||
|
const activeDataset = get(openedDatasetDir);
|
||||||
|
|
||||||
|
if (isNil(activeDataset)) {
|
||||||
|
await goto('/boot');
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const metaPath = await join(activeDataset, 'meta.json');
|
||||||
|
const metaText = await readTextFile(metaPath);
|
||||||
|
const parsedMeta: unknown = JSON.parse(metaText);
|
||||||
|
|
||||||
|
if (!isDatasetMeta(parsedMeta)) {
|
||||||
|
activeDatasetMeta.set(null);
|
||||||
|
activeDatasetImageMetas.set([]);
|
||||||
|
await goto('/boot');
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
activeDatasetMeta.set(parsedMeta);
|
||||||
|
|
||||||
|
const entries = await readDir(activeDataset);
|
||||||
|
const hasImagesMeta = entries.some((entry) => entry.name === 'images.json');
|
||||||
|
|
||||||
|
if (!hasImagesMeta) {
|
||||||
|
activeDatasetImageMetas.set([]);
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
|
const imagesPath = await join(activeDataset, 'images.json');
|
||||||
|
const imagesText = await readTextFile(imagesPath);
|
||||||
|
const parsedImages: unknown = JSON.parse(imagesText);
|
||||||
|
const imageMetas = Array.isArray(parsedImages) ? (parsedImages as ImageMeta[]) : [];
|
||||||
|
|
||||||
|
activeDatasetImageMetas.set(imageMetas);
|
||||||
|
} catch {
|
||||||
|
activeDatasetMeta.set(null);
|
||||||
|
activeDatasetImageMetas.set([]);
|
||||||
|
await goto('/boot');
|
||||||
|
}
|
||||||
|
|
||||||
|
return {};
|
||||||
|
}) satisfies PageLoad;
|
||||||
Reference in New Issue
Block a user