fix 文件转移支持部分成功
This commit is contained in:
parent
4936ae4631
commit
efac3aa24d
@ -10,9 +10,8 @@ from app.db.downloadhistory_oper import DownloadHistoryOper
|
||||
from app.db.models.downloadhistory import DownloadHistory
|
||||
from app.log import logger
|
||||
from app.schemas.context import TransferInfo, TransferTorrent
|
||||
from app.utils.string import StringUtils
|
||||
from app.utils.system import SystemUtils
|
||||
from app.schemas.types import TorrentStatus, EventType, MediaType
|
||||
from app.utils.string import StringUtils
|
||||
|
||||
|
||||
class TransferChain(ChainBase):
|
||||
@ -102,6 +101,7 @@ class TransferChain(ChainBase):
|
||||
# 转移
|
||||
transferinfo: TransferInfo = self.transfer(mediainfo=mediainfo, path=Path(torrent.path))
|
||||
if not transferinfo or not transferinfo.target_path:
|
||||
# 转移失败
|
||||
logger.warn(f"{torrent.title} 入库失败")
|
||||
self.post_message(
|
||||
title=f"{mediainfo.title_year}{meta.season_episode} 入库失败!",
|
||||
@ -132,12 +132,6 @@ class TransferChain(ChainBase):
|
||||
"""
|
||||
发送入库成功的消息
|
||||
"""
|
||||
# 文件大小
|
||||
file_size = StringUtils.str_filesize(
|
||||
SystemUtils.get_directory_size(
|
||||
transferinfo.target_path
|
||||
)
|
||||
)
|
||||
msg_title = f"{mediainfo.title_year} 已入库"
|
||||
if mediainfo.vote_average:
|
||||
msg_str = f"评分:{mediainfo.vote_average},类型:{mediainfo.type.value}"
|
||||
@ -147,6 +141,9 @@ class TransferChain(ChainBase):
|
||||
msg_str = f"{msg_str},类别:{mediainfo.category}"
|
||||
if meta.resource_term:
|
||||
msg_str = f"{msg_str},质量:{meta.resource_term}"
|
||||
msg_str = f"{msg_str}, 大小:{file_size}"
|
||||
msg_str = f"{msg_str},共{transferinfo.file_count}个文件," \
|
||||
f"大小:{StringUtils.str_filesize(transferinfo.total_size)}"
|
||||
if transferinfo.message:
|
||||
msg_str = f"{msg_str},以下文件处理失败:\n{transferinfo.message}"
|
||||
# 发送
|
||||
self.post_message(title=msg_title, text=msg_str, image=mediainfo.get_message_image())
|
||||
|
@ -39,14 +39,23 @@ class FileTransferModule(_ModuleBase):
|
||||
if not settings.LIBRARY_PATH:
|
||||
logger.error("未设置媒体库目录,无法转移文件")
|
||||
return None
|
||||
target_path, msg = self.transfer_media(in_path=path,
|
||||
result = self.transfer_media(in_path=path,
|
||||
meidainfo=mediainfo,
|
||||
rmt_mode=settings.TRANSFER_TYPE,
|
||||
target_dir=Path(settings.LIBRARY_PATH))
|
||||
if not path:
|
||||
logger.error(msg)
|
||||
|
||||
return TransferInfo(path=path, target_path=target_path, message=msg)
|
||||
if not result:
|
||||
return TransferInfo()
|
||||
if isinstance(result, str):
|
||||
return TransferInfo(message=result)
|
||||
# 解包结果
|
||||
target_path, file_count, file_size, fail_list, msg = result
|
||||
# 返回
|
||||
return TransferInfo(path=path,
|
||||
target_path=target_path,
|
||||
message=msg,
|
||||
file_count=file_count,
|
||||
file_size=file_size,
|
||||
fail_list=fail_list)
|
||||
|
||||
@staticmethod
|
||||
def __transfer_command(file_item: Path, target_file: Path, rmt_mode) -> int:
|
||||
@ -321,21 +330,21 @@ class FileTransferModule(_ModuleBase):
|
||||
meidainfo: MediaInfo,
|
||||
rmt_mode: str = None,
|
||||
target_dir: Path = None
|
||||
) -> Tuple[Optional[Path], str]:
|
||||
) -> Union[str, Tuple[Path, int, int, List[Path], str]]:
|
||||
"""
|
||||
识别并转移一个文件、多个文件或者目录
|
||||
:param in_path: 转移的路径,可能是一个文件也可以是一个目录
|
||||
:param target_dir: 目的文件夹,非空的转移到该文件夹,为空时则按类型转移到配置文件中的媒体库文件夹
|
||||
:param rmt_mode: 文件转移方式
|
||||
:param meidainfo: 媒体信息
|
||||
:return: 处理状态,错误信息
|
||||
:return: 目的路径、处理文件数、总大小、失败文件列表、错误信息
|
||||
"""
|
||||
# 检查目录路径
|
||||
if not in_path.exists():
|
||||
return None, f"{in_path} 路径不存在"
|
||||
return f"{in_path} 路径不存在"
|
||||
|
||||
if not target_dir.exists():
|
||||
return None, f"{target_dir} 目标路径不存在"
|
||||
return f"{target_dir} 目标路径不存在"
|
||||
|
||||
# 目的目录加上类型和二级分类
|
||||
target_dir = target_dir / meidainfo.type.value / meidainfo.category
|
||||
@ -344,6 +353,18 @@ class FileTransferModule(_ModuleBase):
|
||||
rename_format = settings.TV_RENAME_FORMAT \
|
||||
if meidainfo.type == MediaType.TV else settings.MOVIE_RENAME_FORMAT
|
||||
|
||||
# 总大小
|
||||
total_filesize = 0
|
||||
|
||||
# 处理文件数
|
||||
total_num = 0
|
||||
|
||||
# 失败文件清单
|
||||
fail_list = []
|
||||
|
||||
# 错误信息
|
||||
err_msgs = []
|
||||
|
||||
# 判断是否为蓝光原盘
|
||||
bluray_flag = self.__is_bluray_dir(in_path)
|
||||
if bluray_flag:
|
||||
@ -361,15 +382,19 @@ class FileTransferModule(_ModuleBase):
|
||||
new_path=new_path,
|
||||
rmt_mode=rmt_mode)
|
||||
if retcode != 0:
|
||||
return None, f"{retcode},蓝光原盘转移失败"
|
||||
return f"{retcode},蓝光原盘转移失败"
|
||||
else:
|
||||
# 计算文件数
|
||||
total_filesize += 1
|
||||
# 计算大小
|
||||
total_filesize += in_path.stat().st_size
|
||||
# 返回转移后的路径
|
||||
return new_path, ""
|
||||
return new_path, total_filesize, total_filesize, [], ""
|
||||
else:
|
||||
# 获取文件清单
|
||||
transfer_files: List[Path] = SystemUtils.list_files_with_extensions(in_path, settings.RMT_MEDIAEXT)
|
||||
if len(transfer_files) == 0:
|
||||
return None, f"{in_path} 目录下没有找到可转移的文件"
|
||||
return f"{in_path} 目录下没有找到可转移的文件"
|
||||
# 识别目录名称,不包括后缀
|
||||
meta = MetaInfo(in_path.stem)
|
||||
# 目的路径
|
||||
@ -422,11 +447,25 @@ class FileTransferModule(_ModuleBase):
|
||||
rmt_mode=rmt_mode,
|
||||
over_flag=overflag)
|
||||
if retcode != 0:
|
||||
return None, f"{retcode},文件转移失败"
|
||||
logger.error(f"{transfer_file} 转移文件失败,错误码:{retcode}")
|
||||
err_msgs.append(f"{transfer_file.name}:错误码 {retcode}")
|
||||
fail_list.append(transfer_file)
|
||||
continue
|
||||
# 计算文件数
|
||||
total_filesize += 1
|
||||
# 计算大小
|
||||
total_filesize += transfer_file.stat().st_size
|
||||
except Exception as err:
|
||||
return None, f"{err}"
|
||||
err_msgs.append(f"{transfer_file.name}:{err}")
|
||||
logger.error(f"{transfer_file}转移失败:{err}")
|
||||
fail_list.append(transfer_file)
|
||||
|
||||
return new_path, ""
|
||||
if total_num == 0:
|
||||
# 没有成功的
|
||||
return "\n".join(err_msgs)
|
||||
|
||||
# 新路径、处理文件数、总大小、失败文件列表、错误信息
|
||||
return new_path, total_num, total_filesize, fail_list, "\n".join(err_msgs)
|
||||
|
||||
@staticmethod
|
||||
def __get_naming_dict(meta: MetaBase, mediainfo: MediaInfo, file_ext: str = None) -> dict:
|
||||
|
@ -110,6 +110,12 @@ class TransferInfo(BaseModel):
|
||||
path: Optional[Path] = None
|
||||
# 转移后路径
|
||||
target_path: Optional[Path] = None
|
||||
# 处理文件数
|
||||
file_count: int = 0
|
||||
# 总文件大小
|
||||
total_size: float = 0
|
||||
# 失败清单
|
||||
fail_list: list = []
|
||||
# 错误信息
|
||||
message: Optional[str] = None
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user