Skip to content

Commit bc41829

Browse files
ChesyreAkimioJR
authored andcommitted
Refactor: Move public_url logic from Alist module to Alist2Strm
1 parent 442d241 commit bc41829

3 files changed

Lines changed: 13 additions & 19 deletions

File tree

app/modules/alist/v3/client.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ def __init__(
2121
username: str = "",
2222
password: str = "",
2323
token: str = "",
24-
public_url: str = "",
2524
) -> None:
2625
"""
2726
AlistClient 类初始化
@@ -30,7 +29,6 @@ def __init__(
3029
:param username: Alist 用户名
3130
:param password: Alist 密码
3231
:param token: Alist 永久令牌
33-
:param public_url: 公共访问地址,用于生成 .strm 文件(可选)
3432
"""
3533

3634
if (username == "" or password == "") and token == "":
@@ -47,11 +45,6 @@ def __init__(
4745
if not url.startswith("http"):
4846
url = "https://" + url
4947
self.url = url.rstrip("/")
50-
51-
# 存储公共访问地址(如果提供)
52-
if public_url and not public_url.startswith("http"):
53-
public_url = "https://" + public_url
54-
self.public_url = public_url.rstrip("/") if public_url else None
5548

5649
if token != "":
5750
self.__token["token"] = token
@@ -224,7 +217,6 @@ async def async_api_fs_list(self, dir_path: str) -> list[AlistPath]:
224217
return [
225218
AlistPath(
226219
server_url=self.url,
227-
public_url=self.public_url,
228220
base_path=self.base_path,
229221
full_path=dir_path + "/" + alist_path["name"],
230222
**alist_path,
@@ -263,7 +255,6 @@ async def async_api_fs_get(self, path: str) -> AlistPath:
263255
logger.debug(f"获取路径 {path} 详细信息成功")
264256
return AlistPath(
265257
server_url=self.url,
266-
public_url=self.public_url,
267258
base_path=self.base_path,
268259
full_path=path,
269260
**result["data"],
@@ -422,4 +413,4 @@ async def get_storage_by_mount_path(
422413
await self.async_api_admin_storage_create(storage)
423414
return storage
424415

425-
return None
416+
return None

app/modules/alist/v3/path.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ class AlistPath(BaseModel):
1313
"""
1414

1515
server_url: str # 服务器地址
16-
public_url: str | None = None # 公共访问地址,用于生成 .strm 文件(可选,如果与 server_url 不同)
1716
base_path: str # 用户基础路径(用于计算文件/目录在 Alist 服务器上的绝对地址)
1817
full_path: str # 相对用户根文件/目录路径
1918

@@ -49,13 +48,10 @@ def download_url(self) -> str:
4948
"""
5049
文件下载地址
5150
"""
52-
# 如果定义了 public_url 则使用它,否则使用 server_url
53-
base_url = self.public_url if self.public_url else self.server_url
54-
5551
if self.sign:
56-
url = base_url + "/d" + self.abs_path + "?sign=" + self.sign
52+
url = self.server_url + "/d" + self.abs_path + "?sign=" + self.sign
5753
else:
58-
url = base_url + "/d" + self.abs_path
54+
url = self.server_url + "/d" + self.abs_path
5955

6056
return URLUtils.encode(url)
6157

@@ -95,4 +91,4 @@ def created_timestamp(self) -> float:
9591
"""
9692
获得创建时间的时间戳
9793
"""
98-
return self.__parse_timestamp(self.created)
94+
return self.__parse_timestamp(self.created)

app/modules/alist2strm/alist2strm.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ def __init__(
4343
4444
:param id: 任务标识 ID,默认为空
4545
:param url: Alist 服务器地址,默认为 "http://localhost:5244"
46+
:param public_url: 公共访问地址,用于 AlistURL 模式生成 .strm 文件(可选)
4647
:param username: Alist 用户名,默认为空
4748
:param password: Alist 密码,默认为空
48-
:param public_url: 公共访问地址,用于生成 .strm 文件(可选)
4949
:param source_dir: 需要同步的 Alist 的目录,默认为 "/"
5050
:param target_dir: strm 文件输出目录,默认为当前工作目录
5151
:param flatten_mode: 平铺模式,将所有 Strm 文件保存至同一级目录,默认为 False
@@ -63,8 +63,12 @@ def __init__(
6363
:param smart_protection: 智能保护配置 {enabled: bool, threshold: int, grace_scans: int}
6464
"""
6565

66-
self.client = AlistClient(url, username, password, token, public_url)
66+
self.client = AlistClient(url, username, password, token)
6767
self.mode = Alist2StrmMode.from_str(mode)
68+
69+
if public_url and not public_url.startswith("http"):
70+
public_url = "https://" + public_url
71+
self.public_url = public_url.rstrip("/") if public_url else None
6872

6973
self.source_dir = source_dir
7074
self.target_dir = Path(target_dir)
@@ -247,6 +251,9 @@ async def __file_processer(self, path: AlistPath) -> None:
247251
# 统一的 URL 生成逻辑,BDMV 文件与普通文件使用相同的逻辑
248252
if self.mode == Alist2StrmMode.AlistURL:
249253
content = path.download_url
254+
# 如果定义了 public_url,则替换服务器 URL 为公共访问 URL
255+
if self.public_url:
256+
content = content.replace(self.client.url, self.public_url)
250257
elif self.mode == Alist2StrmMode.RawURL:
251258
content = path.raw_url
252259
elif self.mode == Alist2StrmMode.AlistPath:

0 commit comments

Comments
 (0)