refactor(backup): Изменено API скачивания бэкапов
This commit is contained in:
parent
b52c0d16fa
commit
04b9682f1e
@ -26,8 +26,8 @@ async def get_all_backups(
|
|||||||
|
|
||||||
|
|
||||||
@router.get(
|
@router.get(
|
||||||
'/{backup_id}/',
|
'/{backup_id}/file/',
|
||||||
response_model=BackupResponseEntity,
|
response_class=FileResponse,
|
||||||
summary='Get a backup file',
|
summary='Get a backup file',
|
||||||
description='Get a backup file',
|
description='Get a backup file',
|
||||||
)
|
)
|
||||||
|
|||||||
@ -5,8 +5,8 @@ import {
|
|||||||
} from "../../../../../Api/backupsApi.js";
|
} from "../../../../../Api/backupsApi.js";
|
||||||
import {useDispatch} from "react-redux";
|
import {useDispatch} from "react-redux";
|
||||||
import {notification} from "antd";
|
import {notification} from "antd";
|
||||||
import {baseQueryWithAuth} from "../../../../../Api/baseQuery.js";
|
|
||||||
import {useState} from "react";
|
import {useState} from "react";
|
||||||
|
import CONFIG from "../../../../../Core/сonfig.js";
|
||||||
|
|
||||||
|
|
||||||
const useBackupManageTab = () => {
|
const useBackupManageTab = () => {
|
||||||
@ -40,50 +40,74 @@ const useBackupManageTab = () => {
|
|||||||
const downloadBackupHandler = async (backupId, fileName) => {
|
const downloadBackupHandler = async (backupId, fileName) => {
|
||||||
try {
|
try {
|
||||||
setDownloadingFiles(true);
|
setDownloadingFiles(true);
|
||||||
const {url, ...options} = await baseQueryWithAuth(
|
|
||||||
{
|
|
||||||
url: `/backups/${backupId}/`,
|
|
||||||
method: 'GET',
|
|
||||||
credentials: 'include',
|
|
||||||
},
|
|
||||||
{},
|
|
||||||
{}
|
|
||||||
);
|
|
||||||
|
|
||||||
const response = await fetch(url, {
|
const token = localStorage.getItem('access_token');
|
||||||
...options,
|
if (!token) {
|
||||||
|
notification.error({
|
||||||
|
message: "Ошибка",
|
||||||
|
description: "Токен не найден",
|
||||||
|
placement: "topRight",
|
||||||
|
});
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const response = await fetch(`${CONFIG.BASE_URL}backups/${backupId}/file/`, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
credentials: 'include',
|
credentials: 'include',
|
||||||
|
headers: {
|
||||||
|
'Authorization': `Bearer ${token}`,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
if (!response.ok) {
|
||||||
|
const errorText = await response.text();
|
||||||
notification.error({
|
notification.error({
|
||||||
message: "Ошибка при скачивании файла",
|
message: "Ошибка",
|
||||||
description: "Не удалось загрузить файл.",
|
description: errorText || "Не удалось скачать резервную копию",
|
||||||
placement: "topRight",
|
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 blob = await response.blob();
|
||||||
const downloadUrl = window.URL.createObjectURL(blob);
|
const downloadUrl = window.URL.createObjectURL(blob);
|
||||||
const link = document.createElement("a");
|
const link = document.createElement("a");
|
||||||
link.href = downloadUrl;
|
link.href = downloadUrl;
|
||||||
link.setAttribute("download", fileName || "file");
|
link.setAttribute("download", fileName || "backup.tar.gz");
|
||||||
document.body.appendChild(link);
|
document.body.appendChild(link);
|
||||||
link.click();
|
link.click();
|
||||||
link.remove();
|
link.remove();
|
||||||
window.URL.revokeObjectURL(downloadUrl);
|
window.URL.revokeObjectURL(downloadUrl);
|
||||||
setDownloadingFiles(false);
|
notification.success({
|
||||||
} catch (e) {
|
message: "Успех",
|
||||||
console.log(e)
|
description: "Резервная копия успешно скачана",
|
||||||
notification.error({
|
|
||||||
message: "Ошибка",
|
|
||||||
description: e?.data?.detail || "Не удалось загрузить резервную копию",
|
|
||||||
placement: "topRight",
|
placement: "topRight",
|
||||||
});
|
});
|
||||||
|
} catch (e) {
|
||||||
|
console.error("Download error:", e); // Отладка
|
||||||
|
notification.error({
|
||||||
|
message: "Ошибка",
|
||||||
|
description: e.message || "Не удалось скачать резервную копию",
|
||||||
|
placement: "topRight",
|
||||||
|
});
|
||||||
|
} finally {
|
||||||
|
setDownloadingFiles(false);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
const deleteBackupHandler = async (backupId) => {
|
const deleteBackupHandler = async (backupId) => {
|
||||||
try {
|
try {
|
||||||
await deleteBackup(backupId).unwrap();
|
await deleteBackup(backupId).unwrap();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user