Skip to content

Commit

Permalink
refactor: 删除历史记录时删除空目录(无媒体文件)
Browse files Browse the repository at this point in the history
  • Loading branch information
jxxghp committed Oct 23, 2024
1 parent a6f0792 commit d6f8c36
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion app/api/endpoints/history.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
from pathlib import Path
from typing import List, Any

from fastapi import APIRouter, Depends
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
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()

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit d6f8c36

Please sign in to comment.