feat: 添加数据集创建功能,支持保存元数据到文件

This commit is contained in:
Vixalie
2026-03-27 16:08:35 +08:00
parent 9a7bad59ba
commit 2095374736

View File

@@ -1,8 +1,12 @@
<script lang="ts">
import { afterNavigate } from '$app/navigation';
import { afterNavigate, goto } from '$app/navigation';
import { openedDatasetDir } from '$lib/stores/dataset';
import { currentActivate } from '$lib/stores/navigate';
import type { DatasetMeta } from '$lib/types/meta';
import { join } from '@tauri-apps/api/path';
import { message, open } from '@tauri-apps/plugin-dialog';
import { readDir } from '@tauri-apps/plugin-fs';
import { readDir, writeTextFile } from '@tauri-apps/plugin-fs';
import { isNil } from 'es-toolkit';
afterNavigate(() => {
currentActivate.set('dataset');
@@ -45,6 +49,41 @@ async function selectStorePath() {
console.error('Failed to select store path:', error);
}
}
async function createDataset() {
if (!isNil(storePath) && !isNil(datasetName) && storePath.trim() && datasetName.trim()) {
const datasetMeta: DatasetMeta = {
name: datasetName.trim(),
targetModel: '',
loraType: '',
unifiedImageSize: true,
unifiedImageRatio: true,
imageSize: [512, 512],
triggerWords: [],
};
try {
const metaPath = await join(storePath, 'meta.json');
await writeTextFile(metaPath, JSON.stringify(datasetMeta, null, 2));
localStorage.setItem('#sym:openedDatasetDir', storePath);
openedDatasetDir.set(storePath);
await goto('/dataset');
} catch (error) {
console.error('Failed to create dataset:', error);
await message('Failed to create dataset. Please check permissions and try again.', {
kind: 'error',
title: 'Create Dataset Failed',
});
}
} else {
await message('Store path and dataset name are required.', {
kind: 'warning',
title: 'Missing Required Fields',
});
}
}
</script>
<main class="flex flex-col gap-2 p-4 self-center">
@@ -60,5 +99,5 @@ async function selectStorePath() {
<span class="label min-w-[10em]">Dataset Name</span>
<input type="text" class="min-w-[20em]" bind:value={datasetName} />
</label>
<button class="btn btn-primary btn-md w-fit self-center">Create</button>
<button class="btn btn-primary btn-md w-fit self-center" onclick={createDataset}>Create</button>
</main>