Update Backup API
This commit is contained in:
@ -1,7 +1,9 @@
|
||||
from fastapi import APIRouter, HTTPException
|
||||
from ..schema.config.hysteria import ConfigFile, GetPortResponse, GetSniResponse
|
||||
from ..schema.response import DetailResponse
|
||||
from fastapi.responses import FileResponse
|
||||
# from ..schema.config.hysteria import InstallInputBody
|
||||
import os
|
||||
import cli_api
|
||||
|
||||
router = APIRouter()
|
||||
@ -147,22 +149,37 @@ async def set_sni_api(sni: str):
|
||||
raise HTTPException(status_code=400, detail=f'Error: {str(e)}')
|
||||
|
||||
|
||||
@router.get('/backup', response_model=DetailResponse, summary='Backup Hysteria2 configuration')
|
||||
@router.get('/backup', response_class=FileResponse, summary='Backup Hysteria2 configuration')
|
||||
async def backup():
|
||||
"""
|
||||
Backups the Hysteria2 configuration.
|
||||
|
||||
Returns:
|
||||
A DetailResponse with a message indicating the Hysteria2 configuration backup was successful.
|
||||
|
||||
Raises:
|
||||
HTTPException: if an error occurs while backing up the Hysteria2 configuration.
|
||||
Backups the Hysteria2 configuration and sends the backup ZIP file.
|
||||
"""
|
||||
try:
|
||||
cli_api.backup_hysteria2()
|
||||
return DetailResponse(detail='Hysteria2 configuration backed up successfully.')
|
||||
backup_dir = "/opt/hysbackup/"
|
||||
|
||||
if not os.path.isdir(backup_dir):
|
||||
raise HTTPException(status_code=500, detail="Backup directory does not exist.")
|
||||
|
||||
files = [f for f in os.listdir(backup_dir) if f.endswith('.zip')]
|
||||
files.sort(key=lambda x: os.path.getctime(os.path.join(backup_dir, x)), reverse=True)
|
||||
latest_backup_file = files[0] if files else None
|
||||
|
||||
if latest_backup_file:
|
||||
backup_file_path = os.path.join(backup_dir, latest_backup_file)
|
||||
|
||||
if not backup_file_path.startswith(backup_dir):
|
||||
raise HTTPException(status_code=400, detail="Invalid backup file path.")
|
||||
|
||||
if not os.path.exists(backup_file_path):
|
||||
raise HTTPException(status_code=404, detail="Backup file not found.")
|
||||
|
||||
return FileResponse(path=backup_file_path, filename=os.path.basename(backup_file_path), media_type="application/zip")
|
||||
else:
|
||||
raise HTTPException(status_code=500, detail="No backup file found after backup process.")
|
||||
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=400, detail=f'Error: {str(e)}')
|
||||
raise HTTPException(status_code=500, detail=f'Error: {str(e)}')
|
||||
|
||||
|
||||
@router.get('/enable-obfs', response_model=DetailResponse, summary='Enable Hysteria2 obfs')
|
||||
|
||||
Reference in New Issue
Block a user