From 117bd80528880c367cb2f3563829d4cdd09aaa5e Mon Sep 17 00:00:00 2001 From: jxxghp Date: Fri, 20 Sep 2024 12:51:32 +0800 Subject: [PATCH] fix scraping --- app/api/endpoints/history.py | 10 +++++----- app/api/endpoints/transfer.py | 6 +++--- app/chain/transfer.py | 12 ++---------- app/modules/filemanager/storages/local.py | 2 +- app/monitor.py | 5 ++++- 5 files changed, 15 insertions(+), 20 deletions(-) diff --git a/app/api/endpoints/history.py b/app/api/endpoints/history.py index 4756b455..b4bc7376 100644 --- a/app/api/endpoints/history.py +++ b/app/api/endpoints/history.py @@ -5,7 +5,7 @@ from fastapi import APIRouter, Depends from sqlalchemy.orm import Session from app import schemas -from app.chain.transfer import TransferChain +from app.chain.storage import StorageChain from app.core.event import eventmanager from app.core.security import verify_token from app.db import get_db @@ -87,15 +87,15 @@ def delete_transfer_history(history_in: schemas.TransferHistory, # 册除媒体库文件 if deletedest and history.dest_fileitem: dest_fileitem = schemas.FileItem(**json.loads(history.dest_fileitem)) - state, msg = TransferChain().delete_files(dest_fileitem) + state = StorageChain().delete_file(dest_fileitem) if not state: - return schemas.Response(success=False, msg=msg) + return schemas.Response(success=False, msg=f"{dest_fileitem.path}删除失败") # 删除源文件 if deletesrc and history.dest_fileitem: dest_fileitem = schemas.FileItem(**json.loads(history.dest_fileitem)) - state, msg = TransferChain().delete_files(dest_fileitem) + state = StorageChain().delete_file(dest_fileitem) if not state: - return schemas.Response(success=False, msg=msg) + return schemas.Response(success=False, msg=f"{dest_fileitem.path}删除失败") # 发送事件 eventmanager.send_event( EventType.DownloadFileDeleted, diff --git a/app/api/endpoints/transfer.py b/app/api/endpoints/transfer.py index b9a2a721..eac588f9 100644 --- a/app/api/endpoints/transfer.py +++ b/app/api/endpoints/transfer.py @@ -7,6 +7,7 @@ from sqlalchemy.orm import Session from app import schemas from app.chain.media import MediaChain +from app.chain.storage import StorageChain from app.chain.transfer import TransferChain from app.core.metainfo import MetaInfoPath from app.core.security import verify_token, verify_apitoken @@ -89,7 +90,6 @@ def manual_transfer(fileitem: FileItem = None, """ force = False target_path = Path(target_path) if target_path else None - transfer = TransferChain() if logid: # 查询历史记录 history: TransferHistory = TransferHistory.get(db, logid) @@ -107,7 +107,7 @@ def manual_transfer(fileitem: FileItem = None, if history.dest_fileitem: # 删除旧的已整理文件 dest_fileitem = FileItem(**json.loads(history.dest_fileitem)) - transfer.delete_files(dest_fileitem) + StorageChain().delete_file(dest_fileitem) # 从历史数据获取信息 if from_history: @@ -144,7 +144,7 @@ def manual_transfer(fileitem: FileItem = None, offset=episode_offset, ) # 开始转移 - state, errormsg = transfer.manual_transfer( + state, errormsg = TransferChain().manual_transfer( fileitem=src_fileitem, target_storage=target_storage, target_path=target_path, diff --git a/app/chain/transfer.py b/app/chain/transfer.py index 2765c404..1f326b28 100644 --- a/app/chain/transfer.py +++ b/app/chain/transfer.py @@ -428,7 +428,7 @@ class TransferChain(ChainBase): ) # 刮削元数据事件 - if scrape: + if scrape or transferinfo.need_scrape: self.eventmanager.send_event(EventType.MetadataScrape, { 'meta': file_meta, 'mediainfo': file_mediainfo, @@ -599,7 +599,7 @@ class TransferChain(ChainBase): if history.dest_fileitem: # 解析目标文件对象 dest_fileitem = FileItem(**json.loads(history.dest_fileitem)) - self.delete_files(dest_fileitem) + self.storagechain.delete_file(dest_fileitem) # 强制转移 if history.src_fileitem: @@ -713,11 +713,3 @@ class TransferChain(ChainBase): mtype=NotificationType.Organize, title=msg_title, text=msg_str, image=mediainfo.get_message_image(), link=settings.MP_DOMAIN('#/history'))) - - def delete_files(self, fileitem: FileItem) -> Tuple[bool, str]: - """ - TODO 删除转移后的文件以及空目录 - :param fileitem: 文件项 - :return: 成功标识,错误信息 - """ - pass diff --git a/app/modules/filemanager/storages/local.py b/app/modules/filemanager/storages/local.py index 4528c001..d8c87f56 100644 --- a/app/modules/filemanager/storages/local.py +++ b/app/modules/filemanager/storages/local.py @@ -152,7 +152,7 @@ class LocalStorage(StorageBase): return False path_obj = Path(fileitem.path) if not path_obj.exists(): - return False + return True try: if path_obj.is_file(): path_obj.unlink() diff --git a/app/monitor.py b/app/monitor.py index a0b8387f..7bf54064 100644 --- a/app/monitor.py +++ b/app/monitor.py @@ -429,7 +429,10 @@ class Monitor(metaclass=Singleton): 'transferinfo': transferinfo }) - # TODO 移动模式删除空目录 + # 移动模式删除空目录 + if dir_info.transfer_type in ["move"]: + logger.info(f"正在删除: {file_item.storage} {file_item.path}") + self.storagechain.delete_file(file_item) except Exception as e: logger.error("目录监控发生错误:%s - %s" % (str(e), traceback.format_exc()))