add 历史记录删除API
This commit is contained in:
parent
3cd843d70d
commit
3ae8ded8dd
@ -1,9 +1,11 @@
|
|||||||
|
from pathlib import Path
|
||||||
from typing import List, Any
|
from typing import List, Any
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends
|
from fastapi import APIRouter, Depends
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
from app import schemas
|
from app import schemas
|
||||||
|
from app.chain.transfer import TransferChain
|
||||||
from app.core.security import verify_token
|
from app.core.security import verify_token
|
||||||
from app.db import get_db
|
from app.db import get_db
|
||||||
from app.db.models.downloadhistory import DownloadHistory
|
from app.db.models.downloadhistory import DownloadHistory
|
||||||
@ -12,7 +14,7 @@ from app.db.models.transferhistory import TransferHistory
|
|||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
@router.get("/download", summary="下载历史记录", response_model=List[schemas.DownloadHistory])
|
@router.get("/download", summary="查询下载历史记录", response_model=List[schemas.DownloadHistory])
|
||||||
async def download_history(page: int = 1,
|
async def download_history(page: int = 1,
|
||||||
count: int = 30,
|
count: int = 30,
|
||||||
db: Session = Depends(get_db),
|
db: Session = Depends(get_db),
|
||||||
@ -23,7 +25,18 @@ async def download_history(page: int = 1,
|
|||||||
return DownloadHistory.list_by_page(db, page, count)
|
return DownloadHistory.list_by_page(db, page, count)
|
||||||
|
|
||||||
|
|
||||||
@router.get("/transfer", summary="转移历史记录", response_model=List[schemas.TransferHistory])
|
@router.delete("/download", summary="删除下载历史记录", response_model=schemas.Response)
|
||||||
|
async def delete_download_history(history_in: schemas.DownloadHistory,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
_: schemas.TokenPayload = Depends(verify_token)) -> Any:
|
||||||
|
"""
|
||||||
|
删除下载历史记录
|
||||||
|
"""
|
||||||
|
DownloadHistory.delete(db, history_in.id)
|
||||||
|
return schemas.Response(success=True)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/transfer", summary="查询转移历史记录", response_model=List[schemas.TransferHistory])
|
||||||
async def transfer_history(title: str = None,
|
async def transfer_history(title: str = None,
|
||||||
page: int = 1,
|
page: int = 1,
|
||||||
count: int = 30,
|
count: int = 30,
|
||||||
@ -36,3 +49,23 @@ async def transfer_history(title: str = None,
|
|||||||
return TransferHistory.list_by_title(db, title, page, count)
|
return TransferHistory.list_by_title(db, title, page, count)
|
||||||
else:
|
else:
|
||||||
return TransferHistory.list_by_page(db, page, count)
|
return TransferHistory.list_by_page(db, page, count)
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/transfer", summary="删除转移历史记录", response_model=schemas.Response)
|
||||||
|
async def delete_transfer_history(history_in=schemas.TransferHistory,
|
||||||
|
delete_file: bool = False,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
_: schemas.TokenPayload = Depends(verify_token)) -> Any:
|
||||||
|
"""
|
||||||
|
删除转移历史记录
|
||||||
|
"""
|
||||||
|
# 触发删除事件
|
||||||
|
if delete_file:
|
||||||
|
history = TransferHistory.get(db, history_in.id)
|
||||||
|
if not history:
|
||||||
|
return schemas.Response(success=False, msg="记录不存在")
|
||||||
|
# 册除文件
|
||||||
|
TransferChain().delete_files(Path(history.dest))
|
||||||
|
# 删除记录
|
||||||
|
TransferHistory.delete(db, history_in.id)
|
||||||
|
return schemas.Response(success=True)
|
||||||
|
@ -61,6 +61,19 @@ async def read_site(
|
|||||||
return site
|
return site
|
||||||
|
|
||||||
|
|
||||||
|
@router.delete("/", summary="删除站点", response_model=schemas.Response)
|
||||||
|
async def delete_site(
|
||||||
|
site_in: schemas.Site,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
|
_: schemas.TokenPayload = Depends(verify_token)
|
||||||
|
) -> Any:
|
||||||
|
"""
|
||||||
|
删除站点
|
||||||
|
"""
|
||||||
|
Site.delete(db, site_in.id)
|
||||||
|
return schemas.Response(success=True)
|
||||||
|
|
||||||
|
|
||||||
@router.get("/cookiecloud", summary="CookieCloud同步", response_model=schemas.Response)
|
@router.get("/cookiecloud", summary="CookieCloud同步", response_model=schemas.Response)
|
||||||
async def cookie_cloud_sync(_: schemas.TokenPayload = Depends(verify_token)) -> Any:
|
async def cookie_cloud_sync(_: schemas.TokenPayload = Depends(verify_token)) -> Any:
|
||||||
"""
|
"""
|
||||||
|
@ -68,9 +68,8 @@ async def update_subscribe(
|
|||||||
|
|
||||||
@router.delete("/", summary="删除订阅", response_model=schemas.Response)
|
@router.delete("/", summary="删除订阅", response_model=schemas.Response)
|
||||||
async def delete_subscribe(
|
async def delete_subscribe(
|
||||||
*,
|
|
||||||
db: Session = Depends(get_db),
|
|
||||||
subscribe_in: schemas.Subscribe,
|
subscribe_in: schemas.Subscribe,
|
||||||
|
db: Session = Depends(get_db),
|
||||||
_: schemas.TokenPayload = Depends(verify_token)
|
_: schemas.TokenPayload = Depends(verify_token)
|
||||||
) -> Any:
|
) -> Any:
|
||||||
"""
|
"""
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
import re
|
import re
|
||||||
|
import shutil
|
||||||
|
from pathlib import Path
|
||||||
from typing import List, Optional, Union
|
from typing import List, Optional, Union
|
||||||
|
|
||||||
from app.chain import ChainBase
|
from app.chain import ChainBase
|
||||||
@ -14,6 +16,7 @@ from app.log import logger
|
|||||||
from app.schemas import TransferInfo, TransferTorrent
|
from app.schemas import TransferInfo, TransferTorrent
|
||||||
from app.schemas.types import TorrentStatus, EventType, MediaType, ProgressKey
|
from app.schemas.types import TorrentStatus, EventType, MediaType, ProgressKey
|
||||||
from app.utils.string import StringUtils
|
from app.utils.string import StringUtils
|
||||||
|
from app.utils.system import SystemUtils
|
||||||
|
|
||||||
|
|
||||||
class TransferChain(ChainBase):
|
class TransferChain(ChainBase):
|
||||||
@ -188,3 +191,30 @@ class TransferChain(ChainBase):
|
|||||||
msg_str = f"{msg_str},以下文件处理失败:\n{transferinfo.message}"
|
msg_str = f"{msg_str},以下文件处理失败:\n{transferinfo.message}"
|
||||||
# 发送
|
# 发送
|
||||||
self.post_message(title=msg_title, text=msg_str, image=mediainfo.get_message_image())
|
self.post_message(title=msg_title, text=msg_str, image=mediainfo.get_message_image())
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def delete_files(path: Path):
|
||||||
|
"""
|
||||||
|
删除转移后的文件以及空目录
|
||||||
|
"""
|
||||||
|
logger.info(f"开始删除文件以及空目录:{path} ...")
|
||||||
|
if not path.exists():
|
||||||
|
logger.error(f"{path} 不存在")
|
||||||
|
return
|
||||||
|
elif path.is_file():
|
||||||
|
# 删除文件
|
||||||
|
path.unlink()
|
||||||
|
logger.warn(f"文件 {path} 已删除")
|
||||||
|
# 判断目录是否为空, 为空则删除
|
||||||
|
if str(path.parent.parent) != str(path.root):
|
||||||
|
# 父父目录非根目录,才删除父目录
|
||||||
|
files = SystemUtils.list_files_with_extensions(path.parent, settings.RMT_MEDIAEXT)
|
||||||
|
if not files:
|
||||||
|
shutil.rmtree(path.parent)
|
||||||
|
logger.warn(f"目录 {path.parent} 已删除")
|
||||||
|
else:
|
||||||
|
if str(path.parent) != str(path.root):
|
||||||
|
# 父目录非根目录,才删除目录
|
||||||
|
shutil.rmtree(path)
|
||||||
|
# 删除目录
|
||||||
|
logger.warn(f"目录 {path} 已删除")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user