feat: show error toast if trying to download db on external db

This commit is contained in:
Jun Siang Cheah 2024-04-27 15:53:31 +01:00
parent e91a49c455
commit 47a33acfeb
4 changed files with 21 additions and 6 deletions

View file

@ -1,5 +1,6 @@
from fastapi import APIRouter, UploadFile, File, Response from fastapi import APIRouter, UploadFile, File, Response
from fastapi import Depends, HTTPException, status from fastapi import Depends, HTTPException, status
from peewee import SqliteDatabase
from starlette.responses import StreamingResponse, FileResponse from starlette.responses import StreamingResponse, FileResponse
from pydantic import BaseModel from pydantic import BaseModel
@ -7,7 +8,7 @@ from pydantic import BaseModel
from fpdf import FPDF from fpdf import FPDF
import markdown import markdown
from apps.web.internal.db import DB
from utils.utils import get_admin_user from utils.utils import get_admin_user
from utils.misc import calculate_sha256, get_gravatar_url 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, status_code=status.HTTP_401_UNAUTHORIZED,
detail=ERROR_MESSAGES.ACCESS_PROHIBITED, 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( return FileResponse(
f"{DATA_DIR}/webui.db", DB.database,
media_type="application/octet-stream", media_type="application/octet-stream",
filename="webui.db", filename="webui.db",
) )

View file

@ -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." 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." 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."

View file

@ -83,9 +83,9 @@ export const downloadDatabase = async (token: string) => {
Authorization: `Bearer ${token}` Authorization: `Bearer ${token}`
} }
}) })
.then((response) => { .then(async (response) => {
if (!response.ok) { if (!response.ok) {
throw new Error('Network response was not ok'); throw await response.json();
} }
return response.blob(); return response.blob();
}) })
@ -100,7 +100,11 @@ export const downloadDatabase = async (token: string) => {
}) })
.catch((err) => { .catch((err) => {
console.log(err); console.log(err);
error = err; error = err.detail;
return null; return null;
}); });
if (error) {
throw error;
}
}; };

View file

@ -2,6 +2,7 @@
import { downloadDatabase } from '$lib/apis/utils'; import { downloadDatabase } from '$lib/apis/utils';
import { onMount, getContext } from 'svelte'; import { onMount, getContext } from 'svelte';
import { config } from '$lib/stores'; import { config } from '$lib/stores';
import { toast } from 'svelte-sonner';
const i18n = getContext('i18n'); const i18n = getContext('i18n');
@ -32,7 +33,9 @@
on:click={() => { on:click={() => {
// exportAllUserChats(); // exportAllUserChats();
downloadDatabase(localStorage.token); downloadDatabase(localStorage.token).catch((error) => {
toast.error(error);
});
}} }}
> >
<div class=" self-center mr-3"> <div class=" self-center mr-3">