magic-campus-cli 0.1.0__py3-none-any.whl

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.
Files changed (53) hide show
  1. magic_campus_cli-0.1.0.dist-info/METADATA +51 -0
  2. magic_campus_cli-0.1.0.dist-info/RECORD +53 -0
  3. magic_campus_cli-0.1.0.dist-info/WHEEL +5 -0
  4. magic_campus_cli-0.1.0.dist-info/entry_points.txt +2 -0
  5. magic_campus_cli-0.1.0.dist-info/top_level.txt +2 -0
  6. magiccampus_cli/__init__.py +1 -0
  7. magiccampus_cli/__main__.py +9 -0
  8. magiccampus_cli/accounts.py +144 -0
  9. magiccampus_cli/cli.py +176 -0
  10. magiccampus_cli/client.py +54 -0
  11. magiccampus_cli/commands/auth.py +32 -0
  12. magiccampus_cli/commands/comment.py +38 -0
  13. magiccampus_cli/commands/config.py +95 -0
  14. magiccampus_cli/commands/feed.py +26 -0
  15. magiccampus_cli/commands/group.py +346 -0
  16. magiccampus_cli/commands/im.py +47 -0
  17. magiccampus_cli/commands/notify.py +81 -0
  18. magiccampus_cli/commands/playgroup.py +49 -0
  19. magiccampus_cli/commands/reaction.py +50 -0
  20. magiccampus_cli/commands/space.py +134 -0
  21. magiccampus_cli/commands/user.py +311 -0
  22. magiccampus_cli/config_store.py +61 -0
  23. magiccampus_cli/notify.py +493 -0
  24. magiccampus_cli/output.py +104 -0
  25. magiccampus_cli/prompts.py +58 -0
  26. magiccampus_cli/targets.py +26 -0
  27. magiccampus_cli/update.py +149 -0
  28. magiccampus_cli/utils.py +67 -0
  29. magiccampus_sdk/__init__.py +43 -0
  30. magiccampus_sdk/auth.py +111 -0
  31. magiccampus_sdk/comment_client.py +107 -0
  32. magiccampus_sdk/feed_api.py +61 -0
  33. magiccampus_sdk/feed_client.py +30 -0
  34. magiccampus_sdk/group_api.py +139 -0
  35. magiccampus_sdk/group_client.py +242 -0
  36. magiccampus_sdk/http/__init__.py +1 -0
  37. magiccampus_sdk/http/group_info_api.py +47 -0
  38. magiccampus_sdk/http/http_api.py +58 -0
  39. magiccampus_sdk/http/http_send.py +758 -0
  40. magiccampus_sdk/http/media.py +839 -0
  41. magiccampus_sdk/http/models.py +645 -0
  42. magiccampus_sdk/http/self_user_info_api.py +182 -0
  43. magiccampus_sdk/http/text.py +75 -0
  44. magiccampus_sdk/models.py +279 -0
  45. magiccampus_sdk/platform_client.py +137 -0
  46. magiccampus_sdk/platform_http.py +71 -0
  47. magiccampus_sdk/playgroup_api.py +104 -0
  48. magiccampus_sdk/playgroup_client.py +45 -0
  49. magiccampus_sdk/reaction_api.py +110 -0
  50. magiccampus_sdk/space_api.py +170 -0
  51. magiccampus_sdk/space_client.py +93 -0
  52. magiccampus_sdk/user_client.py +211 -0
  53. magiccampus_sdk/user_info_api.py +593 -0
