Update Database.svelte

This commit is contained in:
Classic298 2025-08-12 13:15:38 +02:00 committed by GitHub
parent 8d7273afae
commit e4a0bd8640
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,6 +1,7 @@
<script lang="ts"> <script lang="ts">
import fileSaver from 'file-saver'; import fileSaver from 'file-saver';
const { saveAs } = fileSaver; const { saveAs } = fileSaver;
import { downloadDatabase, downloadLiteLLMConfig } from '$lib/apis/utils'; import { downloadDatabase, downloadLiteLLMConfig } from '$lib/apis/utils';
import { onMount, getContext } from 'svelte'; import { onMount, getContext } from 'svelte';
import { config, user } from '$lib/stores'; import { config, user } from '$lib/stores';
@ -8,10 +9,13 @@
import { getAllUserChats } from '$lib/apis/chats'; import { getAllUserChats } from '$lib/apis/chats';
import { getAllUsers } from '$lib/apis/users'; import { getAllUsers } from '$lib/apis/users';
import { exportConfig, importConfig } from '$lib/apis/configs'; import { exportConfig, importConfig } from '$lib/apis/configs';
import PruneDataDialog from '$lib/components/common/PruneDataDialog.svelte'; import PruneDataDialog from '$lib/components/common/PruneDataDialog.svelte';
import { pruneData } from '$lib/apis/prune'; import { pruneData } from '$lib/apis/prune';
const i18n = getContext('i18n'); const i18n = getContext('i18n');
export let saveHandler: Function; export let saveHandler: Function;
let showPruneDataDialog = false; let showPruneDataDialog = false;
const exportAllUserChats = async () => { const exportAllUserChats = async () => {
let blob = new Blob([JSON.stringify(await getAllUserChats(localStorage.token))], { let blob = new Blob([JSON.stringify(await getAllUserChats(localStorage.token))], {
@ -61,7 +65,9 @@
const exportUsers = async () => { const exportUsers = async () => {
const users = await getAllUsers(localStorage.token); const users = await getAllUsers(localStorage.token);
const headers = ['id', 'name', 'email', 'role']; const headers = ['id', 'name', 'email', 'role'];
const csv = [ const csv = [
headers.join(','), headers.join(','),
...users.users.map((user) => { ...users.users.map((user) => {
@ -73,6 +79,7 @@
return `"${String(user[header]).replace(/"/g, '""')}"`; return `"${String(user[header]).replace(/"/g, '""')}"`;
}) })
.join(','); .join(',');
}) })
].join('\n'); ].join('\n');
const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' }); const blob = new Blob([csv], { type: 'text/csv;charset=utf-8;' });
@ -83,6 +90,7 @@
// permissions = await getUserPermissions(localStorage.token); // permissions = await getUserPermissions(localStorage.token);
}); });
</script> </script>
<PruneDataDialog bind:show={showPruneDataDialog} on:confirm={handlePruneDataConfirm} /> <PruneDataDialog bind:show={showPruneDataDialog} on:confirm={handlePruneDataConfirm} />
<form <form
class="flex flex-col h-full justify-between space-y-3 text-sm" class="flex flex-col h-full justify-between space-y-3 text-sm"
@ -101,20 +109,24 @@
on:change={(e) => { on:change={(e) => {
const file = e.target.files[0]; const file = e.target.files[0];
const reader = new FileReader(); const reader = new FileReader();
reader.onload = async (e) => { reader.onload = async (e) => {
const res = await importConfig(localStorage.token, JSON.parse(e.target.result)).catch( const res = await importConfig(localStorage.token, JSON.parse(e.target.result)).catch(
(error) => { (error) => {
toast.error(`${error}`); toast.error(`${error}`);
} }
); );
if (res) { if (res) {
toast.success('Config imported successfully'); toast.success('Config imported successfully');
} }
e.target.value = null; e.target.value = null;
}; };
reader.readAsText(file); reader.readAsText(file);
}} }}
/> />
<button <button
type="button" type="button"
class=" flex rounded-md py-2 px-3 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition" class=" flex rounded-md py-2 px-3 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
@ -141,6 +153,7 @@
{$i18n.t('Import Config from JSON File')} {$i18n.t('Import Config from JSON File')}
</div> </div>
</button> </button>
<button <button
type="button" type="button"
class=" flex rounded-md py-2 px-3 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition" class=" flex rounded-md py-2 px-3 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
@ -171,15 +184,19 @@
{$i18n.t('Export Config to JSON File')} {$i18n.t('Export Config to JSON File')}
</div> </div>
</button> </button>
<hr class="border-gray-100 dark:border-gray-850 my-1" /> <hr class="border-gray-100 dark:border-gray-850 my-1" />
{#if $config?.features.enable_admin_export ?? true} {#if $config?.features.enable_admin_export ?? true}
<div class=" flex w-full justify-between"> <div class=" flex w-full justify-between">
<!-- <div class=" self-center text-xs font-medium">{$i18n.t('Allow Chat Deletion')}</div> --> <!-- <div class=" self-center text-xs font-medium">{$i18n.t('Allow Chat Deletion')}</div> -->
<button <button
class=" flex rounded-md py-1.5 px-3 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition" class=" flex rounded-md py-1.5 px-3 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
type="button" type="button"
on:click={() => { on:click={() => {
// exportAllUserChats(); // exportAllUserChats();
downloadDatabase(localStorage.token).catch((error) => { downloadDatabase(localStorage.token).catch((error) => {
toast.error(`${error}`); toast.error(`${error}`);
}); });
@ -203,6 +220,7 @@
<div class=" self-center text-sm font-medium">{$i18n.t('Download Database')}</div> <div class=" self-center text-sm font-medium">{$i18n.t('Download Database')}</div>
</button> </button>
</div> </div>
<button <button
class=" flex rounded-md py-2 px-3 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition" class=" flex rounded-md py-2 px-3 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
on:click={() => { on:click={() => {
@ -224,10 +242,13 @@
/> />
</svg> </svg>
</div> </div>
<div class=" self-center text-sm font-medium"> <div class=" self-center text-sm font-medium">
{$i18n.t('Export All Chats (All Users)')} {$i18n.t('Export All Chats (All Users)')}
</div> </div>
</button> </button>
<button <button
class=" flex rounded-md py-2 px-3 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition" class=" flex rounded-md py-2 px-3 w-full hover:bg-gray-200 dark:hover:bg-gray-800 transition"
on:click={() => { on:click={() => {