refactor(backup): Изменено API скачивания бэкапов

This commit is contained in:
Андрей Дувакин 2025-07-02 11:59:53 +05:00
parent b52c0d16fa
commit 04b9682f1e
2 changed files with 47 additions and 23 deletions

View File

@ -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',
)

View File

@ -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();