tamar-file-hub-client 0.1.3__py3-none-any.whl → 0.1.4__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.
- file_hub_client/client.py +24 -4
- file_hub_client/rpc/async_client.py +31 -4
- file_hub_client/rpc/gen/file_service_pb2.py +23 -5
- file_hub_client/rpc/gen/file_service_pb2_grpc.py +173 -0
- file_hub_client/rpc/protos/file_service.proto +66 -0
- file_hub_client/rpc/sync_client.py +31 -4
- file_hub_client/schemas/__init__.py +10 -0
- file_hub_client/schemas/context.py +171 -160
- file_hub_client/schemas/file.py +44 -0
- file_hub_client/services/file/async_blob_service.py +259 -8
- file_hub_client/services/file/async_file_service.py +217 -0
- file_hub_client/services/file/sync_blob_service.py +260 -8
- file_hub_client/services/file/sync_file_service.py +217 -0
- file_hub_client/utils/__init__.py +14 -0
- file_hub_client/utils/file_utils.py +186 -153
- file_hub_client/utils/ip_detector.py +226 -0
- {tamar_file_hub_client-0.1.3.dist-info → tamar_file_hub_client-0.1.4.dist-info}/METADATA +162 -1
- {tamar_file_hub_client-0.1.3.dist-info → tamar_file_hub_client-0.1.4.dist-info}/RECORD +20 -19
- {tamar_file_hub_client-0.1.3.dist-info → tamar_file_hub_client-0.1.4.dist-info}/WHEEL +0 -0
- {tamar_file_hub_client-0.1.3.dist-info → tamar_file_hub_client-0.1.4.dist-info}/top_level.txt +0 -0
@@ -1,160 +1,171 @@
|
|
1
|
-
"""
|
2
|
-
用户上下文和请求上下文相关的Schema定义
|
3
|
-
"""
|
4
|
-
from dataclasses import dataclass, field
|
5
|
-
from typing import Optional, Dict, Any
|
6
|
-
from datetime import datetime
|
7
|
-
|
8
|
-
from file_hub_client.enums import Role
|
9
|
-
|
10
|
-
|
11
|
-
@dataclass
|
12
|
-
class UserContext:
|
13
|
-
"""
|
14
|
-
用户上下文信息
|
15
|
-
|
16
|
-
包含两大类信息:
|
17
|
-
1. ownership(所有权): org_id, user_id - 表示资源归属
|
18
|
-
2. operator(操作者): actor_id, role - 表示实际操作者(可能是用户、agent或系统)
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
)
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
user_context
|
159
|
-
|
160
|
-
|
1
|
+
"""
|
2
|
+
用户上下文和请求上下文相关的Schema定义
|
3
|
+
"""
|
4
|
+
from dataclasses import dataclass, field
|
5
|
+
from typing import Optional, Dict, Any
|
6
|
+
from datetime import datetime
|
7
|
+
|
8
|
+
from file_hub_client.enums import Role
|
9
|
+
|
10
|
+
|
11
|
+
@dataclass
|
12
|
+
class UserContext:
|
13
|
+
"""
|
14
|
+
用户上下文信息
|
15
|
+
|
16
|
+
包含两大类信息:
|
17
|
+
1. ownership(所有权): org_id, user_id - 表示资源归属
|
18
|
+
2. operator(操作者): actor_id, role - 表示实际操作者(可能是用户、agent或系统)
|
19
|
+
3. request info(请求信息): user_ip - 表示请求来源IP(用于审计和安全)
|
20
|
+
"""
|
21
|
+
# Ownership - 资源所有权信息
|
22
|
+
org_id: str # 组织ID
|
23
|
+
user_id: str # 用户ID
|
24
|
+
|
25
|
+
# Operator - 操作者信息
|
26
|
+
actor_id: Optional[str] = None # 实际操作者ID(如果为空,默认使用user_id)
|
27
|
+
role: Role = Role.ACCOUNT # 操作者角色(ACCOUNT, AGENT, SYSTEM等)
|
28
|
+
|
29
|
+
# Request info - 请求信息
|
30
|
+
user_ip: Optional[str] = None # 用户IP地址(请求来源IP)
|
31
|
+
|
32
|
+
def __post_init__(self):
|
33
|
+
"""初始化后处理,如果actor_id为空,默认使用user_id"""
|
34
|
+
if self.actor_id is None:
|
35
|
+
self.actor_id = self.user_id
|
36
|
+
|
37
|
+
def to_metadata(self) -> Dict[str, str]:
|
38
|
+
"""转换为gRPC metadata格式"""
|
39
|
+
metadata = {
|
40
|
+
'x-org-id': self.org_id,
|
41
|
+
'x-user-id': self.user_id,
|
42
|
+
'x-actor-id': self.actor_id,
|
43
|
+
'x-role': self.role,
|
44
|
+
}
|
45
|
+
|
46
|
+
# 只有当user_ip不为None时才添加x-user-ip
|
47
|
+
if self.user_ip is not None:
|
48
|
+
metadata['x-user-ip'] = self.user_ip
|
49
|
+
|
50
|
+
return metadata
|
51
|
+
|
52
|
+
@classmethod
|
53
|
+
def from_metadata(cls, metadata: Dict[str, str]) -> Optional['UserContext']:
|
54
|
+
"""从metadata中解析用户上下文"""
|
55
|
+
org_id = metadata.get('x-org-id')
|
56
|
+
user_id = metadata.get('x-user-id')
|
57
|
+
|
58
|
+
if not org_id or not user_id:
|
59
|
+
return None
|
60
|
+
|
61
|
+
return cls(
|
62
|
+
org_id=org_id,
|
63
|
+
user_id=user_id,
|
64
|
+
actor_id=metadata.get('x-actor-id'),
|
65
|
+
role=Role(metadata.get('x-role', Role.ACCOUNT)),
|
66
|
+
user_ip=metadata.get('x-user-ip')
|
67
|
+
)
|
68
|
+
|
69
|
+
|
70
|
+
@dataclass
|
71
|
+
class RequestContext:
|
72
|
+
"""
|
73
|
+
请求上下文信息
|
74
|
+
|
75
|
+
包含请求相关的元数据,如客户端信息、请求追踪等
|
76
|
+
"""
|
77
|
+
request_id: Optional[str] = None # 请求ID,用于追踪
|
78
|
+
client_ip: Optional[str] = None # 客户端IP地址
|
79
|
+
client_version: Optional[str] = None # 客户端版本
|
80
|
+
client_type: Optional[str] = None # 客户端类型(web, mobile, desktop, cli等)
|
81
|
+
user_agent: Optional[str] = None # User-Agent信息
|
82
|
+
timestamp: Optional[datetime] = field(default_factory=datetime.now) # 请求时间戳
|
83
|
+
extra: Dict[str, Any] = field(default_factory=dict) # 其他扩展信息
|
84
|
+
|
85
|
+
def to_metadata(self) -> Dict[str, str]:
|
86
|
+
"""转换为gRPC metadata格式"""
|
87
|
+
metadata = {}
|
88
|
+
|
89
|
+
if self.request_id:
|
90
|
+
metadata['x-request-id'] = self.request_id
|
91
|
+
if self.client_ip:
|
92
|
+
metadata['x-client-ip'] = self.client_ip
|
93
|
+
if self.client_version:
|
94
|
+
metadata['x-client-version'] = self.client_version
|
95
|
+
if self.client_type:
|
96
|
+
metadata['x-client-type'] = self.client_type
|
97
|
+
if self.user_agent:
|
98
|
+
metadata['x-user-agent'] = self.user_agent
|
99
|
+
if self.timestamp:
|
100
|
+
metadata['x-timestamp'] = self.timestamp.isoformat()
|
101
|
+
|
102
|
+
# 添加扩展信息
|
103
|
+
for key, value in self.extra.items():
|
104
|
+
metadata[f'x-{key}'] = str(value)
|
105
|
+
|
106
|
+
return metadata
|
107
|
+
|
108
|
+
@classmethod
|
109
|
+
def from_metadata(cls, metadata: Dict[str, str]) -> 'RequestContext':
|
110
|
+
"""从metadata中解析请求上下文"""
|
111
|
+
# 提取标准字段
|
112
|
+
request_id = metadata.get('x-request-id')
|
113
|
+
client_ip = metadata.get('x-client-ip')
|
114
|
+
client_version = metadata.get('x-client-version')
|
115
|
+
client_type = metadata.get('x-client-type')
|
116
|
+
user_agent = metadata.get('x-user-agent')
|
117
|
+
|
118
|
+
# 解析时间戳
|
119
|
+
timestamp = None
|
120
|
+
if 'x-timestamp' in metadata:
|
121
|
+
try:
|
122
|
+
timestamp = datetime.fromisoformat(metadata['x-timestamp'])
|
123
|
+
except:
|
124
|
+
pass
|
125
|
+
|
126
|
+
# 提取扩展字段
|
127
|
+
extra = {}
|
128
|
+
for key, value in metadata.items():
|
129
|
+
if key.startswith('x-') and key not in [
|
130
|
+
'x-request-id', 'x-client-ip', 'x-client-version',
|
131
|
+
'x-client-type', 'x-user-agent', 'x-timestamp',
|
132
|
+
'x-org-id', 'x-user-id', 'x-actor-id', 'x-role'
|
133
|
+
]:
|
134
|
+
extra[key[2:]] = value # 去掉 'x-' 前缀
|
135
|
+
|
136
|
+
return cls(
|
137
|
+
request_id=request_id,
|
138
|
+
client_ip=client_ip,
|
139
|
+
client_version=client_version,
|
140
|
+
client_type=client_type,
|
141
|
+
user_agent=user_agent,
|
142
|
+
timestamp=timestamp,
|
143
|
+
extra=extra
|
144
|
+
)
|
145
|
+
|
146
|
+
|
147
|
+
@dataclass
|
148
|
+
class FullContext:
|
149
|
+
"""完整的上下文信息,包含用户上下文和请求上下文"""
|
150
|
+
user_context: Optional[UserContext] = None
|
151
|
+
request_context: Optional[RequestContext] = None
|
152
|
+
|
153
|
+
def to_metadata(self) -> Dict[str, str]:
|
154
|
+
"""转换为gRPC metadata格式"""
|
155
|
+
metadata = {}
|
156
|
+
|
157
|
+
if self.user_context:
|
158
|
+
metadata.update(self.user_context.to_metadata())
|
159
|
+
|
160
|
+
if self.request_context:
|
161
|
+
metadata.update(self.request_context.to_metadata())
|
162
|
+
|
163
|
+
return metadata
|
164
|
+
|
165
|
+
@classmethod
|
166
|
+
def from_metadata(cls, metadata: Dict[str, str]) -> 'FullContext':
|
167
|
+
"""从metadata中解析完整上下文"""
|
168
|
+
return cls(
|
169
|
+
user_context=UserContext.from_metadata(metadata),
|
170
|
+
request_context=RequestContext.from_metadata(metadata)
|
171
|
+
)
|
file_hub_client/schemas/file.py
CHANGED
@@ -124,3 +124,47 @@ class GetGcsUrlResponse(BaseModel):
|
|
124
124
|
class BatchGcsUrlResponse(BaseModel):
|
125
125
|
"""批量GCS URL响应"""
|
126
126
|
gcs_urls: List[GcsUrlInfo] = Field(default_factory=list, description="GCS URL列表")
|
127
|
+
|
128
|
+
|
129
|
+
# ========= 压缩服务相关模型 =========
|
130
|
+
|
131
|
+
class CompressedVariant(BaseModel):
|
132
|
+
"""压缩变体信息"""
|
133
|
+
variant_name: str = Field(..., description="变体名称")
|
134
|
+
variant_type: str = Field(..., description="变体类型")
|
135
|
+
media_type: str = Field(..., description="媒体类型")
|
136
|
+
width: int = Field(..., description="宽度")
|
137
|
+
height: int = Field(..., description="高度")
|
138
|
+
file_size: int = Field(..., description="文件大小")
|
139
|
+
format: str = Field(..., description="格式")
|
140
|
+
quality: Optional[int] = Field(None, description="质量")
|
141
|
+
duration: Optional[float] = Field(None, description="时长")
|
142
|
+
bitrate: Optional[int] = Field(None, description="比特率")
|
143
|
+
fps: Optional[int] = Field(None, description="帧率")
|
144
|
+
compression_ratio: float = Field(..., description="压缩比")
|
145
|
+
stored_path: str = Field(..., description="存储路径")
|
146
|
+
|
147
|
+
|
148
|
+
class CompressionStatusResponse(BaseModel):
|
149
|
+
"""压缩状态响应"""
|
150
|
+
status: str = Field(..., description="状态: pending, processing, completed, failed")
|
151
|
+
error_message: Optional[str] = Field(None, description="错误信息")
|
152
|
+
variants: List[CompressedVariant] = Field(default_factory=list, description="压缩变体列表")
|
153
|
+
|
154
|
+
|
155
|
+
class GetVariantsResponse(BaseModel):
|
156
|
+
"""获取变体响应"""
|
157
|
+
variants: List[CompressedVariant] = Field(default_factory=list, description="压缩变体列表")
|
158
|
+
|
159
|
+
|
160
|
+
class RecompressionResponse(BaseModel):
|
161
|
+
"""重新压缩响应"""
|
162
|
+
task_id: str = Field(..., description="任务ID")
|
163
|
+
status: str = Field(..., description="状态")
|
164
|
+
|
165
|
+
|
166
|
+
class VariantDownloadUrlResponse(BaseModel):
|
167
|
+
"""变体下载URL响应"""
|
168
|
+
url: str = Field(..., description="下载URL")
|
169
|
+
error: Optional[str] = Field(None, description="错误信息")
|
170
|
+
variant_info: Optional[CompressedVariant] = Field(None, description="变体详细信息")
|