import { requestClient } from '#/api/request';
|
|
function downloadBlob(blob: Blob, headers?: any) {
|
let filename = 'backup.sql';
|
if (headers) {
|
const disposition = headers['content-disposition'] || headers['Content-Disposition'];
|
if (disposition && disposition.indexOf('filename=') !== -1) {
|
const matches = disposition.match(/filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/);
|
if (matches != null && matches[1]) {
|
filename = matches[1].replace(/['"]/g, '');
|
}
|
}
|
}
|
|
const url = window.URL.createObjectURL(blob);
|
const link = document.createElement('a');
|
link.style.display = 'none';
|
link.href = url;
|
link.setAttribute('download', decodeURIComponent(filename));
|
document.body.append(link);
|
link.click();
|
link.remove();
|
window.URL.revokeObjectURL(url);
|
}
|
|
/** 下载数据库备份文件 */
|
export function backupDatabase(onDownloadProgress?: (progressEvent: any) => void) {
|
return requestClient.request('/system/database/backup', {
|
method: 'GET',
|
responseType: 'blob',
|
timeout: 300000, // 5分钟
|
onDownloadProgress,
|
responseReturn: 'raw',
|
}).then((res: any) => {
|
const blob = res.data || res;
|
if (blob instanceof Blob) {
|
downloadBlob(blob, res.headers);
|
}
|
return res;
|
}).catch((err: any) => {
|
if (err instanceof Blob) {
|
downloadBlob(err);
|
return err;
|
} else if (err?.response?.data instanceof Blob) {
|
downloadBlob(err.response.data, err.response.headers);
|
return err;
|
}
|
throw err;
|
});
|
}
|