Fix SSE route dependency and align architecture docs

This commit is contained in:
ash66
2026-05-18 16:32:42 +08:00
parent 86b9ac806a
commit 3f69cad404
149 changed files with 4786 additions and 5957 deletions

View File

@@ -1,4 +1,4 @@
"""MinIO对象存储客户端 - 文档文件存储"""
"""Provide service-layer logic for minio client."""
from minio import Minio
from minio.error import S3Error
@@ -8,10 +8,12 @@ from io import BytesIO
import os
from app.config.settings import settings
# Keep service responsibilities explicit so downstream behavior stays predictable.
class MinIOClient:
"""MinIO对象存储客户端"""
"""Represent the Min I O Client type."""
def __init__(
self,
@@ -21,16 +23,7 @@ class MinIOClient:
bucket: str = None,
secure: bool = None
):
"""
初始化MinIO客户端
Args:
endpoint: MinIO服务地址
access_key: 访问密钥
secret_key: 秘密密钥
bucket: 存储桶名称
secure: 是否使用HTTPS
"""
"""Initialize the Min I O Client instance."""
self.endpoint = endpoint or settings.minio_endpoint
self.access_key = access_key or settings.minio_access_key
self.secret_key = secret_key or settings.minio_secret_key
@@ -43,7 +36,7 @@ class MinIOClient:
logger.info(f"MinIO客户端配置: {self.endpoint}, bucket={self.bucket}")
def connect(self) -> bool:
"""连接MinIO服务"""
"""Handle connect for the Min I O Client instance."""
try:
self.client = Minio(
self.endpoint,
@@ -60,7 +53,7 @@ class MinIOClient:
return False
def ensure_bucket(self) -> bool:
"""确保存储桶存在"""
"""Handle ensure bucket for the Min I O Client instance."""
if not self.connected:
logger.warning("未连接MinIO请先调用connect()")
return False
@@ -82,17 +75,7 @@ class MinIOClient:
object_name: str,
metadata: Dict[str, Any] = None
) -> bool:
"""
上传本地文件到MinIO
Args:
file_path: 本地文件路径
object_name: MinIO对象名称
metadata: 元数据
Returns:
bool: 是否成功
"""
"""Handle upload file for the Min I O Client instance."""
if not self.connected:
self.connect()
self.ensure_bucket()
@@ -125,18 +108,7 @@ class MinIOClient:
content_type: str = "application/octet-stream",
metadata: Dict[str, Any] = None
) -> bool:
"""
上传字节数据到MinIO
Args:
data: 文件字节数据
object_name: MinIO对象名称
content_type: 内容类型
metadata: 元数据注意MinIO仅支持US-ASCII字符
Returns:
bool: 是否成功
"""
"""Handle upload bytes for the Min I O Client instance."""
if not self.connected:
self.connect()
self.ensure_bucket()
@@ -144,18 +116,18 @@ class MinIOClient:
try:
data_stream = BytesIO(data)
# 处理metadata仅保留ASCII安全字符
# Keep service responsibilities explicit so downstream behavior stays predictable.
safe_metadata = None
if metadata:
safe_metadata = {}
for key, value in metadata.items():
if isinstance(value, str):
# 只保留ASCII字符或转换为安全格式
# Keep service responsibilities explicit so downstream behavior stays predictable.
try:
value.encode('ascii')
safe_metadata[key] = value
except UnicodeEncodeError:
# 中文字符跳过或用占位符
# Keep service responsibilities explicit so downstream behavior stays predictable.
safe_metadata[key] = ""
else:
safe_metadata[key] = str(value)
@@ -181,16 +153,7 @@ class MinIOClient:
object_name: str,
file_path: str
) -> bool:
"""
从MinIO下载文件到本地
Args:
object_name: MinIO对象名称
file_path: 本地保存路径
Returns:
bool: 是否成功
"""
"""Handle download file for the Min I O Client instance."""
if not self.connected:
self.connect()
@@ -212,16 +175,7 @@ class MinIOClient:
object_name: str,
expires: int = 3600
) -> Optional[str]:
"""
获取对象下载URL临时URL
Args:
object_name: MinIO对象名称
expires: URL有效期
Returns:
str: 下载URL
"""
"""Return object url for the Min I O Client instance."""
if not self.connected:
self.connect()
@@ -238,15 +192,7 @@ class MinIOClient:
return None
def get_object_data(self, object_name: str) -> Optional[bytes]:
"""
获取对象数据(字节)
Args:
object_name: MinIO对象名称
Returns:
bytes: 文件数据
"""
"""Return object data for the Min I O Client instance."""
if not self.connected:
self.connect()
@@ -262,15 +208,7 @@ class MinIOClient:
return None
def delete_object(self, object_name: str) -> bool:
"""
删除对象
Args:
object_name: MinIO对象名称
Returns:
bool: 是否成功
"""
"""Delete object for the Min I O Client instance."""
if not self.connected:
self.connect()
@@ -284,15 +222,7 @@ class MinIOClient:
return False
def list_objects(self, prefix: str = "") -> list:
"""
列出存储桶中的对象
Args:
prefix: 对象名称前缀
Returns:
list: 对象列表
"""
"""List objects for the Min I O Client instance."""
if not self.connected:
self.connect()
@@ -305,15 +235,7 @@ class MinIOClient:
return []
def object_exists(self, object_name: str) -> bool:
"""
检查对象是否存在
Args:
object_name: MinIO对象名称
Returns:
bool: 是否存在
"""
"""Handle object exists for the Min I O Client instance."""
if not self.connected:
self.connect()
@@ -325,7 +247,7 @@ class MinIOClient:
return False
def _get_content_type(self, file_path: str) -> str:
"""根据文件扩展名获取Content-Type"""
"""Handle get content type for this module for the Min I O Client instance."""
ext = os.path.splitext(file_path)[1].lower()
content_types = {
'.pdf': 'application/pdf',
@@ -338,13 +260,13 @@ class MinIOClient:
return content_types.get(ext, 'application/octet-stream')
def close(self):
"""关闭连接MinIO客户端无需显式关闭"""
"""Release the resources held by this component."""
self.connected = False
logger.info("MinIO客户端已关闭")
def create_minio_client() -> MinIOClient:
"""便捷函数创建MinIO客户端"""
"""Create minio client."""
client = MinIOClient()
client.connect()
client.ensure_bucket()