refac
This commit is contained in:
1 parent
2140c189e1
commit
09163ccc73
1 file changed
+66
-49
@@ -619,6 +619,51 @@
|
|||||||
return currentPath && path ? `${currentPath}/${path}` : currentPath || path;
|
return currentPath && path ? `${currentPath}/${path}` : currentPath || path;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const uploadManifestEntries = async (
|
||||||
|
entries: DirectoryManifestEntry[],
|
||||||
|
resolveDirectoryId: (entry: DirectoryManifestEntry) => string | null | undefined
|
||||||
|
) => {
|
||||||
|
let failedCount = 0;
|
||||||
|
|
||||||
|
for (const [index, entry] of entries.entries()) {
|
||||||
|
const displayPath = entry.path ? `${entry.path}/${entry.filename}` : entry.filename;
|
||||||
|
syncing = $i18n.t('Uploading {{current}}/{{total}}: {{file}}', {
|
||||||
|
current: index + 1,
|
||||||
|
total: entries.length,
|
||||||
|
file: displayPath
|
||||||
|
});
|
||||||
|
|
||||||
|
const fileObject = new File([entry.file], entry.filename, { type: entry.file.type });
|
||||||
|
const uploadedFile = await uploadFile(localStorage.token, fileObject, {
|
||||||
|
knowledge_id: knowledge.id,
|
||||||
|
file_hash: entry.checksum,
|
||||||
|
directory_id: resolveDirectoryId(entry)
|
||||||
|
}).catch((error) => ({ error }));
|
||||||
|
|
||||||
|
if (!uploadedFile || uploadedFile.error) {
|
||||||
|
const error = uploadedFile?.error;
|
||||||
|
const reason =
|
||||||
|
typeof error === 'string'
|
||||||
|
? error
|
||||||
|
: (error?.detail ?? error?.message ?? $i18n.t('Failed to upload file.'));
|
||||||
|
|
||||||
|
failedCount++;
|
||||||
|
console.error('Upload failed:', displayPath, reason);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (failedCount > 0) {
|
||||||
|
toast.error(
|
||||||
|
$i18n.t('Upload failed for {{failed}} of {{total}} files.', {
|
||||||
|
failed: failedCount,
|
||||||
|
total: entries.length
|
||||||
|
})
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return failedCount;
|
||||||
|
};
|
||||||
|
|
||||||
const uploadDirectoryEntries = async (entries: DirectoryFileEntry[]) => {
|
const uploadDirectoryEntries = async (entries: DirectoryFileEntry[]) => {
|
||||||
if (!knowledge) return;
|
if (!knowledge) return;
|
||||||
|
|
||||||
@@ -645,30 +690,14 @@
|
|||||||
|
|
||||||
const directoryIdByPath = await createMissingDirectories(diff);
|
const directoryIdByPath = await createMissingDirectories(diff);
|
||||||
|
|
||||||
let uploadedCount = 0;
|
const failedCount = await uploadManifestEntries(manifest, (entry) =>
|
||||||
for (const entry of manifest) {
|
entry.path ? directoryIdByPath[getDirectoryUploadPath(entry.path)] : currentDirectoryId
|
||||||
uploadedCount++;
|
);
|
||||||
const displayPath = entry.path ? `${entry.path}/${entry.filename}` : entry.filename;
|
|
||||||
syncing = $i18n.t('Uploading {{current}}/{{total}}: {{file}}', {
|
|
||||||
current: uploadedCount,
|
|
||||||
total: manifest.length,
|
|
||||||
file: displayPath
|
|
||||||
});
|
|
||||||
|
|
||||||
const fileObject = new File([entry.file], entry.filename, { type: entry.file.type });
|
if (failedCount === 0) {
|
||||||
await uploadFile(localStorage.token, fileObject, {
|
toast.success($i18n.t('File uploaded successfully'));
|
||||||
knowledge_id: knowledge.id,
|
|
||||||
file_hash: entry.checksum,
|
|
||||||
directory_id: entry.path
|
|
||||||
? directoryIdByPath[getDirectoryUploadPath(entry.path)]
|
|
||||||
: currentDirectoryId
|
|
||||||
}).catch((e) => {
|
|
||||||
toast.error(`${e}`);
|
|
||||||
return null;
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
toast.success($i18n.t('File uploaded successfully'));
|
|
||||||
init();
|
init();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
toast.error(`${e}`);
|
toast.error(`${e}`);
|
||||||
@@ -723,36 +752,24 @@
|
|||||||
diff.modified.some((m: any) => m.filename === entry.filename && m.path === entry.path)
|
diff.modified.some((m: any) => m.filename === entry.filename && m.path === entry.path)
|
||||||
);
|
);
|
||||||
|
|
||||||
let uploadedCount = 0;
|
const failedCount = await uploadManifestEntries(filesToUpload, (entry) =>
|
||||||
for (const entry of filesToUpload) {
|
entry.path ? directoryIdByPath[entry.path] : null
|
||||||
uploadedCount++;
|
);
|
||||||
const displayPath = entry.path ? `${entry.path}/${entry.filename}` : entry.filename;
|
|
||||||
syncing = $i18n.t('Uploading {{current}}/{{total}}: {{file}}', {
|
|
||||||
current: uploadedCount,
|
|
||||||
total: filesToUpload.length,
|
|
||||||
file: displayPath
|
|
||||||
});
|
|
||||||
|
|
||||||
const fileObject = new File([entry.file], entry.filename, { type: entry.file.type });
|
|
||||||
await uploadFile(localStorage.token, fileObject, {
|
|
||||||
knowledge_id: knowledge.id,
|
|
||||||
file_hash: entry.checksum,
|
|
||||||
directory_id: entry.path ? directoryIdByPath[entry.path] : null
|
|
||||||
}).catch(() => null);
|
|
||||||
}
|
|
||||||
|
|
||||||
// ── 7. Report ──
|
// ── 7. Report ──
|
||||||
toast.success(
|
if (failedCount === 0) {
|
||||||
$i18n.t(
|
toast.success(
|
||||||
'Sync complete: {{added}} added, {{modified}} modified, {{deleted}} deleted, {{unmodified}} unmodified',
|
$i18n.t(
|
||||||
{
|
'Sync complete: {{added}} added, {{modified}} modified, {{deleted}} deleted, {{unmodified}} unmodified',
|
||||||
added: diff.added.length,
|
{
|
||||||
modified: diff.modified.length,
|
added: diff.added.length,
|
||||||
deleted: diff.deleted.length,
|
modified: diff.modified.length,
|
||||||
unmodified: diff.unmodified_count
|
deleted: diff.deleted.length,
|
||||||
}
|
unmodified: diff.unmodified_count
|
||||||
)
|
}
|
||||||
);
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
init();
|
init();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
toast.error(`${e}`);
|
toast.error(`${e}`);
|
||||||
|
|||||||
Reference in new issue
Block a user