This commit is contained in:
jxxghp
2024-10-24 14:28:30 +08:00
parent 42b5dd4178
commit 1a8d9044d7
4 changed files with 54 additions and 25 deletions

View File

@@ -1,4 +1,3 @@
from pathlib import Path
from typing import List, Any from typing import List, Any
from fastapi import APIRouter, Depends from fastapi import APIRouter, Depends
@@ -6,7 +5,6 @@ from sqlalchemy.orm import Session
from app import schemas from app import schemas
from app.chain.storage import StorageChain from app.chain.storage import StorageChain
from app.core.config import settings
from app.core.event import eventmanager from app.core.event import eventmanager
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
@@ -88,34 +86,16 @@ def delete_transfer_history(history_in: schemas.TransferHistory,
# 册除媒体库文件 # 册除媒体库文件
if deletedest and history.dest_fileitem: if deletedest and history.dest_fileitem:
dest_fileitem = schemas.FileItem(**history.dest_fileitem) dest_fileitem = schemas.FileItem(**history.dest_fileitem)
state = StorageChain().delete_file(dest_fileitem) state = StorageChain().delete_media_file(fileitem=dest_fileitem, mtype=MediaType(history.type))
if not state: if not state:
return schemas.Response(success=False, msg=f"{dest_fileitem.path}删除失败") 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: if deletesrc and history.src_fileitem:
src_fileitem = schemas.FileItem(**history.src_fileitem) src_fileitem = schemas.FileItem(**history.src_fileitem)
state = StorageChain().delete_file(src_fileitem) state = StorageChain().delete_media_file(src_fileitem)
if not state: if not state:
return schemas.Response(success=False, msg=f"{src_fileitem.path}删除失败") return schemas.Response(success=False, msg=f"{src_fileitem.path} 删除失败")
# 发送事件 # 发送事件
eventmanager.send_event( eventmanager.send_event(
EventType.DownloadFileDeleted, EventType.DownloadFileDeleted,

View File

@@ -96,7 +96,9 @@ def manual_transfer(transer_item: ManualTransferItem,
if history.dest_fileitem: if history.dest_fileitem:
# 删除旧的已整理文件 # 删除旧的已整理文件
dest_fileitem = FileItem(**history.dest_fileitem) dest_fileitem = FileItem(**history.dest_fileitem)
StorageChain().delete_file(dest_fileitem) state = StorageChain().delete_media_file(dest_fileitem, mtype=MediaType(history.type))
if not state:
return schemas.Response(success=False, msg=f"{dest_fileitem.path} 删除失败")
# 从历史数据获取信息 # 从历史数据获取信息
if transer_item.from_history: if transer_item.from_history:

View File

@@ -3,6 +3,9 @@ from typing import Optional, Tuple, List, Dict
from app import schemas from app import schemas
from app.chain import ChainBase from app.chain import ChainBase
from app.core.config import settings
from app.log import logger
from app.schemas import MediaType
class StorageChain(ChainBase): class StorageChain(ChainBase):
@@ -74,6 +77,12 @@ class StorageChain(ChainBase):
""" """
return self.run_module("get_file_item", storage=storage, path=path) return self.run_module("get_file_item", storage=storage, path=path)
def get_parent_item(self, fileitem: schemas.FileItem) -> Optional[schemas.FileItem]:
"""
获取上级目录项
"""
return self.run_module("get_parent_item", fileitem=fileitem)
def snapshot_storage(self, storage: str, path: Path) -> Optional[Dict[str, float]]: def snapshot_storage(self, storage: str, path: Path) -> Optional[Dict[str, float]]:
""" """
快照存储 快照存储
@@ -91,3 +100,31 @@ class StorageChain(ChainBase):
获取支持的整理方式 获取支持的整理方式
""" """
return self.run_module("support_transtype", storage=storage) return self.run_module("support_transtype", storage=storage)
def delete_media_file(self, fileitem: schemas.FileItem, mtype: MediaType = None) -> bool:
"""
删除媒体文件,以及不含媒体文件的目录
"""
state = self.delete_file(fileitem)
if not state:
logger.warn(f"{fileitem.storage}{fileitem.path} 删除失败")
return False
# 上级目录
if mtype and mtype == MediaType.TV:
dir_path = Path(fileitem.path).parent.parent
dir_item = self.get_file_item(storage=fileitem.storage, path=dir_path)
else:
dir_item = self.get_parent_item(fileitem)
if dir_item:
files = self.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:
self.delete_file(dir_item)
return False

View File

@@ -260,6 +260,16 @@ class FileManagerModule(_ModuleBase):
return None return None
return storage_oper.get_item(path) return storage_oper.get_item(path)
def get_parent_item(self, fileitem: FileItem) -> Optional[FileItem]:
"""
获取上级目录项
"""
storage_oper = self.__get_storage_oper(fileitem.storage)
if not storage_oper:
logger.error(f"不支持 {fileitem.storage} 的文件获取")
return None
return storage_oper.get_parent(fileitem)
def snapshot_storage(self, storage: str, path: Path) -> Optional[Dict[str, float]]: def snapshot_storage(self, storage: str, path: Path) -> Optional[Dict[str, float]]:
""" """
快照存储 快照存储