Skip to content

Commit 2f1a448

Browse files
authored
Merge pull request #5226 from stkevintan/path-mapping
2 parents 63a890e + 99cab7c commit 2f1a448

File tree

7 files changed

+42
-15
lines changed

7 files changed

+42
-15
lines changed

app/chain/__init__.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -707,19 +707,17 @@ def download(self, content: Union[Path, str, bytes], download_dir: Path, cookie:
707707
cookie=cookie, episodes=episodes, category=category, label=label,
708708
downloader=downloader)
709709

710-
def download_added(self, context: Context, download_dir: Path, storage: str, torrent_content: Union[str, bytes] = None) -> None:
710+
def download_added(self, context: Context, download_dir: Path, torrent_content: Union[str, bytes] = None) -> None:
711711
"""
712712
添加下载任务成功后,从站点下载字幕,保存到下载目录
713713
:param context: 上下文,包括识别信息、媒体信息、种子信息
714714
:param download_dir: 下载目录
715-
:param storage: 存储类型
716715
:param torrent_content: 种子内容,如果有则直接使用该内容,否则从context中获取种子文件路径
717716
:return: None,该方法可被多个模块同时处理
718717
"""
719718
return self.run_module("download_added", context=context,
720719
torrent_content=torrent_content,
721-
download_dir=download_dir,
722-
storage=storage)
720+
download_dir=download_dir)
723721

