diff --git a/api/app/controllers/backup_router.py b/api/app/controllers/backup_router.py index a5a4cf7..96fffcf 100644 --- a/api/app/controllers/backup_router.py +++ b/api/app/controllers/backup_router.py @@ -26,8 +26,8 @@ async def get_all_backups( @router.get( - '/{backup_id}/', - response_model=BackupResponseEntity, + '/{backup_id}/file/', + response_class=FileResponse, summary='Get a backup file', description='Get a backup file', ) diff --git a/web-app/src/Components/Pages/AdminPage/Components/BackupManageTab/useBackupManageTab.js b/web-app/src/Components/Pages/AdminPage/Components/BackupManageTab/useBackupManageTab.js index a580147..dcbd356 100644 --- a/web-app/src/Components/Pages/AdminPage/Components/BackupManageTab/useBackupManageTab.js +++ b/web-app/src/Components/Pages/AdminPage/Components/BackupManageTab/useBackupManageTab.js @@ -5,8 +5,8 @@ import { } from "../../../../../Api/backupsApi.js"; import {useDispatch} from "react-redux"; import {notification} from "antd"; -import {baseQueryWithAuth} from "../../../../../Api/baseQuery.js"; import {useState} from "react"; +import CONFIG from "../../../../../Core/сonfig.js"; const useBackupManageTab = () => { @@ -40,50 +40,74 @@ const useBackupManageTab = () => { const downloadBackupHandler = async (backupId, fileName) => { try { setDownloadingFiles(true); - const {url, ...options} = await baseQueryWithAuth( - { - url: `/backups/${backupId}/`, - method: 'GET', - credentials: 'include', - }, - {}, - {} - ); - const response = await fetch(url, { - ...options, + const token = localStorage.getItem('access_token'); + if (!token) { + notification.error({ + message: "Ошибка", + description: "Токен не найден", + placement: "topRight", + }); + return; + } + + const response = await fetch(`${CONFIG.BASE_URL}backups/${backupId}/file/`, { method: 'GET', credentials: 'include', + headers: { + 'Authorization': `Bearer ${token}`, + }, }); if (!response.ok) { + const errorText = await response.text(); notification.error({ - message: "Ошибка при скачивании файла", - description: "Не удалось загрузить файл.", + message: "Ошибка", + description: errorText || "Не удалось скачать резервную копию", placement: "topRight", }); + return; + } + + const contentType = response.headers.get('content-type'); + + if (!contentType || (!contentType.includes('application/gzip') && !contentType.includes('application/zip'))) { + const errorText = await response.text(); + notification.error({ + message: "Ошибка", + description: errorText || "Не удалось скачать резервную копию", + placement: "topRight", + }); + return; } const blob = await response.blob(); const downloadUrl = window.URL.createObjectURL(blob); const link = document.createElement("a"); link.href = downloadUrl; - link.setAttribute("download", fileName || "file"); + link.setAttribute("download", fileName || "backup.tar.gz"); document.body.appendChild(link); link.click(); link.remove(); window.URL.revokeObjectURL(downloadUrl); - setDownloadingFiles(false); - } catch (e) { - console.log(e) - notification.error({ - message: "Ошибка", - description: e?.data?.detail || "Не удалось загрузить резервную копию", + notification.success({ + message: "Успех", + description: "Резервная копия успешно скачана", placement: "topRight", }); + } catch (e) { + console.error("Download error:", e); // Отладка + notification.error({ + message: "Ошибка", + description: e.message || "Не удалось скачать резервную копию", + placement: "topRight", + }); + } finally { + setDownloadingFiles(false); } }; + const deleteBackupHandler = async (backupId) => { try { await deleteBackup(backupId).unwrap();