From d6f8c364bf974c3d031be64f625fec9cbdea5256 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Wed, 23 Oct 2024 18:02:31 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E5=88=A0=E9=99=A4=E5=8E=86?= =?UTF-8?q?=E5=8F=B2=E8=AE=B0=E5=BD=95=E6=97=B6=E5=88=A0=E9=99=A4=E7=A9=BA?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=EF=BC=88=E6=97=A0=E5=AA=92=E4=BD=93=E6=96=87?= =?UTF-8?q?=E4=BB=B6=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/endpoints/history.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/app/api/endpoints/history.py b/app/api/endpoints/history.py index 86411b4a..3338c524 100644 --- a/app/api/endpoints/history.py +++ b/app/api/endpoints/history.py @@ -1,3 +1,4 @@ +from pathlib import Path from typing import List, Any from fastapi import APIRouter, Depends @@ -5,6 +6,7 @@ from sqlalchemy.orm import Session from app import schemas from app.chain.storage import StorageChain +from app.core.config import settings from app.core.event import eventmanager from app.core.security import verify_token from app.db import get_db @@ -12,7 +14,7 @@ from app.db.models import User from app.db.models.downloadhistory import DownloadHistory from app.db.models.transferhistory import TransferHistory from app.db.user_oper import get_current_active_superuser -from app.schemas.types import EventType +from app.schemas.types import EventType, MediaType router = APIRouter() @@ -89,6 +91,25 @@ def delete_transfer_history(history_in: schemas.TransferHistory, state = StorageChain().delete_file(dest_fileitem) if not state: return schemas.Response(success=False, msg=f"{dest_fileitem.path}删除失败") + # 上级目录 + if history.type == MediaType.TV.value: + dir_path = Path(dest_fileitem.path).parent.parent + else: + dir_path = Path(dest_fileitem.path).parent + dir_item = StorageChain().get_file_item(storage=dest_fileitem.storage, path=dir_path) + if dir_item: + files = StorageChain().list_files(dir_item, recursion=True) + if files: + # 检查是否还有其他媒体文件 + media_file_exist = False + for file in files: + if file.extension and f".{file.extension.lower()}" in settings.RMT_MEDIAEXT: + media_file_exist = True + break + # 删除空目录 + if not media_file_exist: + StorageChain().delete_file(dir_item) + # 删除源文件 if deletesrc and history.src_fileitem: src_fileitem = schemas.FileItem(**history.src_fileitem)