724722
def list_torrents(self, status: TorrentStatus = None,
725723
hashs: Union[list, str] = None,

app/chain/download.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from app.helper.directory import DirectoryHelper
2020
from app.helper.torrent import TorrentHelper
2121
from app.log import logger
22-
from app.schemas import ExistMediaInfo, NotExistMediaInfo, DownloadingTorrent, Notification, ResourceSelectionEventData, \
22+
from app.schemas import ExistMediaInfo, FileURI, NotExistMediaInfo, DownloadingTorrent, Notification, ResourceSelectionEventData, \
2323
ResourceDownloadEventData
2424
from app.schemas.types import MediaType, TorrentStatus, EventType, MessageChannel, NotificationType, ContentType, \
2525
ChainEventType
@@ -235,10 +235,7 @@ def download_single(self, context: Context,
235235
storage = 'local'
236236
# 下载目录
237237
if save_path:
238-
uri = schemas.FileURI.from_uri(save_path)
239-
# 下载目录使用自定义的
240-
download_dir = Path(uri.path)
241-
storage = uri.storage
238+
download_dir = Path(save_path)
242239
else:
243240
# 根据媒体信息查询下载目录配置
244241
dir_info = DirectoryHelper().get_dir(_media, include_unsorted=True)
@@ -263,6 +260,8 @@ def download_single(self, context: Context,
263260
self.messagehelper.put(f"{_media.type.value} {_media.title_year} 未找到下载目录!",
264261
title="下载失败", role="system")
265262
return None
263+
fileURI = FileURI(storage=storage, path=download_dir.as_posix())
264+
download_dir = Path(fileURI.uri)
266265

267266
# 添加下载
268267
result: Optional[tuple] = self.download(content=torrent_content,
@@ -362,7 +361,7 @@ def download_single(self, context: Context,
362361
username=username,
363362
)
364363
# 下载成功后处理
365-
self.download_added(context=context, download_dir=download_dir, storage=storage, torrent_content=torrent_content)
364+
self.download_added(context=context, download_dir=download_dir, torrent_content=torrent_content)
366365
# 广播事件
367366
self.eventmanager.send_event(EventType.DownloadAdded, {
368367
"hash": _hash,

app/modules/__init__.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from abc import abstractmethod, ABCMeta
22
from typing import Generic, Tuple, Union, TypeVar, Type, Dict, Optional, Callable
3+
from pathlib import Path
34

45
from app.helper.service import ServiceConfigHelper
56
from app.schemas import Notification, NotificationConf, MediaServerConf, DownloaderConf
@@ -290,6 +291,30 @@ def reset_default_config_name(self):
290291
重置默认配置名称
291292
"""
292293
self._default_config_name = None
294+
295+
def normalize_path(self, path: Path, downloader: Optional[str]) -> str:
296+
"""
297+
根据下载器配置和路径映射,规范化下载路径
298+
299+
:param path: 存储路径
300+
:param downloader: 下载器名称
301+
:return: 规范化后发送给下载器的路径
302+
"""
303+
dir = path.as_posix()
304+
conf = self.get_config(downloader)
305+
if conf and conf.path_mapping:
306+
for (storage_path, download_path) in conf.path_mapping:
307+
storage_path = Path(storage_path.strip()).as_posix()
308+
download_path = Path(download_path.strip()).as_posix()
309+
if dir.startswith(storage_path):
310+
dir = dir.replace(storage_path, download_path, 1)
311+
break
312+
# 去掉存储协议前缀 if any, 下载器无法识别
313+
for s in StorageSchema:
314+
prefix = f"{s.value}:"
315+
if dir.startswith(prefix):
316+
return dir[len(prefix):]
317+
return dir
293318

294319

295320
class _MediaServerBase(ServiceBase[TService, MediaServerConf]):

app/modules/qbittorrent/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ def __get_torrent_info() -> Tuple[Optional[Torrent], Optional[bytes]]:
150150
# 添加任务
151151
state = server.add_torrent(
152152
content=content,
153-
download_dir=download_dir.as_posix(),
153+
download_dir=self.normalize_path(download_dir, downloader),
154154
is_paused=is_paused,
155155
tag=tags,
156156
cookie=cookie,

app/modules/subtitle/__init__.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from app.helper.torrent import TorrentHelper
1212
from app.log import logger
1313
from app.modules import _ModuleBase
14-
from app.schemas.file import FileItem
14+
from app.schemas.file import FileURI
1515
from app.schemas.types import ModuleType, OtherModulesType
1616
from app.utils.http import RequestUtils
1717
from app.utils.string import StringUtils
@@ -65,12 +65,11 @@ def stop(self) -> None:
6565
def test(self):
6666
pass
6767

68-
def download_added(self, context: Context, download_dir: Path, storage: str, torrent_content: Union[str, bytes] = None):
68+
def download_added(self, context: Context, download_dir: Path, torrent_content: Union[str, bytes] = None):
6969
"""
7070
添加下载任务成功后,从站点下载字幕,保存到下载目录
7171
:param context: 上下文,包括识别信息、媒体信息、种子信息
7272
:param download_dir: 下载目录
73-
:param storage: 存储类型
7473
:param torrent_content: 种子内容,如果是种子文件,则为文件内容,否则为种子字符串
7574
:return: None,该方法可被多个模块同时处理
7675
"""
@@ -93,6 +92,10 @@ def download_added(self, context: Context, download_dir: Path, storage: str, tor
9392
storageChain = StorageChain()
9493
# 等待目录存在
9594
working_dir_item = None
95+
# split download_dir into storage and path
96+
fileURI = FileURI.from_uri(download_dir.as_posix())
97+
storage = fileURI.storage
98+
download_dir = Path(fileURI.path)
9699
for _ in range(30):
97100
found = storageChain.get_file_item(storage, download_dir / folder_name)
98101
if found:

app/modules/transmission/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ def __get_torrent_info() -> Tuple[Optional[Torrent], Optional[bytes]]:
151151
# 添加任务
152152
torrent = server.add_torrent(
153153
content=content,
154-
download_dir=download_dir.as_posix(),
154+
download_dir=self.normalize_path(download_dir, downloader),
155155
is_paused=is_paused,
156156
labels=labels,
157157
cookie=cookie

app/schemas/system.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ class DownloaderConf(BaseModel):
5151
config: Optional[dict] = Field(default_factory=dict)
5252
# 是否启用
5353
enabled: Optional[bool] = False
54+
# 路径映射
55+
path_mapping: Optional[list[tuple[str, str]]] = Field(default_factory=list)
5456

5557

5658
class NotificationConf(BaseModel):

0 commit comments

Comments
 (0)