|
6 | 6 |
|
7 | 7 | from app import db |
8 | 8 | from app.models import SourceCreate, SourceUpdate |
9 | | -from app.services import article_svc, github_svc |
| 9 | +from app.services import article_svc, github_svc, netdisk_svc |
10 | 10 | from app.services.match import ARCH_OPTIONS, EXT_OPTIONS, OS_OPTIONS, describe_rule, normalize_rule |
11 | 11 |
|
12 | 12 | router = APIRouter(prefix="/api/sources", tags=["sources"]) |
13 | 13 |
|
14 | 14 |
|
15 | 15 | def _public(source: dict[str, Any]) -> dict[str, Any]: |
16 | 16 | data = {k: v for k, v in source.items() if k not in {"fingerprint", "createdAt", "updatedAt"}} |
17 | | - data["ruleText"] = describe_rule(source.get("filterRule") or {}) |
| 17 | + rule = source.get("filterRule") or {} |
| 18 | + if source.get("type") == "netdisk": |
| 19 | + data["ruleText"] = netdisk_svc.describe_netdisk_rule(rule) |
| 20 | + data["shareCode"] = str(rule.get("code") or "") |
| 21 | + else: |
| 22 | + data["ruleText"] = describe_rule(rule) |
| 23 | + data["shareCode"] = "" |
18 | 24 | if not source.get("enabled"): |
19 | 25 | data["status"] = "off" |
20 | 26 | return data |
@@ -61,6 +67,16 @@ def create(body: SourceCreate) -> dict[str, Any]: |
61 | 67 | name = github_svc.default_name(url) |
62 | 68 | filter_rule = normalize_rule(body.filter_rule.model_dump(by_alias=True) if body.filter_rule else {}) |
63 | 69 | include_prerelease = body.include_prerelease |
| 70 | + elif body.type == "netdisk": |
| 71 | + if not url.startswith("http://") and not url.startswith("https://"): |
| 72 | + raise HTTPException(400, "网盘请填写完整 http(s) 分享链接") |
| 73 | + if not netdisk_svc.is_netdisk_url(url): |
| 74 | + raise HTTPException(400, "暂不支持该网盘,目前支持百度/阿里/夸克/123/天翼/蓝奏") |
| 75 | + if not name: |
| 76 | + name = netdisk_svc.default_name(url) |
| 77 | + code = (body.share_code or "").strip() or netdisk_svc.extract_code(url) |
| 78 | + filter_rule = {"code": code} |
| 79 | + include_prerelease = True |
64 | 80 | else: |
65 | 81 | if not url.startswith("http://") and not url.startswith("https://"): |
66 | 82 | raise HTTPException(400, "文章请填写完整 http(s) 链接") |
@@ -99,12 +115,25 @@ def update(source_id: int, body: SourceUpdate) -> dict[str, Any]: |
99 | 115 | github_svc.parse_repo(url) |
100 | 116 | except ValueError as e: |
101 | 117 | raise HTTPException(400, str(e)) from e |
| 118 | + elif src["type"] == "netdisk": |
| 119 | + if not url.startswith("http://") and not url.startswith("https://"): |
| 120 | + raise HTTPException(400, "网盘请填写完整 http(s) 分享链接") |
| 121 | + if not netdisk_svc.is_netdisk_url(url): |
| 122 | + raise HTTPException(400, "暂不支持该网盘,目前支持百度/阿里/夸克/123/天翼/蓝奏") |
102 | 123 | payload["url"] = url |
103 | 124 | if "enabled" in data: |
104 | 125 | payload["enabled"] = data["enabled"] |
105 | 126 | if "include_prerelease" in data: |
106 | 127 | payload["include_prerelease"] = data["include_prerelease"] |
107 | | - if "filter_rule" in data and data["filter_rule"] is not None: |
| 128 | + if src["type"] == "netdisk" and ("share_code" in data or "url" in payload): |
| 129 | + current_rule = src.get("filterRule") or {} |
| 130 | + code = current_rule.get("code") or "" |
| 131 | + if "share_code" in data and data["share_code"] is not None: |
| 132 | + code = str(data["share_code"] or "").strip() |
| 133 | + elif "url" in payload: |
| 134 | + code = netdisk_svc.extract_code(payload["url"], str(code)) |
| 135 | + payload["filter_rule"] = {"code": code} |
| 136 | + elif "filter_rule" in data and data["filter_rule"] is not None: |
108 | 137 | payload["filter_rule"] = normalize_rule( |
109 | 138 | body.filter_rule.model_dump(by_alias=True) if body.filter_rule else {} |
110 | 139 | ) |
|
0 commit comments