mirror of
https://github.com/jxxghp/MoviePilot.git
synced 2026-04-13 13:29:44 +08:00
64 lines
1.8 KiB
Python
64 lines
1.8 KiB
Python
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
|