From 47a33acfeb8033d9c419c3d46f2c82b951bb7963 Mon Sep 17 00:00:00 2001 From: Jun Siang Cheah Date: Sat, 27 Apr 2024 15:53:31 +0100 Subject: [PATCH] feat: show error toast if trying to download db on external db --- backend/apps/web/routers/utils.py | 10 ++++++++-- backend/constants.py | 2 ++ src/lib/apis/utils/index.ts | 10 +++++++--- src/lib/components/admin/Settings/Database.svelte | 5 ++++- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/backend/apps/web/routers/utils.py b/backend/apps/web/routers/utils.py index 953f5ce5c..12805873d 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 310c13311..a26945756 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 ef6b0d25e..3f28d9444 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 06a0d595c..951282cb8 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); + }); }} >