@@ -0,0 +1,839 @@
1
+ import hashlib
2
+ import json
3
+ import math
4
+ import mimetypes
5
+ import os
6
+ import shutil
7
+ import struct
8
+ import subprocess
9
+ import tempfile
10
+ import time
11
+ import urllib.parse
12
+ import urllib.request
13
+ import uuid
14
+ from contextlib import contextmanager
15
+ from dataclasses import dataclass
16
+ from typing import Any, Callable, Dict, Iterable, Iterator, List, Mapping, Optional, Sequence, Tuple, Union
17
+
18
+ from .http_api import post_json_data
19
+ from .models import WSMessage
20
+
21
+
22
+ QuoteMessageInput = Union[Mapping[str, Any], WSMessage]
23
+
24
+
25
+ @dataclass(frozen=True)
26
+ class UploadPartSign:
27
+ part_number: int
28
+ url: str
29
+ query: Tuple[Dict[str, Sequence[str]], ...] = ()
30
+ header: Tuple[Dict[str, Sequence[str]], ...] = ()
31
+
32
+
33
+ @dataclass(frozen=True)
34
+ class UploadSignBatch:
35
+ url: str
36
+ query: Tuple[Dict[str, Sequence[str]], ...]
37
+ header: Tuple[Dict[str, Sequence[str]], ...]
38
+ parts: Tuple[UploadPartSign, ...]
39
+
40
+
41
+ @dataclass(frozen=True)
42
+ class ImageMetadata:
43
+ width: int
44
+ height: int
45
+ content_type: str
46
+ size: int
47
+
48
+
49
+ def probe_video_duration_ms(path: str) -> int:
50
+ resolved = os.path.abspath(path)
51
+ ffprobe = shutil.which("ffprobe")
52
+ if not ffprobe:
53
+ raise RuntimeError("ffprobe is required to inspect video duration")
54
+ probe = subprocess.run(
55
+ [
56
+ ffprobe,
57
+ "-v",
58
+ "error",
59
+ "-show_entries",
60
+ "format=duration",
61
+ "-of",
62
+ "default=noprint_wrappers=1:nokey=1",
63
+ resolved,
64
+ ],
65
+ check=True,
66
+ capture_output=True,
67
+ text=True,
68
+ )
69
+ raw_duration = probe.stdout.strip()
70
+ if not raw_duration:
71
+ raise RuntimeError(f"unable to detect video duration: {resolved}")
72
+ duration_ms = math.ceil(float(raw_duration) * 1000)
73
+ if duration_ms <= 0:
74
+ raise RuntimeError(f"invalid video duration for {resolved}: {raw_duration}")
75
+ return duration_ms
76
+
77
+
78
+ def extract_video_first_frame(path: str) -> str:
79
+ resolved = os.path.abspath(path)
80
+ ffmpeg = shutil.which("ffmpeg")
81
+ if not ffmpeg:
82
+ raise RuntimeError("ffmpeg is required to extract a video snapshot")
83
+ fd, snapshot_path = tempfile.mkstemp(prefix="openim-video-snapshot-", suffix=".jpg")
84
+ os.close(fd)
85
+ os.unlink(snapshot_path)
86
+ try:
87
+ subprocess.run(
88
+ [
89
+ ffmpeg,
90
+ "-y",
91
+ "-i",
92
+ resolved,
93
+ "-frames:v",
94
+ "1",
95
+ "-q:v",
96
+ "2",
97
+ snapshot_path,
98
+ ],
99
+ check=True,
100
+ capture_output=True,
101
+ text=True,
102
+ )
103
+ if not os.path.isfile(snapshot_path) or os.path.getsize(snapshot_path) <= 0:
104
+ raise RuntimeError(f"failed to generate snapshot for {resolved}")
105
+ return snapshot_path
106
+ except Exception:
107
+ if os.path.exists(snapshot_path):
108
+ os.unlink(snapshot_path)
109
+ raise
110
+
111
+
112
+ @contextmanager
113
+ def auto_video_snapshot(video_path: str, snapshot_path: str = "") -> Iterator[str]:
114
+ if snapshot_path:
115
+ yield snapshot_path
116
+ return
117
+ generated_snapshot = ""
118
+ try:
119
+ generated_snapshot = extract_video_first_frame(video_path)
120
+ except Exception:
121
+ generated_snapshot = ""
122
+ try:
123
+ yield generated_snapshot
124
+ finally:
125
+ if generated_snapshot and os.path.exists(generated_snapshot):
126
+ os.unlink(generated_snapshot)
127
+
128
+
129
+ def build_text_content(text: str) -> Dict[str, Any]:
130
+ return {"content": text}
131
+
132
+
133
+ def build_custom_content(
134
+ custom: Any,
135
+ *,
136
+ extension: str = "",
137
+ description: str = "",
138
+ ) -> Dict[str, str]:
139
+ if isinstance(custom, Mapping) and any(
140
+ key in custom for key in ("data", "extension", "description")
141
+ ):
142
+ data = custom.get("data", "")
143
+ extension = str(custom.get("extension", extension) or "")
144
+ description = str(custom.get("description", description) or "")
145
+ else:
146
+ data = custom
147
+
148
+ if isinstance(data, (Mapping, list)):
149
+ data = json.dumps(data, ensure_ascii=False, separators=(",", ":"))
150
+ elif data is None:
151
+ data = ""
152
+ else:
153
+ data = str(data)
154
+
155
+ return {
156
+ "data": data,
157
+ "extension": str(extension or ""),
158
+ "description": str(description or ""),
159
+ }
160
+
161
+
162
+ def merge_message_ex(ex: str = "", **fields: Any) -> str:
163
+ payload: Dict[str, Any] = {}
164
+ normalized_ex = str(ex or "").strip()
165
+ if normalized_ex:
166
+ parsed = json.loads(normalized_ex)
167
+ if not isinstance(parsed, dict):
168
+ raise ValueError("ex must be a JSON object string")
169
+ payload.update(parsed)
170
+ for key, value in fields.items():
171
+ if value is not None:
172
+ payload[str(key)] = value
173
+ if not payload:
174
+ return ""
175
+ return json.dumps(payload, ensure_ascii=False, separators=(",", ":"))
176
+
177
+
178
+ def build_markdown_ex(ex: str = "") -> str:
179
+ return merge_message_ex(ex, textFormat="markdown")
180
+
181
+
182
+ def build_at_text_content(
183
+ *,
184
+ text: str,
185
+ at_user_list: Sequence[str],
186
+ at_users_info: Optional[Sequence[Dict[str, str]]] = None,
187
+ quote_message: Optional[QuoteMessageInput] = None,
188
+ is_at_self: bool = False,
189
+ ) -> Dict[str, Any]:
190
+ unique_user_ids: List[str] = []
191
+ seen_user_ids: set[str] = set()
192
+ for user_id in at_user_list:
193
+ normalized_user_id = str(user_id or "").strip()
194
+ if not normalized_user_id or normalized_user_id in seen_user_ids:
195
+ continue
196
+ seen_user_ids.add(normalized_user_id)
197
+ unique_user_ids.append(normalized_user_id)
198
+
199
+ normalized_users_info: List[Dict[str, str]] = []
200
+ seen_info_user_ids: set[str] = set()
201
+ for item in at_users_info or ():
202
+ at_user_id = str(item.get("atUserID") or item.get("at_user_id") or "").strip()
203
+ if not at_user_id or at_user_id in seen_info_user_ids:
204
+ continue
205
+ seen_info_user_ids.add(at_user_id)
206
+ normalized_users_info.append(
207
+ {
208
+ "atUserID": at_user_id,
209
+ "groupNickname": str(item.get("groupNickname") or item.get("group_nickname") or "").strip(),
210
+ }
211
+ )
212
+ if not normalized_users_info:
213
+ normalized_users_info = [{"atUserID": user_id, "groupNickname": ""} for user_id in unique_user_ids]
214
+
215
+ payload: Dict[str, Any] = {
216
+ "text": text,
217
+ "atUserList": unique_user_ids,
218
+ "atUsersInfo": normalized_users_info,
219
+ "isAtSelf": bool(is_at_self),
220
+ }
221
+ normalized_quote_message = normalize_quote_message(quote_message)
222
+ if normalized_quote_message:
223
+ payload["quoteMessage"] = normalized_quote_message
224
+ return payload
225
+
226
+
227
+ def build_quote_content(
228
+ *,
229
+ text: str,
230
+ quote_message: QuoteMessageInput,
231
+ message_entity_list: Optional[Sequence[Dict[str, Any]]] = None,
232
+ ) -> Dict[str, Any]:
233
+ return {
234
+ "text": str(text or ""),
235
+ "quoteMessage": normalize_quote_message(quote_message),
236
+ "messageEntityList": [dict(item) for item in message_entity_list or ()],
237
+ }
238
+
239
+
240
+ def normalize_quote_message(quote_message: Optional[QuoteMessageInput]) -> Dict[str, Any]:
241
+ if quote_message is None:
242
+ return {}
243
+ if isinstance(quote_message, WSMessage):
244
+ return quote_message.to_quote_message()
245
+ return dict(quote_message)
246
+
247
+
248
+ def build_picture_content_for_upload(
249
+ *,
250
+ image_path: str,
251
+ uploaded_url: str,
252
+ ) -> Dict[str, Any]:
253
+ meta = read_image_metadata(image_path)
254
+ picture_info = {
255
+ "uuid": uuid.uuid4().hex,
256
+ "type": meta.content_type,
257
+ "size": meta.size,
258
+ "width": meta.width,
259
+ "height": meta.height,
260
+ "url": uploaded_url,
261
+ }
262
+ return {
263
+ "sourcePath": image_path,
264
+ "sourcePicture": picture_info,
265
+ "bigPicture": dict(picture_info),
266
+ "snapshotPicture": _build_snapshot_picture(uploaded_url, picture_info),
267
+ }
268
+
269
+
270
+ def build_picture_content_by_url(
271
+ *,
272
+ source_url: str,
273
+ width: int,
274
+ height: int,
275
+ size: int = 0,
276
+ source_path: str = "",
277
+ content_type: str = "",
278
+ big_picture_url: str = "",
279
+ snapshot_url: str = "",
280
+ snapshot_width: int = 0,
281
+ snapshot_height: int = 0,
282
+ uuid_value: str = "",
283
+ ) -> Dict[str, Any]:
284
+ picture_info = {
285
+ "uuid": uuid_value or uuid.uuid4().hex,
286
+ "type": content_type or detect_content_type(source_path or source_url, default="image/*"),
287
+ "size": int(size),
288
+ "width": int(width),
289
+ "height": int(height),
290
+ "url": source_url,
291
+ }
292
+ big_picture = dict(picture_info)
293
+ if big_picture_url:
294
+ big_picture["url"] = big_picture_url
295
+ snapshot_picture = _build_snapshot_picture(snapshot_url or source_url, picture_info)
296
+ if snapshot_width > 0:
297
+ snapshot_picture["width"] = int(snapshot_width)
298
+ if snapshot_height > 0:
299
+ snapshot_picture["height"] = int(snapshot_height)
300
+ return {
301
+ "sourcePath": source_path,
302
+ "sourcePicture": picture_info,
303
+ "bigPicture": big_picture,
304
+ "snapshotPicture": snapshot_picture,
305
+ }
306
+
307
+
308
+ def build_sound_content_for_upload(
309
+ *,
310
+ sound_path: str,
311
+ uploaded_url: str,
312
+ duration: int,
313
+ sound_type: str = "",
314
+ ) -> Dict[str, Any]:
315
+ return {
316
+ "uuid": uuid.uuid4().hex,
317
+ "soundPath": sound_path,
318
+ "sourceUrl": uploaded_url,
319
+ "dataSize": os.path.getsize(sound_path),
320
+ "duration": int(duration),
321
+ "soundType": sound_type or detect_content_type(sound_path, default="application/octet-stream"),
322
+ }
323
+
324
+
325
+ def build_sound_content_by_url(
326
+ *,
327
+ source_url: str,
328
+ duration: int,
329
+ data_size: int = 0,
330
+ sound_path: str = "",
331
+ sound_type: str = "",
332
+ uuid_value: str = "",
333
+ ) -> Dict[str, Any]:
334
+ return {
335
+ "uuid": uuid_value or uuid.uuid4().hex,
336
+ "soundPath": sound_path,
337
+ "sourceUrl": source_url,
338
+ "dataSize": int(data_size),
339
+ "duration": int(duration),
340
+ "soundType": sound_type or detect_content_type(sound_path or source_url, default="application/octet-stream"),
341
+ }
342
+
343
+
344
+ def build_video_content_for_upload(
345
+ *,
346
+ video_path: str,
347
+ uploaded_url: str,
348
+ duration: int,
349
+ video_type: str = "",
350
+ snapshot_path: str = "",
351
+ snapshot_url: str = "",
352
+ ) -> Dict[str, Any]:
353
+ snapshot_meta: Optional[ImageMetadata] = None
354
+ if snapshot_path:
355
+ snapshot_meta = read_image_metadata(snapshot_path)
356
+ return {
357
+ "videoPath": video_path,
358
+ "videoUUID": uuid.uuid4().hex,
359
+ "videoUrl": uploaded_url,
360
+ "videoType": video_type or detect_content_type(video_path, default="application/octet-stream"),
361
+ "videoSize": os.path.getsize(video_path),
362
+ "duration": int(duration),
363
+ "snapshotPath": snapshot_path,
364
+ "snapshotUUID": uuid.uuid4().hex if snapshot_path else "",
365
+ "snapshotSize": snapshot_meta.size if snapshot_meta else 0,
366
+ "snapshotUrl": snapshot_url,
367
+ "snapshotWidth": snapshot_meta.width if snapshot_meta else 0,
368
+ "snapshotHeight": snapshot_meta.height if snapshot_meta else 0,
369
+ "snapshotType": snapshot_meta.content_type if snapshot_meta else "",
370
+ }
371
+
372
+
373
+ def build_video_content_by_url(
374
+ *,
375
+ video_url: str,
376
+ duration: int,
377
+ video_size: int = 0,
378
+ video_path: str = "",
379
+ video_type: str = "",
380
+ snapshot_url: str = "",
381
+ snapshot_path: str = "",
382
+ snapshot_size: int = 0,
383
+ snapshot_width: int = 0,
384
+ snapshot_height: int = 0,
385
+ snapshot_type: str = "",
386
+ video_uuid: str = "",
387
+ snapshot_uuid: str = "",
388
+ ) -> Dict[str, Any]:
389
+ resolved_snapshot_type = ""
390
+ if snapshot_type:
391
+ resolved_snapshot_type = snapshot_type
392
+ elif snapshot_path or snapshot_url:
393
+ resolved_snapshot_type = detect_content_type(snapshot_path or snapshot_url, default="image/*")
394
+ return {
395
+ "videoPath": video_path,
396
+ "videoUUID": video_uuid or uuid.uuid4().hex,
397
+ "videoUrl": video_url,
398
+ "videoType": video_type or detect_content_type(video_path or video_url, default="application/octet-stream"),
399
+ "videoSize": int(video_size),
400
+ "duration": int(duration),
401
+ "snapshotPath": snapshot_path,
402
+ "snapshotUUID": snapshot_uuid or (uuid.uuid4().hex if snapshot_url or snapshot_path else ""),
403
+ "snapshotSize": int(snapshot_size),
404
+ "snapshotUrl": snapshot_url,
405
+ "snapshotWidth": int(snapshot_width),
406
+ "snapshotHeight": int(snapshot_height),
407
+ "snapshotType": resolved_snapshot_type,
408
+ }
409
+
410
+
411
+ def build_file_content_for_upload(
412
+ *,
413
+ file_path: str,
414
+ uploaded_url: str,
415
+ file_name: str = "",
416
+ file_type: str = "",
417
+ ) -> Dict[str, Any]:
418
+ resolved_name = file_name or os.path.basename(file_path)
419
+ return {
420
+ "filePath": file_path,
421
+ "uuid": uuid.uuid4().hex,
422
+ "sourceUrl": uploaded_url,
423
+ "fileName": resolved_name,
424
+ "fileSize": os.path.getsize(file_path),
425
+ "fileType": file_type or detect_content_type(resolved_name or file_path, default="application/octet-stream"),
426
+ }
427
+
428
+
429
+ def build_file_content_by_url(
430
+ *,
431
+ source_url: str,
432
+ file_name: str,
433
+ file_size: int = 0,
434
+ file_path: str = "",
435
+ file_type: str = "",
436
+ uuid_value: str = "",
437
+ ) -> Dict[str, Any]:
438
+ return {
439
+ "filePath": file_path,
440
+ "uuid": uuid_value or uuid.uuid4().hex,
441
+ "sourceUrl": source_url,
442
+ "fileName": file_name,
443
+ "fileSize": int(file_size),
444
+ "fileType": file_type or detect_content_type(file_name or file_path or source_url, default="application/octet-stream"),
445
+ }
446
+
447
+
448
+ def upload_file(
449
+ *,
450
+ api_addr: str,
451
+ token: str,
452
+ timeout_seconds: float,
453
+ login_user_id: str,
454
+ operation_id_factory: Callable[[], str],
455
+ file_path: str,
456
+ name: str,
457
+ cause: str,
458
+ content_type: str = "",
459
+ make_http_error: Callable[[int, str], Exception],
460
+ make_sdk_error: Callable[[int, str, str], Exception],
461
+ ) -> str:
462
+ resolved_path = os.path.abspath(file_path)
463
+ if not os.path.isfile(resolved_path):
464
+ raise FileNotFoundError(resolved_path)
465
+
466
+ file_size = os.path.getsize(resolved_path)
467
+ if file_size <= 0:
468
+ raise ValueError(f"file is empty: {resolved_path}")
469
+
470
+ part_limit = _post_json(
471
+ api_addr=api_addr,
472
+ route="/object/part_limit",
473
+ payload={},
474
+ token=token,
475
+ operation_id=operation_id_factory(),
476
+ timeout_seconds=timeout_seconds,
477
+ make_http_error=make_http_error,
478
+ make_sdk_error=make_sdk_error,
479
+ )
480
+ part_size = _compute_part_size(
481
+ file_size=file_size,
482
+ min_part_size=int(part_limit.get("minPartSize", 0) or 0),
483
+ max_part_size=int(part_limit.get("maxPartSize", 0) or 0),
484
+ max_num_size=int(part_limit.get("maxNumSize", 0) or 0),
485
+ )
486
+ part_sizes, part_md5s, detected_content_type = _compute_file_parts(resolved_path, part_size)
487
+ effective_content_type = content_type or detected_content_type or detect_content_type(
488
+ resolved_path, default="application/octet-stream"
489
+ )
490
+ part_hash = hashlib.md5(",".join(part_md5s).encode("utf-8")).hexdigest()
491
+ max_parts = min(20, len(part_sizes))
492
+ remote_name = _build_remote_name(login_user_id, name)
493
+
494
+ initiate_resp = _post_json(
495
+ api_addr=api_addr,
496
+ route="/object/initiate_multipart_upload",
497
+ payload={
498
+ "hash": part_hash,
499
+ "size": file_size,
500
+ "partSize": part_size,
501
+ "maxParts": max_parts,
502
+ "cause": cause,
503
+ "name": remote_name,
504
+ "contentType": effective_content_type,
505
+ },
506
+ token=token,
507
+ operation_id=operation_id_factory(),
508
+ timeout_seconds=timeout_seconds,
509
+ make_http_error=make_http_error,
510
+ make_sdk_error=make_sdk_error,
511
+ )
512
+ if initiate_resp.get("upload") is None:
513
+ return str(initiate_resp.get("url", "") or "")
514
+
515
+ upload_info = dict(initiate_resp.get("upload") or {})
516
+ if int(upload_info.get("partSize", 0) or 0) != part_size:
517
+ raise ValueError(
518
+ f"part size mismatch, expect {part_size}, got {int(upload_info.get('partSize', 0) or 0)}"
519
+ )
520
+
521
+ cached_sign = _parse_sign_batch(upload_info.get("sign"))
522
+ cached_at = 0.0
523
+ upload_id = str(upload_info.get("uploadID", "") or "")
524
+ if not upload_id:
525
+ raise ValueError("uploadID is required for multipart upload")
526
+
527
+ with open(resolved_path, "rb") as fp:
528
+ for idx, current_part_size in enumerate(part_sizes, start=1):
529
+ expected_md5 = part_md5s[idx - 1]
530
+ sign = _find_sign(cached_sign, idx, cached_at)
531
+ if sign is None:
532
+ part_numbers = list(range(idx, min(idx + max_parts, len(part_sizes) + 1)))
533
+ auth_sign_resp = _post_json(
534
+ api_addr=api_addr,
535
+ route="/object/auth_sign",
536
+ payload={"uploadID": upload_id, "partNumbers": part_numbers},
537
+ token=token,
538
+ operation_id=operation_id_factory(),
539
+ timeout_seconds=timeout_seconds,
540
+ make_http_error=make_http_error,
541
+ make_sdk_error=make_sdk_error,
542
+ )
543
+ cached_sign = _parse_sign_batch(auth_sign_resp)
544
+ cached_at = time.time()
545
+ sign = _find_sign(cached_sign, idx, cached_at)
546
+ if sign is None:
547
+ raise ValueError(f"missing part sign for part {idx}")
548
+
549
+ chunk = fp.read(current_part_size)
550
+ if len(chunk) != current_part_size:
551
+ raise IOError(
552
+ f"unexpected end of file while reading {resolved_path}, part={idx}, size={current_part_size}"
553
+ )
554
+ chunk_md5 = hashlib.md5(chunk).hexdigest()
555
+ if chunk_md5 != expected_md5:
556
+ raise ValueError(f"md5 mismatch for part {idx}, expect {expected_md5}, got {chunk_md5}")
557
+ _put_signed_part(
558
+ sign=sign,
559
+ batch=cached_sign,
560
+ body=chunk,
561
+ timeout_seconds=timeout_seconds,
562
+ )
563
+
564
+ complete_resp = _post_json(
565
+ api_addr=api_addr,
566
+ route="/object/complete_multipart_upload",
567
+ payload={
568
+ "uploadID": upload_id,
569
+ "parts": part_md5s,
570
+ "name": remote_name,
571
+ "contentType": effective_content_type,
572
+ "cause": cause,
573
+ },
574
+ token=token,
575
+ operation_id=operation_id_factory(),
576
+ timeout_seconds=timeout_seconds,
577
+ make_http_error=make_http_error,
578
+ make_sdk_error=make_sdk_error,
579
+ )
580
+ return str(complete_resp.get("url", "") or "")
581
+
582
+
583
+ def read_image_metadata(path: str) -> ImageMetadata:
584
+ resolved = os.path.abspath(path)
585
+ size = os.path.getsize(resolved)
586
+ with open(resolved, "rb") as fp:
587
+ head = fp.read(64)
588
+ fp.seek(0)
589
+ content_type, width, height = _read_image_size(fp, head)
590
+ return ImageMetadata(width=width, height=height, content_type=content_type, size=size)
591
+
592
+
593
+ def detect_content_type(path_or_name: str, *, default: str = "application/octet-stream") -> str:
594
+ if not path_or_name:
595
+ return default
596
+ lowered = path_or_name.lower()
597
+ if lowered.startswith("http://") or lowered.startswith("https://"):
598
+ parsed = urllib.parse.urlparse(path_or_name)
599
+ guess, _ = mimetypes.guess_type(parsed.path)
600
+ else:
601
+ guess, _ = mimetypes.guess_type(path_or_name)
602
+ return guess or default
603
+
604
+
605
+ def _post_json(
606
+ *,
607
+ api_addr: str,
608
+ route: str,
609
+ payload: Dict[str, Any],
610
+ token: str,
611
+ operation_id: str,
612
+ timeout_seconds: float,
613
+ make_http_error: Callable[[int, str], Exception],
614
+ make_sdk_error: Callable[[int, str, str], Exception],
615
+ ) -> Dict[str, Any]:
616
+ return post_json_data(
617
+ api_addr=api_addr,
618
+ route=route,
619
+ payload=payload,
620
+ token=token,
621
+ operation_id=operation_id,
622
+ timeout_seconds=timeout_seconds,
623
+ make_http_error=make_http_error,
624
+ make_sdk_error=make_sdk_error,
625
+ )
626
+
627
+
628
+ def _compute_part_size(*, file_size: int, min_part_size: int, max_part_size: int, max_num_size: int) -> int:
629
+ if file_size <= 0:
630
+ raise ValueError("file size must be greater than 0")
631
+ if min_part_size <= 0 or max_part_size <= 0 or max_num_size <= 0:
632
+ raise ValueError("invalid part limit response")
633
+ limit = max_part_size * max_num_size
634
+ if file_size > limit:
635
+ raise ValueError(f"file size must be less than {limit} bytes")
636
+ if file_size <= min_part_size * max_num_size:
637
+ return min_part_size
638
+ return int(math.ceil(file_size / max_num_size))
639
+
640
+
641
+ def _compute_file_parts(path: str, part_size: int) -> Tuple[List[int], List[str], str]:
642
+ size = os.path.getsize(path)
643
+ parts = int(math.ceil(size / part_size))
644
+ sizes = [part_size] * parts
645
+ sizes[-1] = size - part_size * (parts - 1)
646
+ hashes: List[str] = []
647
+ detected_type = ""
648
+ with open(path, "rb") as fp:
649
+ for current_size in sizes:
650
+ chunk = fp.read(current_size)
651
+ if len(chunk) != current_size:
652
+ raise IOError(f"failed to read full part from {path}")
653
+ if not detected_type:
654
+ detected_type = detect_content_type(path)
655
+ hashes.append(hashlib.md5(chunk).hexdigest())
656
+ return sizes, hashes, detected_type
657
+
658
+
659
+ def _parse_sign_batch(raw: Any) -> Optional[UploadSignBatch]:
660
+ if not isinstance(raw, dict):
661
+ return None
662
+ parts: List[UploadPartSign] = []
663
+ for item in raw.get("parts", []) or []:
664
+ if not isinstance(item, dict):
665
+ continue
666
+ parts.append(
667
+ UploadPartSign(
668
+ part_number=int(item.get("partNumber", 0) or 0),
669
+ url=str(item.get("url", "") or ""),
670
+ query=tuple(_coerce_key_values(item.get("query"))),
671
+ header=tuple(_coerce_key_values(item.get("header"))),
672
+ )
673
+ )
674
+ return UploadSignBatch(
675
+ url=str(raw.get("url", "") or ""),
676
+ query=tuple(_coerce_key_values(raw.get("query"))),
677
+ header=tuple(_coerce_key_values(raw.get("header"))),
678
+ parts=tuple(parts),
679
+ )
680
+
681
+
682
+ def _coerce_key_values(raw: Any) -> Iterable[Dict[str, Sequence[str]]]:
683
+ if not isinstance(raw, list):
684
+ return ()
685
+ items: List[Dict[str, Sequence[str]]] = []
686
+ for item in raw:
687
+ if not isinstance(item, dict):
688
+ continue
689
+ key = str(item.get("key", "") or "")
690
+ values_raw = item.get("values", [])
691
+ values = tuple(str(v) for v in values_raw) if isinstance(values_raw, list) else ()
692
+ if key:
693
+ items.append({"key": key, "values": values})
694
+ return items
695
+
696
+
697
+ def _find_sign(batch: Optional[UploadSignBatch], part_number: int, cached_at: float) -> Optional[UploadPartSign]:
698
+ if batch is None:
699
+ return None
700
+ if cached_at and time.time() - cached_at > 60.0:
701
+ return None
702
+ for part in batch.parts:
703
+ if part.part_number == part_number:
704
+ return part
705
+ return None
706
+
707
+
708
+ def _put_signed_part(
709
+ *,
710
+ sign: UploadPartSign,
711
+ batch: Optional[UploadSignBatch],
712
+ body: bytes,
713
+ timeout_seconds: float,
714
+ ) -> None:
715
+ raw_url = sign.url or (batch.url if batch is not None else "")
716
+ if not raw_url:
717
+ raise ValueError(f"missing signed upload URL for part {sign.part_number}")
718
+ parsed = urllib.parse.urlparse(raw_url)
719
+ query = urllib.parse.parse_qs(parsed.query, keep_blank_values=True)
720
+ for source in (batch.query if batch is not None else (), sign.query):
721
+ for item in source:
722
+ query[str(item["key"])] = list(item["values"])
723
+ parsed = parsed._replace(query=urllib.parse.urlencode(query, doseq=True))
724
+ req = urllib.request.Request(parsed.geturl(), data=body, method="PUT")
725
+ for source in (batch.header if batch is not None else (), sign.header):
726
+ for item in source:
727
+ for value in item["values"]:
728
+ req.add_header(str(item["key"]), value)
729
+ req.add_header("Content-Length", str(len(body)))
730
+ with urllib.request.urlopen(req, timeout=timeout_seconds) as resp:
731
+ resp.read()
732
+ if resp.status // 100 != 2:
733
+ raise RuntimeError(f"PUT {parsed.geturl()} failed, status={resp.status}")
734
+
735
+
736
+ def _build_remote_name(login_user_id: str, name: str) -> str:
737
+ trimmed = str(name or "").lstrip("/")
738
+ if not trimmed:
739
+ raise ValueError("remote file name is required")
740
+ prefix = f"{login_user_id}/"
741
+ return trimmed if trimmed.startswith(prefix) else prefix + trimmed
742
+
743
+
744
+ def _build_snapshot_picture(uploaded_url: str, source_picture: Dict[str, Any]) -> Dict[str, Any]:
745
+ snapshot = {
746
+ "uuid": source_picture.get("uuid", ""),
747
+ "type": source_picture.get("type", ""),
748
+ "size": source_picture.get("size", 0),
749
+ "width": 640,
750
+ "height": 640,
751
+ "url": uploaded_url,
752
+ }
753
+ try:
754
+ parsed = urllib.parse.urlparse(uploaded_url)
755
+ query = urllib.parse.parse_qs(parsed.query, keep_blank_values=True)
756
+ query["type"] = ["image"]
757
+ query["width"] = ["640"]
758
+ query["height"] = ["640"]
759
+ snapshot["url"] = parsed._replace(query=urllib.parse.urlencode(query, doseq=True)).geturl()
760
+ except Exception:
761
+ snapshot["width"] = int(source_picture.get("width", 0) or 0)
762
+ snapshot["height"] = int(source_picture.get("height", 0) or 0)
763
+ return snapshot
764
+
765
+
766
+ def _read_image_size(fp: Any, head: bytes) -> Tuple[str, int, int]:
767
+ if head.startswith(b"\x89PNG\r\n\x1a\n"):
768
+ fp.seek(16)
769
+ width, height = struct.unpack(">II", fp.read(8))
770
+ return "image/png", int(width), int(height)
771
+ if head.startswith((b"GIF87a", b"GIF89a")):
772
+ width, height = struct.unpack("<HH", head[6:10])
773
+ return "image/gif", int(width), int(height)
774
+ if head[:2] == b"\xff\xd8":
775
+ return _read_jpeg_size(fp)
776
+ if head.startswith(b"BM") and len(head) >= 26:
777
+ width, height = struct.unpack("<II", head[18:26])
778
+ return "image/bmp", int(width), int(abs(height))
779
+ if head.startswith(b"RIFF") and head[8:12] == b"WEBP":
780
+ return _read_webp_size(head)
781
+ raise ValueError("unsupported image format")
782
+
783
+
784
+ def _read_jpeg_size(fp: Any) -> Tuple[str, int, int]:
785
+ fp.seek(2)
786
+ while True:
787
+ marker_start = fp.read(1)
788
+ if not marker_start:
789
+ break
790
+ if marker_start != b"\xff":
791
+ continue
792
+ marker = fp.read(1)
793
+ while marker == b"\xff":
794
+ marker = fp.read(1)
795
+ if not marker or marker in {b"\xd8", b"\xd9"}:
796
+ continue
797
+ length_bytes = fp.read(2)
798
+ if len(length_bytes) != 2:
799
+ break
800
+ block_length = struct.unpack(">H", length_bytes)[0]
801
+ if marker in {
802
+ b"\xc0",
803
+ b"\xc1",
804
+ b"\xc2",
805
+ b"\xc3",
806
+ b"\xc5",
807
+ b"\xc6",
808
+ b"\xc7",
809
+ b"\xc9",
810
+ b"\xca",
811
+ b"\xcb",
812
+ b"\xcd",
813
+ b"\xce",
814
+ b"\xcf",
815
+ }:
816
+ data = fp.read(5)
817
+ if len(data) != 5:
818
+ break
819
+ height, width = struct.unpack(">HH", data[1:5])
820
+ return "image/jpeg", int(width), int(height)
821
+ fp.seek(block_length - 2, os.SEEK_CUR)
822
+ raise ValueError("invalid jpeg image")
823
+
824
+
825
+ def _read_webp_size(head: bytes) -> Tuple[str, int, int]:
826
+ chunk = head[12:16]
827
+ if chunk == b"VP8X" and len(head) >= 30:
828
+ width = 1 + int.from_bytes(head[24:27], "little")
829
+ height = 1 + int.from_bytes(head[27:30], "little")
830
+ return "image/webp", width, height
831
+ if chunk == b"VP8 " and len(head) >= 30:
832
+ width, height = struct.unpack("<HH", head[26:30])
833
+ return "image/webp", int(width & 0x3FFF), int(height & 0x3FFF)
834
+ if chunk == b"VP8L" and len(head) >= 25:
835
+ b0, b1, b2, b3 = head[21:25]
836
+ width = 1 + (((b1 & 0x3F) << 8) | b0)
837
+ height = 1 + (((b3 & 0x0F) << 10) | (b2 << 2) | ((b1 & 0xC0) >> 6))
838
+ return "image/webp", width, height
839
+ raise ValueError("unsupported webp image")