artdam-cli 0.3.2__tar.gz → 0.3.4__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {artdam_cli-0.3.2 → artdam_cli-0.3.4}/PKG-INFO +17 -7
- {artdam_cli-0.3.2 → artdam_cli-0.3.4}/README.md +14 -6
- artdam_cli-0.3.4/artdam_cli/client.py +172 -0
- {artdam_cli-0.3.2 → artdam_cli-0.3.4}/artdam_cli/commands/comment.py +9 -2
- artdam_cli-0.3.4/artdam_cli/commands/delete_cmd.py +16 -0
- {artdam_cli-0.3.2 → artdam_cli-0.3.4}/artdam_cli/commands/download.py +9 -2
- {artdam_cli-0.3.2 → artdam_cli-0.3.4}/artdam_cli/commands/get.py +11 -3
- {artdam_cli-0.3.2 → artdam_cli-0.3.4}/artdam_cli/commands/login.py +6 -2
- {artdam_cli-0.3.2 → artdam_cli-0.3.4}/artdam_cli/commands/project.py +6 -3
- {artdam_cli-0.3.2 → artdam_cli-0.3.4}/artdam_cli/commands/projects.py +4 -4
- artdam_cli-0.3.4/artdam_cli/commands/search.py +119 -0
- artdam_cli-0.3.4/artdam_cli/commands/tags.py +68 -0
- artdam_cli-0.3.4/artdam_cli/commands/upload.py +25 -0
- {artdam_cli-0.3.2 → artdam_cli-0.3.4}/artdam_cli/main.py +84 -84
- {artdam_cli-0.3.2 → artdam_cli-0.3.4}/artdam_cli/update_check.py +71 -71
- artdam_cli-0.3.4/artdam_cli/uploader.py +110 -0
- {artdam_cli-0.3.2 → artdam_cli-0.3.4}/artdam_cli.egg-info/PKG-INFO +17 -7
- {artdam_cli-0.3.2 → artdam_cli-0.3.4}/artdam_cli.egg-info/SOURCES.txt +4 -1
- {artdam_cli-0.3.2 → artdam_cli-0.3.4}/artdam_cli.egg-info/requires.txt +3 -0
- {artdam_cli-0.3.2 → artdam_cli-0.3.4}/pyproject.toml +4 -1
- artdam_cli-0.3.4/tests/test_search_pagination.py +78 -0
- artdam_cli-0.3.4/tests/test_uploader.py +98 -0
- artdam_cli-0.3.2/artdam_cli/client.py +0 -81
- artdam_cli-0.3.2/artdam_cli/commands/delete_cmd.py +0 -11
- artdam_cli-0.3.2/artdam_cli/commands/search.py +0 -62
- artdam_cli-0.3.2/artdam_cli/commands/tags.py +0 -47
- artdam_cli-0.3.2/artdam_cli/commands/upload.py +0 -22
- {artdam_cli-0.3.2 → artdam_cli-0.3.4}/artdam_cli/__init__.py +0 -0
- {artdam_cli-0.3.2 → artdam_cli-0.3.4}/artdam_cli/commands/__init__.py +0 -0
- {artdam_cli-0.3.2 → artdam_cli-0.3.4}/artdam_cli/commands/favorite.py +0 -0
- {artdam_cli-0.3.2 → artdam_cli-0.3.4}/artdam_cli/commands/folder_cmd.py +0 -0
- {artdam_cli-0.3.2 → artdam_cli-0.3.4}/artdam_cli/commands/folders.py +0 -0
- {artdam_cli-0.3.2 → artdam_cli-0.3.4}/artdam_cli/commands/rate.py +0 -0
- {artdam_cli-0.3.2 → artdam_cli-0.3.4}/artdam_cli/commands/restore.py +0 -0
- {artdam_cli-0.3.2 → artdam_cli-0.3.4}/artdam_cli/commands/tag_cmd.py +0 -0
- {artdam_cli-0.3.2 → artdam_cli-0.3.4}/artdam_cli/commands/update.py +0 -0
- {artdam_cli-0.3.2 → artdam_cli-0.3.4}/artdam_cli/config.py +0 -0
- {artdam_cli-0.3.2 → artdam_cli-0.3.4}/artdam_cli.egg-info/dependency_links.txt +0 -0
- {artdam_cli-0.3.2 → artdam_cli-0.3.4}/artdam_cli.egg-info/entry_points.txt +0 -0
- {artdam_cli-0.3.2 → artdam_cli-0.3.4}/artdam_cli.egg-info/top_level.txt +0 -0
- {artdam_cli-0.3.2 → artdam_cli-0.3.4}/setup.cfg +0 -0
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: artdam-cli
|
|
3
|
-
Version: 0.3.
|
|
3
|
+
Version: 0.3.4
|
|
4
4
|
Summary: ArtDAM 数字资产管理平台命令行工具
|
|
5
5
|
Requires-Python: >=3.10
|
|
6
6
|
Description-Content-Type: text/markdown
|
|
7
7
|
Requires-Dist: click>=8.1
|
|
8
8
|
Requires-Dist: httpx>=0.27
|
|
9
9
|
Requires-Dist: rich>=13.0
|
|
10
|
+
Provides-Extra: dev
|
|
11
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
10
12
|
|
|
11
13
|
# artdam-cli
|
|
12
14
|
|
|
@@ -54,22 +56,27 @@ artdam login
|
|
|
54
56
|
|
|
55
57
|
#### `artdam search`
|
|
56
58
|
|
|
57
|
-
|
|
59
|
+
搜索资产,返回匹配列表。默认只取首页(`--limit` 条);加 `--all` 自动翻页取全部结果
|
|
60
|
+
(Agent 场景推荐,避免结果被截断)。`--all` 最多返回 1000 条,超出会提示用更精确的关键词或
|
|
61
|
+
`--ext` / `--folder` 收窄,避免在几万条资产上卡死。
|
|
58
62
|
|
|
59
63
|
```bash
|
|
60
64
|
artdam search "头盔" --project 1
|
|
61
65
|
artdam search "头盔" --project 1 --limit 50
|
|
62
|
-
artdam search "头盔" --project 1 --
|
|
66
|
+
artdam search "头盔" --project 1 --all # 自动翻页,取全部匹配
|
|
67
|
+
artdam search "头盔" --project 1 --all --json # 全量 JSON 输出(AI Agent 用)
|
|
68
|
+
artdam search "头盔" --project 1 --ext jpg # 按扩展名过滤
|
|
69
|
+
artdam search "头盔" --project 1 --ext jpg,png # 多个扩展名
|
|
63
70
|
artdam search "头盔" --project 1 --folder 10
|
|
64
|
-
artdam search "头盔" --project 1 --json # 输出原始 JSON(AI Agent 用)
|
|
65
71
|
```
|
|
66
72
|
|
|
67
73
|
| 参数 | 说明 |
|
|
68
74
|
|---|---|
|
|
69
75
|
| `KEYWORD` | 搜索关键词 |
|
|
70
76
|
| `-p, --project` | 项目 ID(必填) |
|
|
71
|
-
| `-n, --limit` |
|
|
72
|
-
| `--
|
|
77
|
+
| `-n, --limit` | 返回数量上限,默认 20(不与 `--all` 共用) |
|
|
78
|
+
| `--all` | 自动翻页取全部匹配结果(最多 1000 条) |
|
|
79
|
+
| `-e, --ext` | 按文件扩展名过滤,逗号分隔,如 `jpg,png` |
|
|
73
80
|
| `--folder` | 限定文件夹 ID |
|
|
74
81
|
| `--json` | 输出原始 JSON |
|
|
75
82
|
|
|
@@ -114,11 +121,14 @@ artdam folders --project 1 --json
|
|
|
114
121
|
|
|
115
122
|
#### `artdam upload`
|
|
116
123
|
|
|
117
|
-
|
|
124
|
+
上传文件到指定项目和文件夹。采用分块上传,支持大文件(最大 10GB),带进度条;
|
|
125
|
+
上传前按内容计算 SHA-256,库中已有相同文件则**秒传**(直接复用,不重复传输)。
|
|
126
|
+
单个分块遇到网络抖动(超时 / 5xx)会自动重试,中断时清理服务端会话。
|
|
118
127
|
|
|
119
128
|
```bash
|
|
120
129
|
artdam upload ./image.jpg --project 1
|
|
121
130
|
artdam upload ./image.jpg --project 1 --folder 10
|
|
131
|
+
artdam upload ./big_scene.zip --project 1 # 大文件分块 + 进度条
|
|
122
132
|
```
|
|
123
133
|
|
|
124
134
|
#### `artdam update`
|
|
@@ -44,22 +44,27 @@ artdam login
|
|
|
44
44
|
|
|
45
45
|
#### `artdam search`
|
|
46
46
|
|
|
47
|
-
|
|
47
|
+
搜索资产,返回匹配列表。默认只取首页(`--limit` 条);加 `--all` 自动翻页取全部结果
|
|
48
|
+
(Agent 场景推荐,避免结果被截断)。`--all` 最多返回 1000 条,超出会提示用更精确的关键词或
|
|
49
|
+
`--ext` / `--folder` 收窄,避免在几万条资产上卡死。
|
|
48
50
|
|
|
49
51
|
```bash
|
|
50
52
|
artdam search "头盔" --project 1
|
|
51
53
|
artdam search "头盔" --project 1 --limit 50
|
|
52
|
-
artdam search "头盔" --project 1 --
|
|
54
|
+
artdam search "头盔" --project 1 --all # 自动翻页,取全部匹配
|
|
55
|
+
artdam search "头盔" --project 1 --all --json # 全量 JSON 输出(AI Agent 用)
|
|
56
|
+
artdam search "头盔" --project 1 --ext jpg # 按扩展名过滤
|
|
57
|
+
artdam search "头盔" --project 1 --ext jpg,png # 多个扩展名
|
|
53
58
|
artdam search "头盔" --project 1 --folder 10
|
|
54
|
-
artdam search "头盔" --project 1 --json # 输出原始 JSON(AI Agent 用)
|
|
55
59
|
```
|
|
56
60
|
|
|
57
61
|
| 参数 | 说明 |
|
|
58
62
|
|---|---|
|
|
59
63
|
| `KEYWORD` | 搜索关键词 |
|
|
60
64
|
| `-p, --project` | 项目 ID(必填) |
|
|
61
|
-
| `-n, --limit` |
|
|
62
|
-
| `--
|
|
65
|
+
| `-n, --limit` | 返回数量上限,默认 20(不与 `--all` 共用) |
|
|
66
|
+
| `--all` | 自动翻页取全部匹配结果(最多 1000 条) |
|
|
67
|
+
| `-e, --ext` | 按文件扩展名过滤,逗号分隔,如 `jpg,png` |
|
|
63
68
|
| `--folder` | 限定文件夹 ID |
|
|
64
69
|
| `--json` | 输出原始 JSON |
|
|
65
70
|
|
|
@@ -104,11 +109,14 @@ artdam folders --project 1 --json
|
|
|
104
109
|
|
|
105
110
|
#### `artdam upload`
|
|
106
111
|
|
|
107
|
-
|
|
112
|
+
上传文件到指定项目和文件夹。采用分块上传,支持大文件(最大 10GB),带进度条;
|
|
113
|
+
上传前按内容计算 SHA-256,库中已有相同文件则**秒传**(直接复用,不重复传输)。
|
|
114
|
+
单个分块遇到网络抖动(超时 / 5xx)会自动重试,中断时清理服务端会话。
|
|
108
115
|
|
|
109
116
|
```bash
|
|
110
117
|
artdam upload ./image.jpg --project 1
|
|
111
118
|
artdam upload ./image.jpg --project 1 --folder 10
|
|
119
|
+
artdam upload ./big_scene.zip --project 1 # 大文件分块 + 进度条
|
|
112
120
|
```
|
|
113
121
|
|
|
114
122
|
#### `artdam update`
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import time
|
|
4
|
+
from collections.abc import Callable
|
|
5
|
+
|
|
6
|
+
import httpx
|
|
7
|
+
|
|
8
|
+
from artdam_cli import config
|
|
9
|
+
|
|
10
|
+
# 瞬时错误(5xx / 超时 / 连接重置)重试退避,单位秒;4xx 不重试
|
|
11
|
+
_RETRY_BACKOFF = (1, 2, 4)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _make_client(timeout: float = 30) -> httpx.Client:
|
|
15
|
+
base_url = config.require("base_url")
|
|
16
|
+
token = config.require("token")
|
|
17
|
+
return httpx.Client(
|
|
18
|
+
base_url=base_url,
|
|
19
|
+
headers={"Authorization": f"Bearer {token}"},
|
|
20
|
+
timeout=timeout,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def _send_with_retry(do_request: Callable[[], httpx.Response]) -> httpx.Response:
|
|
25
|
+
"""对瞬时错误(5xx / 超时 / 传输错)指数退避重试;4xx 立即返回交由 _raise。"""
|
|
26
|
+
for attempt in range(len(_RETRY_BACKOFF) + 1):
|
|
27
|
+
try:
|
|
28
|
+
r = do_request()
|
|
29
|
+
except (httpx.TimeoutException, httpx.TransportError) as e:
|
|
30
|
+
if attempt < len(_RETRY_BACKOFF):
|
|
31
|
+
time.sleep(_RETRY_BACKOFF[attempt])
|
|
32
|
+
continue
|
|
33
|
+
raise SystemExit(
|
|
34
|
+
f"网络错误,重试 {len(_RETRY_BACKOFF)} 次仍失败:{e}"
|
|
35
|
+
) from e
|
|
36
|
+
if r.status_code >= 500 and attempt < len(_RETRY_BACKOFF):
|
|
37
|
+
time.sleep(_RETRY_BACKOFF[attempt])
|
|
38
|
+
continue
|
|
39
|
+
return r
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def get(path: str, params: dict | None = None, not_found_msg: str = "资源不存在") -> dict:
|
|
43
|
+
with _make_client() as c:
|
|
44
|
+
r = c.get(path, params=params)
|
|
45
|
+
_raise(r, not_found_msg=not_found_msg)
|
|
46
|
+
return r.json()
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def download_file(path: str, dest: str, not_found_msg: str = "资源不存在") -> int:
|
|
50
|
+
base_url = config.require("base_url")
|
|
51
|
+
token = config.require("token")
|
|
52
|
+
with httpx.stream(
|
|
53
|
+
"GET",
|
|
54
|
+
f"{base_url}{path}",
|
|
55
|
+
headers={"Authorization": f"Bearer {token}"},
|
|
56
|
+
timeout=120,
|
|
57
|
+
follow_redirects=True,
|
|
58
|
+
) as r:
|
|
59
|
+
_raise(r, not_found_msg=not_found_msg)
|
|
60
|
+
total = 0
|
|
61
|
+
with open(dest, "wb") as f:
|
|
62
|
+
for chunk in r.iter_bytes(65536):
|
|
63
|
+
f.write(chunk)
|
|
64
|
+
total += len(chunk)
|
|
65
|
+
return total
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def post(path: str, json: dict | None = None) -> dict:
|
|
69
|
+
with _make_client() as c:
|
|
70
|
+
r = c.post(path, json=json)
|
|
71
|
+
_raise(r)
|
|
72
|
+
return r.json() if r.status_code != 204 else {}
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def patch(path: str, json: dict) -> dict:
|
|
76
|
+
with _make_client() as c:
|
|
77
|
+
r = c.patch(path, json=json)
|
|
78
|
+
_raise(r)
|
|
79
|
+
return r.json()
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def delete_req(path: str) -> None:
|
|
83
|
+
with _make_client() as c:
|
|
84
|
+
r = c.delete(path)
|
|
85
|
+
_raise(r)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def dedup_check(
|
|
89
|
+
project_id: int,
|
|
90
|
+
sha256: str,
|
|
91
|
+
filename: str,
|
|
92
|
+
size: int,
|
|
93
|
+
collection_id: int | None = None,
|
|
94
|
+
) -> dict:
|
|
95
|
+
"""按 sha256 查重;命中则服务端在当前项目建复用记录,返回 {hit, asset_id}。"""
|
|
96
|
+
params: dict = {"project_id": project_id}
|
|
97
|
+
if collection_id is not None:
|
|
98
|
+
params["collection_id"] = collection_id
|
|
99
|
+
with _make_client() as c:
|
|
100
|
+
r = c.post(
|
|
101
|
+
"/api/upload/dedup",
|
|
102
|
+
params=params,
|
|
103
|
+
json={"sha256": sha256, "filename": filename, "size": size},
|
|
104
|
+
)
|
|
105
|
+
_raise(r)
|
|
106
|
+
return r.json()
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def init_upload(
|
|
110
|
+
project_id: int,
|
|
111
|
+
filename: str,
|
|
112
|
+
total_size: int,
|
|
113
|
+
chunk_size: int,
|
|
114
|
+
sha256: str | None = None,
|
|
115
|
+
collection_id: int | None = None,
|
|
116
|
+
) -> dict:
|
|
117
|
+
"""初始化分块上传 session,返回 {upload_id, chunk_size, total_chunks}。"""
|
|
118
|
+
body: dict = {
|
|
119
|
+
"filename": filename,
|
|
120
|
+
"total_size": total_size,
|
|
121
|
+
"chunk_size": chunk_size,
|
|
122
|
+
}
|
|
123
|
+
if sha256:
|
|
124
|
+
body["sha256"] = sha256
|
|
125
|
+
if collection_id is not None:
|
|
126
|
+
body["collection_id"] = collection_id
|
|
127
|
+
with _make_client() as c:
|
|
128
|
+
r = c.post("/api/upload/init", params={"project_id": project_id}, json=body)
|
|
129
|
+
_raise(r)
|
|
130
|
+
return r.json()
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def upload_chunk(upload_id: str, chunk_index: int, data: bytes) -> dict:
|
|
134
|
+
"""上传单个分块(瞬时错误自动重试;同 chunk_index 服务端幂等覆盖)。"""
|
|
135
|
+
|
|
136
|
+
def do() -> httpx.Response:
|
|
137
|
+
with _make_client(timeout=120) as c:
|
|
138
|
+
return c.post(
|
|
139
|
+
"/api/upload/chunk",
|
|
140
|
+
data={"upload_id": upload_id, "chunk_index": chunk_index},
|
|
141
|
+
files={"file": ("chunk", data)},
|
|
142
|
+
)
|
|
143
|
+
|
|
144
|
+
r = _send_with_retry(do)
|
|
145
|
+
_raise(r)
|
|
146
|
+
return r.json()
|
|
147
|
+
|
|
148
|
+
|
|
149
|
+
def complete_upload(upload_id: str) -> dict:
|
|
150
|
+
"""合并分块并建 asset,返回 {asset_id, filename}。"""
|
|
151
|
+
with _make_client(timeout=120) as c:
|
|
152
|
+
r = c.post("/api/upload/complete", params={"upload_id": upload_id})
|
|
153
|
+
_raise(r)
|
|
154
|
+
return r.json()
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def cancel_upload(upload_id: str) -> None:
|
|
158
|
+
"""best-effort 取消上传 session(清服务端 chunks),失败静默。"""
|
|
159
|
+
try:
|
|
160
|
+
with _make_client() as c:
|
|
161
|
+
c.delete(f"/api/upload/{upload_id}")
|
|
162
|
+
except Exception:
|
|
163
|
+
pass
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def _raise(r: httpx.Response, not_found_msg: str = "资源不存在") -> None:
|
|
167
|
+
if r.status_code == 401:
|
|
168
|
+
raise SystemExit("Token 无效或已过期,请重新运行 artdam login")
|
|
169
|
+
if r.status_code == 404:
|
|
170
|
+
raise SystemExit(not_found_msg)
|
|
171
|
+
if r.is_error:
|
|
172
|
+
raise SystemExit(f"请求失败 {r.status_code}: {r.text[:200]}")
|
|
@@ -6,7 +6,14 @@ from artdam_cli import client
|
|
|
6
6
|
@click.command()
|
|
7
7
|
@click.argument("asset_id", type=int)
|
|
8
8
|
@click.argument("text")
|
|
9
|
-
|
|
9
|
+
@click.option(
|
|
10
|
+
"--scope",
|
|
11
|
+
type=click.Choice(["project", "publish"]),
|
|
12
|
+
default="project",
|
|
13
|
+
show_default=True,
|
|
14
|
+
help="评论可见范围",
|
|
15
|
+
)
|
|
16
|
+
def comment(asset_id: int, text: str, scope: str) -> None:
|
|
10
17
|
"""给资产添加评论。"""
|
|
11
|
-
data = client.post(f"/api/assets/{asset_id}/comments", {"content": text})
|
|
18
|
+
data = client.post(f"/api/assets/{asset_id}/comments", {"content": text, "scope": scope})
|
|
12
19
|
click.echo(f"✓ 评论已添加 id={data.get('id')}")
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import click
|
|
2
|
+
|
|
3
|
+
from artdam_cli import client
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@click.command(name="delete")
|
|
7
|
+
@click.argument("asset_id", type=int)
|
|
8
|
+
@click.option("--yes", "-y", is_flag=True, help="跳过确认直接删除(脚本调用用)")
|
|
9
|
+
def delete_cmd(asset_id: int, yes: bool) -> None:
|
|
10
|
+
"""删除资产(软删除,可通过 restore 恢复)。"""
|
|
11
|
+
if not yes:
|
|
12
|
+
data = client.get(f"/api/assets/{asset_id}", not_found_msg=f"资产 ID {asset_id} 不存在")
|
|
13
|
+
filename = data.get("filename", str(asset_id))
|
|
14
|
+
click.confirm(f"确认删除「{filename}」(ID {asset_id})?", abort=True)
|
|
15
|
+
client.delete_req(f"/api/assets/{asset_id}")
|
|
16
|
+
click.echo(f"✓ 资产 {asset_id} 已删除(可用 artdam restore {asset_id} 恢复)")
|
|
@@ -14,14 +14,21 @@ console = Console()
|
|
|
14
14
|
@click.option("--out", "-o", default=".", help="下载目录(默认当前目录)")
|
|
15
15
|
def download(asset_id: int, out: str) -> None:
|
|
16
16
|
"""下载资产文件。"""
|
|
17
|
-
data = client.get(
|
|
17
|
+
data = client.get(
|
|
18
|
+
f"/api/assets/{asset_id}",
|
|
19
|
+
not_found_msg=f"资产 ID {asset_id} 不存在",
|
|
20
|
+
)
|
|
18
21
|
filename = data.get("filename", f"asset_{asset_id}")
|
|
19
22
|
dest = os.path.join(out, filename)
|
|
20
23
|
os.makedirs(out, exist_ok=True)
|
|
21
24
|
|
|
22
25
|
with Progress() as progress:
|
|
23
26
|
task = progress.add_task(f"下载 {filename}...", total=None)
|
|
24
|
-
total = client.download_file(
|
|
27
|
+
total = client.download_file(
|
|
28
|
+
f"/api/assets/{asset_id}/file",
|
|
29
|
+
dest,
|
|
30
|
+
not_found_msg=f"资产 {asset_id} 的文件无法下载(文件不存在)",
|
|
31
|
+
)
|
|
25
32
|
progress.update(task, completed=total, total=total)
|
|
26
33
|
|
|
27
34
|
size_str = f"{total / 1024 / 1024:.1f} MB"
|
|
@@ -3,7 +3,6 @@ import json
|
|
|
3
3
|
import click
|
|
4
4
|
from rich.console import Console
|
|
5
5
|
from rich.panel import Panel
|
|
6
|
-
from rich.text import Text
|
|
7
6
|
|
|
8
7
|
from artdam_cli import client, config
|
|
9
8
|
|
|
@@ -23,7 +22,16 @@ def get(asset_id: int, as_json: bool) -> None:
|
|
|
23
22
|
|
|
24
23
|
tags = ", ".join(t["name"] for t in data.get("tags", []))
|
|
25
24
|
size = data.get("file_size") or 0
|
|
26
|
-
|
|
25
|
+
if size <= 0:
|
|
26
|
+
size_str = "—"
|
|
27
|
+
elif size < 1024:
|
|
28
|
+
size_str = f"{size} B"
|
|
29
|
+
elif size < 1024 * 1024:
|
|
30
|
+
size_str = f"{size / 1024:.1f} KB"
|
|
31
|
+
else:
|
|
32
|
+
size_str = f"{size / 1024 / 1024:.1f} MB"
|
|
33
|
+
raw_time = data.get("created_at", "—")
|
|
34
|
+
time_str = raw_time[:16].replace("T", " ") if raw_time and "T" in raw_time else raw_time
|
|
27
35
|
base_url = config.get("base_url") or ""
|
|
28
36
|
# 前端端口通常是后端端口替换:8000→5173,3082→3090
|
|
29
37
|
frontend_url = base_url.replace(":8000", ":5173").replace(":3082", ":3090")
|
|
@@ -36,7 +44,7 @@ def get(asset_id: int, as_json: bool) -> None:
|
|
|
36
44
|
f"[bold]大小[/bold] {size_str}",
|
|
37
45
|
f"[bold]标签[/bold] {tags or '—'}",
|
|
38
46
|
f"[bold]AI 描述[/bold] {data.get('ai_description') or '—'}",
|
|
39
|
-
f"[bold]上传时间[/bold] {
|
|
47
|
+
f"[bold]上传时间[/bold] {time_str}",
|
|
40
48
|
f"[bold]路径[/bold] {data.get('original_path', '—')}",
|
|
41
49
|
f"[bold]预览[/bold] {preview_url}",
|
|
42
50
|
]
|
|
@@ -58,7 +58,9 @@ def _sso_login(url: str) -> None:
|
|
|
58
58
|
raise SystemExit("登录超时(120 秒),请重试")
|
|
59
59
|
|
|
60
60
|
server.shutdown()
|
|
61
|
-
config.
|
|
61
|
+
data = config.load()
|
|
62
|
+
data.update({"base_url": url, "token": token_holder["value"]})
|
|
63
|
+
config.save(data)
|
|
62
64
|
click.echo("✓ SSO 登录成功,配置已保存到 ~/.artdam/config.json")
|
|
63
65
|
|
|
64
66
|
|
|
@@ -81,7 +83,9 @@ def _password_login(url: str, username: str | None, passwd: str | None) -> None:
|
|
|
81
83
|
token = r.json().get("access_token")
|
|
82
84
|
if not token:
|
|
83
85
|
raise SystemExit("登录响应中未找到 token")
|
|
84
|
-
config.
|
|
86
|
+
data = config.load()
|
|
87
|
+
data.update({"base_url": url, "token": token})
|
|
88
|
+
config.save(data)
|
|
85
89
|
click.echo("✓ 登录成功,配置已保存到 ~/.artdam/config.json")
|
|
86
90
|
|
|
87
91
|
|
|
@@ -3,9 +3,12 @@ import click
|
|
|
3
3
|
from artdam_cli import config
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
@click.group(name="project")
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
@click.group(name="project", invoke_without_command=True)
|
|
7
|
+
@click.pass_context
|
|
8
|
+
def project_group(ctx: click.Context) -> None:
|
|
9
|
+
"""管理默认项目(省去每次输入 -p)。不带子命令时等同于 project show。"""
|
|
10
|
+
if ctx.invoked_subcommand is None:
|
|
11
|
+
ctx.invoke(project_show)
|
|
9
12
|
|
|
10
13
|
|
|
11
14
|
@project_group.command(name="use")
|
|
@@ -12,19 +12,19 @@ console = Console()
|
|
|
12
12
|
@click.command()
|
|
13
13
|
@click.option("--json", "as_json", is_flag=True, help="输出原始 JSON(AI Agent 用)")
|
|
14
14
|
def projects(as_json: bool) -> None:
|
|
15
|
-
"""
|
|
15
|
+
"""列出当前用户已加入的所有项目。"""
|
|
16
16
|
data = client.get("/api/projects/")
|
|
17
|
-
items = data if isinstance(data, list) else []
|
|
17
|
+
items = [p for p in (data if isinstance(data, list) else []) if p.get("user_role")]
|
|
18
18
|
|
|
19
19
|
if as_json:
|
|
20
20
|
click.echo(json.dumps(items, ensure_ascii=False, indent=2))
|
|
21
21
|
return
|
|
22
22
|
|
|
23
23
|
if not items:
|
|
24
|
-
console.print("[yellow]
|
|
24
|
+
console.print("[yellow]暂无已加入的项目[/yellow]")
|
|
25
25
|
return
|
|
26
26
|
|
|
27
|
-
table = Table(title=f"
|
|
27
|
+
table = Table(title=f"我的项目(共 {len(items)} 个)")
|
|
28
28
|
table.add_column("ID", style="dim", width=6)
|
|
29
29
|
table.add_column("代码", width=10)
|
|
30
30
|
table.add_column("名称", min_width=16)
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import json
|
|
2
|
+
|
|
3
|
+
import click
|
|
4
|
+
from rich.console import Console
|
|
5
|
+
from rich.table import Table
|
|
6
|
+
|
|
7
|
+
from artdam_cli import client, config
|
|
8
|
+
|
|
9
|
+
console = Console()
|
|
10
|
+
|
|
11
|
+
_PAGE_SIZE = 50 # 翻页时每页取 50 条(后端上限 page_size=200,50 兼顾速度与内存)
|
|
12
|
+
_FETCH_ALL_CAP = 1000 # --all 最多拉取条数,超过则提示收窄查询,避免几万条卡死/爆内存
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _fetch_all(params: dict, console: Console) -> tuple[list[dict], int, bool]:
|
|
16
|
+
"""自动翻页取结果,最多 _FETCH_ALL_CAP 条。返回 (items, total, capped)。
|
|
17
|
+
|
|
18
|
+
capped=True 表示结果总数超过上限、只返回了前 _FETCH_ALL_CAP 条。
|
|
19
|
+
翻页时在 TTY 显示进度(非 TTY / --json 管道下 Rich 自动静默)。
|
|
20
|
+
"""
|
|
21
|
+
items: list[dict] = []
|
|
22
|
+
page = 1
|
|
23
|
+
total = 0
|
|
24
|
+
with console.status("正在翻页拉取…") as status:
|
|
25
|
+
while True:
|
|
26
|
+
data = client.get(
|
|
27
|
+
"/api/assets/", params={**params, "page": page, "page_size": _PAGE_SIZE}
|
|
28
|
+
)
|
|
29
|
+
page_items = data.get("items", [])
|
|
30
|
+
items.extend(page_items)
|
|
31
|
+
total = data.get("total", len(items))
|
|
32
|
+
status.update(f"已拉取 {len(items)}/{total} 条…")
|
|
33
|
+
if (
|
|
34
|
+
not page_items
|
|
35
|
+
or len(items) >= total
|
|
36
|
+
or len(items) >= _FETCH_ALL_CAP
|
|
37
|
+
):
|
|
38
|
+
break
|
|
39
|
+
page += 1
|
|
40
|
+
capped = len(items) >= _FETCH_ALL_CAP and total > _FETCH_ALL_CAP
|
|
41
|
+
return items[:_FETCH_ALL_CAP], total, capped
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
@click.command()
|
|
45
|
+
@click.argument("keyword", default="", required=False)
|
|
46
|
+
@click.option("--project", "-p", default=None, type=int, help="项目 ID(省略则使用默认项目)")
|
|
47
|
+
@click.option("--limit", "-n", default=50, show_default=True, help="返回数量上限(不与 --all 共用)")
|
|
48
|
+
@click.option("--all", "fetch_all", is_flag=True, help=f"自动翻页取全部结果(最多 {_FETCH_ALL_CAP} 条,超出会提示收窄)")
|
|
49
|
+
@click.option("--ext", "-e", default=None, help="按文件扩展名过滤,逗号分隔,例: jpg,png")
|
|
50
|
+
@click.option("--folder", default=None, type=int, help="限定文件夹 ID")
|
|
51
|
+
@click.option("--json", "as_json", is_flag=True, help="输出原始 JSON(AI Agent 用)")
|
|
52
|
+
def search(
|
|
53
|
+
keyword: str,
|
|
54
|
+
project: int | None,
|
|
55
|
+
limit: int,
|
|
56
|
+
fetch_all: bool,
|
|
57
|
+
ext: str | None,
|
|
58
|
+
folder: int | None,
|
|
59
|
+
as_json: bool,
|
|
60
|
+
) -> None:
|
|
61
|
+
"""搜索资产。KEYWORD 可省略(列出全部资产)。加 --all 可自动翻页取全部结果。"""
|
|
62
|
+
project = config.require_project(project)
|
|
63
|
+
base_params: dict = {"project_id": project}
|
|
64
|
+
if keyword:
|
|
65
|
+
base_params["q"] = keyword
|
|
66
|
+
if ext:
|
|
67
|
+
# 归一化:去掉用户可能带的点号(".jpg" → "jpg"),对接后端 extensions
|
|
68
|
+
base_params["extensions"] = ",".join(
|
|
69
|
+
e.strip().lstrip(".") for e in ext.split(",") if e.strip()
|
|
70
|
+
)
|
|
71
|
+
if folder:
|
|
72
|
+
base_params["folder_id"] = folder
|
|
73
|
+
|
|
74
|
+
capped = False
|
|
75
|
+
if fetch_all:
|
|
76
|
+
items, total, capped = _fetch_all(base_params, console)
|
|
77
|
+
else:
|
|
78
|
+
data = client.get("/api/assets/", params={**base_params, "page_size": limit})
|
|
79
|
+
items = data.get("items", [])
|
|
80
|
+
total = data.get("total", len(items))
|
|
81
|
+
|
|
82
|
+
if capped:
|
|
83
|
+
# 警告走 stderr,避免污染 --json 的 stdout
|
|
84
|
+
click.echo(
|
|
85
|
+
f"⚠ 结果共 {total} 条,--all 最多返回 {_FETCH_ALL_CAP} 条;"
|
|
86
|
+
f"请用更精确的关键词或加 --ext / --folder 收窄。",
|
|
87
|
+
err=True,
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
if as_json:
|
|
91
|
+
click.echo(json.dumps(items, ensure_ascii=False, indent=2))
|
|
92
|
+
return
|
|
93
|
+
|
|
94
|
+
if not items:
|
|
95
|
+
console.print("[yellow]未找到匹配资产[/yellow]")
|
|
96
|
+
return
|
|
97
|
+
|
|
98
|
+
suffix = f"(显示 {len(items)}/{total})" if not fetch_all and len(items) < total else ""
|
|
99
|
+
title = f"搜索「{keyword}」— 共 {total} 条{suffix}" if keyword else f"全部资产 — 共 {total} 条{suffix}"
|
|
100
|
+
table = Table(title=title)
|
|
101
|
+
table.add_column("ID", style="dim", width=6)
|
|
102
|
+
table.add_column("文件名", min_width=20)
|
|
103
|
+
table.add_column("类型", width=10)
|
|
104
|
+
table.add_column("大小", width=10)
|
|
105
|
+
table.add_column("标签", min_width=20)
|
|
106
|
+
|
|
107
|
+
for item in items:
|
|
108
|
+
tags = ", ".join(t["name"] for t in item.get("tags", []))
|
|
109
|
+
size = item.get("file_size") or 0
|
|
110
|
+
size_str = f"{size / 1024 / 1024:.1f} MB" if size > 0 else "—"
|
|
111
|
+
table.add_row(
|
|
112
|
+
str(item["id"]),
|
|
113
|
+
item.get("filename", ""),
|
|
114
|
+
(item.get("mime_type") or "").split("/")[-1],
|
|
115
|
+
size_str,
|
|
116
|
+
tags or "—",
|
|
117
|
+
)
|
|
118
|
+
|
|
119
|
+
console.print(table)
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import json
|
|
2
|
+
|
|
3
|
+
import click
|
|
4
|
+
from rich.console import Console
|
|
5
|
+
from rich.table import Table
|
|
6
|
+
|
|
7
|
+
from artdam_cli import client, config
|
|
8
|
+
|
|
9
|
+
console = Console()
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
_DEFAULT_LIMIT = 50
|
|
13
|
+
# --all / -s 最多显示条数。/api/tags/ 不分页、一次返回全部,
|
|
14
|
+
# 正式项目标签可达数万,无上限会刷爆终端,故截断显示并提示收窄。
|
|
15
|
+
_MAX_DISPLAY = 1000
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
@click.command()
|
|
19
|
+
@click.option("--project", "-p", default=None, type=int, help="项目 ID(省略则使用默认项目)")
|
|
20
|
+
@click.option("--search", "-s", default=None, help="按名称过滤")
|
|
21
|
+
@click.option("--all", "show_all", is_flag=True, help=f"显示全部标签(默认只显示前 {_DEFAULT_LIMIT} 个,最多 {_MAX_DISPLAY} 个)")
|
|
22
|
+
@click.option("--json", "as_json", is_flag=True, help="输出原始 JSON")
|
|
23
|
+
def tags(project: int | None, search: str | None, show_all: bool, as_json: bool) -> None:
|
|
24
|
+
"""列出项目标签。默认显示前 50 个,用 -s 过滤或 --all 查看全部(最多 1000)。"""
|
|
25
|
+
project = config.require_project(project)
|
|
26
|
+
if search:
|
|
27
|
+
data = client.get("/api/tags/search", params={"project_id": project, "q": search})
|
|
28
|
+
else:
|
|
29
|
+
data = client.get("/api/tags/", params={"project_id": project})
|
|
30
|
+
|
|
31
|
+
items = data if isinstance(data, list) else data.get("items", [])
|
|
32
|
+
|
|
33
|
+
if as_json:
|
|
34
|
+
click.echo(json.dumps(items, ensure_ascii=False, indent=2))
|
|
35
|
+
return
|
|
36
|
+
|
|
37
|
+
if not items:
|
|
38
|
+
console.print("[yellow]暂无标签[/yellow]")
|
|
39
|
+
return
|
|
40
|
+
|
|
41
|
+
total = len(items)
|
|
42
|
+
# 默认(无 -s 无 --all)截到 50;-s 或 --all 截到 _MAX_DISPLAY。
|
|
43
|
+
cap = _MAX_DISPLAY if (show_all or search) else _DEFAULT_LIMIT
|
|
44
|
+
display = items[:cap]
|
|
45
|
+
truncated = total > len(display)
|
|
46
|
+
|
|
47
|
+
suffix = f"(显示 {len(display)}/{total} 个)" if truncated else f"({total} 个)"
|
|
48
|
+
table = Table(title=f"项目 {project} 标签列表{suffix}")
|
|
49
|
+
table.add_column("ID", style="dim", width=6)
|
|
50
|
+
table.add_column("标签名", min_width=20)
|
|
51
|
+
table.add_column("资产数", width=8)
|
|
52
|
+
|
|
53
|
+
for item in display:
|
|
54
|
+
table.add_row(
|
|
55
|
+
str(item["id"]),
|
|
56
|
+
item.get("name", ""),
|
|
57
|
+
str(item.get("asset_count", "—")),
|
|
58
|
+
)
|
|
59
|
+
|
|
60
|
+
console.print(table)
|
|
61
|
+
|
|
62
|
+
if truncated:
|
|
63
|
+
hint = (
|
|
64
|
+
f"已达显示上限 {_MAX_DISPLAY} 个,请用 -s <关键词> 收窄。"
|
|
65
|
+
if cap == _MAX_DISPLAY
|
|
66
|
+
else f"还有 {total - len(display)} 个标签未显示,用 -s <关键词> 过滤或加 --all 查看全部。"
|
|
67
|
+
)
|
|
68
|
+
click.echo(hint, err=True)
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import click
|
|
2
|
+
from rich.console import Console
|
|
3
|
+
|
|
4
|
+
from artdam_cli import config, uploader
|
|
5
|
+
|
|
6
|
+
console = Console()
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
@click.command()
|
|
10
|
+
@click.argument("file_path", type=click.Path(exists=True, dir_okay=False))
|
|
11
|
+
@click.option("--project", "-p", type=int, default=None, help="项目 ID(省略则使用默认项目)")
|
|
12
|
+
@click.option("--folder", "-f", type=int, default=None, help="目标文件夹 ID")
|
|
13
|
+
def upload(file_path: str, project: int | None, folder: int | None) -> None:
|
|
14
|
+
"""上传文件到 ArtDAM(分块上传,支持大文件 + 秒传)。"""
|
|
15
|
+
project = config.require_project(project)
|
|
16
|
+
result = uploader.chunked_upload(file_path, project, folder, console)
|
|
17
|
+
if result.get("deduped"):
|
|
18
|
+
console.print(
|
|
19
|
+
f"✓ 秒传(库中已有相同文件) asset_id={result['asset_id']} "
|
|
20
|
+
f"文件名={result['filename']}"
|
|
21
|
+
)
|
|
22
|
+
else:
|
|
23
|
+
console.print(
|
|
24
|
+
f"✓ 上传成功 asset_id={result['asset_id']} 文件名={result['filename']}"
|
|
25
|
+
)
|