Files
MoviePilot/app/actions/transfer_file.py
2025-02-22 09:57:32 +08:00

64 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from app.actions import BaseAction
from app.schemas import ActionParams, ActionContext
from chain.storage import StorageChain
from chain.transfer import TransferChain
from log import logger
class TransferFileParams(ActionParams):
"""
整理文件参数
"""
pass
class TransferFileAction(BaseAction):
"""
整理文件
"""
__fileitems = []
def __init__(self):
super().__init__()
self.transferchain = TransferChain()
self.storagechain = StorageChain()
@property
def name(self) -> str:
return "整理文件"
@property
def description(self) -> str:
return "整理和转移文件"
@property
def success(self) -> bool:
return True if self.__fileitems else False
async def execute(self, params: TransferFileParams, context: ActionContext) -> ActionContext:
"""
从downloads中整理文件记录到fileitems
"""
for download in context.downloads:
if not download.completed:
logger.info(f"下载任务 {download.download_id} 未完成")
continue
fileitem = self.storagechain.get_file_item(storage="local", path=download.path)
if not fileitem:
logger.info(f"文件 {download.path} 不存在")
continue
logger.info(f"开始整理文件 {download.path} ...")
state, errmsg = self.transferchain.do_transfer(fileitem, background=False)
if not state:
logger.error(f"整理文件 {download.path} 失败: {errmsg}")
continue
logger.info(f"整理文件 {download.path} 完成")
self.__fileitems.append(fileitem)
if self.__fileitems:
context.fileitems.extend(self.__fileitems)
self.job_done()
return context