From 04b9682f1e380d96b3f257c1bb38cc61b22a4d29 Mon Sep 17 00:00:00 2001 From: andrei Date: Wed, 2 Jul 2025 11:59:53 +0500 Subject: [PATCH] =?UTF-8?q?refactor(backup):=20=D0=98=D0=B7=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=D0=B5=D0=BD=D0=BE=20API=20=D1=81=D0=BA=D0=B0=D1=87=D0=B8?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F=20=D0=B1=D1=8D=D0=BA=D0=B0=D0=BF?= =?UTF-8?q?=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/app/controllers/backup_router.py | 4 +- .../BackupManageTab/useBackupManageTab.js | 66 +++++++++++++------ 2 files changed, 47 insertions(+), 23 deletions(-) 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();