feat: refactor model import to a single backend endpoint

This refactors the model import functionality to improve performance and user experience by centralizing the logic on the backend.

Previously, the frontend would parse an imported JSON file and send an individual API request for each model, which was slow and inefficient.

This change introduces a new backend endpoint, `/api/v1/models/import`, that accepts a list of model objects. The frontend now reads the selected JSON file, parses it, and sends the entire payload to the backend in a single request. The backend then processes this list, creating or updating models as necessary.

This commit also includes the following fixes:
- Handles cases where the imported JSON contains models without `meta` or `params` fields by providing default empty values.
This commit is contained in:
silentoplayz
2025-09-28 18:49:42 -04:00
parent 231d182c35
commit fe28097817
3 changed files with 29 additions and 19 deletions

View File

@@ -465,18 +465,28 @@ let modelsImportInProgress = false;
type="file"
accept=".json"
hidden
on:change={async () => {
on:change={() => {
if (importFiles.length > 0) {
modelsImportInProgress = true;
const res = await importModels(localStorage.token, importFiles[0]);
modelsImportInProgress = false;
const reader = new FileReader();
reader.onload = async (event) => {
try {
const models = JSON.parse(String(event.target.result));
modelsImportInProgress = true;
const res = await importModels(localStorage.token, models);
modelsImportInProgress = false;
if (res) {
toast.success($i18n.t('Models imported successfully'));
await init();
} else {
toast.error($i18n.t('Failed to import models'));
}
if (res) {
toast.success($i18n.t('Models imported successfully'));
await init();
} else {
toast.error($i18n.t('Failed to import models'));
}
} catch (e) {
toast.error($i18n.t('Invalid JSON file'));
console.error(e);
}
};
reader.readAsText(importFiles[0]);
}
}}
/>