diff --git a/backend/apps/web/routers/utils.py b/backend/apps/web/routers/utils.py index 953f5ce5..12805873 100644 --- a/backend/apps/web/routers/utils.py +++ b/backend/apps/web/routers/utils.py @@ -1,5 +1,6 @@ from fastapi import APIRouter, UploadFile, File, Response from fastapi import Depends, HTTPException, status +from peewee import SqliteDatabase from starlette.responses import StreamingResponse, FileResponse from pydantic import BaseModel @@ -7,7 +8,7 @@ from pydantic import BaseModel from fpdf import FPDF import markdown - +from apps.web.internal.db import DB from utils.utils import get_admin_user from utils.misc import calculate_sha256, get_gravatar_url @@ -96,8 +97,13 @@ async def download_db(user=Depends(get_admin_user)): status_code=status.HTTP_401_UNAUTHORIZED, detail=ERROR_MESSAGES.ACCESS_PROHIBITED, ) + if not isinstance(DB, SqliteDatabase): + raise HTTPException( + status_code=status.HTTP_400_BAD_REQUEST, + detail=ERROR_MESSAGES.DB_NOT_SQLITE, + ) return FileResponse( - f"{DATA_DIR}/webui.db", + DB.database, media_type="application/octet-stream", filename="webui.db", ) diff --git a/backend/constants.py b/backend/constants.py index 310c1331..a2694575 100644 --- a/backend/constants.py +++ b/backend/constants.py @@ -69,3 +69,5 @@ class ERROR_MESSAGES(str, Enum): CREATE_API_KEY_ERROR = "Oops! Something went wrong while creating your API key. Please try again later. If the issue persists, contact support for assistance." EMPTY_CONTENT = "The content provided is empty. Please ensure that there is text or data present before proceeding." + + DB_NOT_SQLITE = "This feature is only available when running with SQLite databases." diff --git a/src/lib/apis/utils/index.ts b/src/lib/apis/utils/index.ts index ef6b0d25..3f28d944 100644 --- a/src/lib/apis/utils/index.ts +++ b/src/lib/apis/utils/index.ts @@ -83,9 +83,9 @@ export const downloadDatabase = async (token: string) => { Authorization: `Bearer ${token}` } }) - .then((response) => { + .then(async (response) => { if (!response.ok) { - throw new Error('Network response was not ok'); + throw await response.json(); } return response.blob(); }) @@ -100,7 +100,11 @@ export const downloadDatabase = async (token: string) => { }) .catch((err) => { console.log(err); - error = err; + error = err.detail; return null; }); + + if (error) { + throw error; + } }; diff --git a/src/lib/components/admin/Settings/Database.svelte b/src/lib/components/admin/Settings/Database.svelte index 06a0d595..951282cb 100644 --- a/src/lib/components/admin/Settings/Database.svelte +++ b/src/lib/components/admin/Settings/Database.svelte @@ -2,6 +2,7 @@ import { downloadDatabase } from '$lib/apis/utils'; import { onMount, getContext } from 'svelte'; import { config } from '$lib/stores'; + import { toast } from 'svelte-sonner'; const i18n = getContext('i18n'); @@ -32,7 +33,9 @@ on:click={() => { // exportAllUserChats(); - downloadDatabase(localStorage.token); + downloadDatabase(localStorage.token).catch((error) => { + toast.error(error); + }); }} >