From 3d2ff28bcdc552249c4792dc2bc82728476f0ff6 Mon Sep 17 00:00:00 2001 From: jxxghp Date: Wed, 20 Aug 2025 23:38:51 +0800 Subject: [PATCH] fix download --- app/helper/torrent.py | 3 -- app/modules/qbittorrent/__init__.py | 41 ++++++++++++++++------------ app/modules/transmission/__init__.py | 39 +++++++++++++++----------- 3 files changed, 47 insertions(+), 36 deletions(-) diff --git a/app/helper/torrent.py b/app/helper/torrent.py index 752333f8..6c3264ee 100644 --- a/app/helper/torrent.py +++ b/app/helper/torrent.py @@ -202,9 +202,6 @@ class TorrentHelper(metaclass=WeakSingleton): if not torrent_content: return "", [] try: - if isinstance(torrent_content, bytes): - # 如果是字节流,则转换为字符串 - torrent_content = torrent_content.decode('utf-8', errors='ignore') # 解析种子内容 torrentinfo = Torrent.from_string(torrent_content) # 获取文件清单 diff --git a/app/modules/qbittorrent/__init__.py b/app/modules/qbittorrent/__init__.py index 2e9b836d..6d5f5f78 100644 --- a/app/modules/qbittorrent/__init__.py +++ b/app/modules/qbittorrent/__init__.py @@ -5,6 +5,7 @@ from qbittorrentapi import TorrentFilesList from torrentool.torrent import Torrent from app import schemas +from app.core.cache import get_file_cache_backend from app.core.config import settings from app.core.metainfo import MetaInfo from app.core.event import eventmanager, Event @@ -107,29 +108,38 @@ class QbittorrentModule(_ModuleBase, _DownloaderBase[Qbittorrent]): :return: 下载器名称、种子Hash、种子文件布局、错误原因 """ - def __get_torrent_info() -> Tuple[str, int]: + def __get_torrent_info() -> Tuple[Optional[Torrent], Optional[bytes]]: """ 获取种子名称 """ + torrent_info, torrent_content = None, None try: if isinstance(content, Path): - torrentinfo = Torrent.from_file(content) - else: - if isinstance(content, bytes): - torrentinfo = Torrent.from_string(content.decode("utf-8", errors='ignore')) + if content.exists(): + torrent_content = content.read_bytes() else: - torrentinfo = Torrent.from_string(content) - return torrentinfo.name, torrentinfo.total_size + # 缓存处理器 + cache_backend = get_file_cache_backend() + # 读取缓存的种子文件 + torrent_content = cache_backend.get(content.as_posix(), region="torrents") + else: + torrent_content = content + + if torrent_content: + torrent_info = Torrent.from_string(torrent_content) + + return torrent_info, torrent_content except Exception as e: logger.error(f"获取种子名称失败:{e}") - return "", 0 + return None, None if not content: return None, None, None, "下载内容为空" - if isinstance(content, Path) and not content.exists(): - logger.error(f"种子文件不存在:{content}") - return None, None, None, f"种子文件不存在:{content}" + # 读取种子的名称 + torrent, content = __get_torrent_info() + if not torrent: + return None, None, None, f"添加种子任务失败:无法读取种子文件" # 获取下载器 server: Qbittorrent = self.get_instance(downloader) @@ -148,7 +158,7 @@ class QbittorrentModule(_ModuleBase, _DownloaderBase[Qbittorrent]): is_paused = True if episodes else False # 添加任务 state = server.add_torrent( - content=content.read_bytes() if isinstance(content, Path) else content, + content=content, download_dir=str(download_dir), is_paused=is_paused, tag=tags, @@ -161,10 +171,6 @@ class QbittorrentModule(_ModuleBase, _DownloaderBase[Qbittorrent]): torrent_layout = server.get_content_layout() if not state: - # 读取种子的名称 - torrent_name, torrent_size = __get_torrent_info() - if not torrent_name: - return None, None, None, f"添加种子任务失败:无法读取种子文件" # 查询所有下载器的种子 torrents, error = server.get_torrents() if error: @@ -173,7 +179,8 @@ class QbittorrentModule(_ModuleBase, _DownloaderBase[Qbittorrent]): try: for torrent in torrents: # 名称与大小相等则认为是同一个种子 - if torrent.get("name") == torrent_name and torrent.get("total_size") == torrent_size: + if torrent.get("name") == torrent.name \ + and torrent.get("total_size") == torrent.total_size: torrent_hash = torrent.get("hash") torrent_tags = [str(tag).strip() for tag in torrent.get("tags").split(',')] logger.warn(f"下载器中已存在该种子任务:{torrent_hash} - {torrent.get('name')}") diff --git a/app/modules/transmission/__init__.py b/app/modules/transmission/__init__.py index c1c23053..3c36e1f7 100644 --- a/app/modules/transmission/__init__.py +++ b/app/modules/transmission/__init__.py @@ -5,6 +5,7 @@ from torrentool.torrent import Torrent from transmission_rpc import File from app import schemas +from app.core.cache import get_file_cache_backend from app.core.config import settings from app.core.metainfo import MetaInfo from app.core.event import eventmanager, Event @@ -108,28 +109,38 @@ class TransmissionModule(_ModuleBase, _DownloaderBase[Transmission]): :return: 下载器名称、种子Hash、种子文件布局、错误原因 """ - def __get_torrent_info() -> Tuple[str, int]: + def __get_torrent_info() -> Tuple[Optional[Torrent], Optional[bytes]]: """ 获取种子名称 """ + torrent_info, torrent_content = None, None try: if isinstance(content, Path): - torrentinfo = Torrent.from_file(content) - else: - if isinstance(content, bytes): - torrentinfo = Torrent.from_string(content.decode("utf-8", errors='ignore')) + if content.exists(): + torrent_content = content.read_bytes() else: - torrentinfo = Torrent.from_string(content) - return torrentinfo.name, torrentinfo.total_size + # 缓存处理器 + cache_backend = get_file_cache_backend() + # 读取缓存的种子文件 + torrent_content = cache_backend.get(content.as_posix(), region="torrents") + else: + torrent_content = content + + if torrent_content: + torrent_info = Torrent.from_string(torrent_content) + + return torrent_info, torrent_content except Exception as e: logger.error(f"获取种子名称失败:{e}") - return "", 0 + return None, None if not content: return None, None, None, "下载内容为空" - if isinstance(content, Path) and not content.exists(): - return None, None, None, f"种子文件不存在:{content}" + # 读取种子的名称 + torrent, content = __get_torrent_info() + if not torrent: + return None, None, None, f"添加种子任务失败:无法读取种子文件" # 获取下载器 server: Transmission = self.get_instance(downloader) @@ -148,7 +159,7 @@ class TransmissionModule(_ModuleBase, _DownloaderBase[Transmission]): labels = None # 添加任务 torrent = server.add_torrent( - content=content.read_bytes() if isinstance(content, Path) else content, + content=content, download_dir=str(download_dir), is_paused=is_paused, labels=labels, @@ -158,10 +169,6 @@ class TransmissionModule(_ModuleBase, _DownloaderBase[Transmission]): torrent_layout = "Original" if not torrent: - # 读取种子的名称 - torrent_name, torrent_size = __get_torrent_info() - if not torrent_name: - return None, None, None, f"添加种子任务失败:无法读取种子文件" # 查询所有下载器的种子 torrents, error = server.get_torrents() if error: @@ -170,7 +177,7 @@ class TransmissionModule(_ModuleBase, _DownloaderBase[Transmission]): try: for torrent in torrents: # 名称与大小相等则认为是同一个种子 - if torrent.name == torrent_name and torrent.total_size == torrent_size: + if torrent.name == torrent.name and torrent.total_size == torrent.total_size: torrent_hash = torrent.hashString logger.warn(f"下载器中已存在该种子任务:{torrent_hash} - {torrent.name}") # 给种子打上标签