mirror of
https://github.com/fawney19/Aether.git
synced 2026-09-02 09:20:22 +08:00
- 删除全部 Python 源码 (src/) 及 Alembic 迁移脚本,归档至 _deprecated_py_src/ - 重构 Rust gateway ai_pipeline: 拆分 planner/finalize 模块,新增 contracts/adaptation 层 - 重组 handlers 模块为 admin/public/proxy/internal/shared 子模块结构 - 新增 executor 模块,引入 Rust 原生数据库迁移 (aether-data/migrations) - 简化 CI/Docker 构建流程,移除 base image 二级构建,统一为单一 app image - 移除 Python 相关基础设施文件 (entrypoint.sh, gunicorn_conf.py, Dockerfile.base)
76 lines
1.6 KiB
Python
76 lines
1.6 KiB
Python
"""
|
||
数据压缩/解压工具
|
||
|
||
提供JSON数据的gzip压缩和解压功能
|
||
"""
|
||
|
||
import gzip
|
||
import json
|
||
from typing import Any
|
||
|
||
|
||
def compress_json(data: Any) -> bytes | None:
|
||
"""
|
||
将JSON数据压缩为gzip格式的字节
|
||
|
||
Args:
|
||
data: 任意可JSON序列化的数据
|
||
|
||
Returns:
|
||
gzip压缩后的字节,如果输入为None则返回None
|
||
"""
|
||
if data is None:
|
||
return None
|
||
|
||
try:
|
||
# 转换为JSON字符串
|
||
json_str = json.dumps(data, ensure_ascii=False)
|
||
# gzip压缩
|
||
compressed = gzip.compress(json_str.encode("utf-8"), compresslevel=6)
|
||
return compressed
|
||
except Exception:
|
||
# 如果压缩失败,返回None
|
||
return None
|
||
|
||
|
||
def decompress_json(compressed_data: bytes | None) -> Any | None:
|
||
"""
|
||
解压gzip格式的字节为JSON数据
|
||
|
||
Args:
|
||
compressed_data: gzip压缩的字节数据
|
||
|
||
Returns:
|
||
解压后的JSON数据,如果输入为None或解压失败则返回None
|
||
"""
|
||
if compressed_data is None:
|
||
return None
|
||
|
||
try:
|
||
# gzip解压
|
||
json_str = gzip.decompress(compressed_data).decode("utf-8")
|
||
# 解析JSON
|
||
data = json.loads(json_str)
|
||
return data
|
||
except Exception:
|
||
# 如果解压失败,返回None
|
||
return None
|
||
|
||
|
||
def get_body_size(data: Any) -> int:
|
||
"""
|
||
获取JSON数据序列化后的字节大小
|
||
|
||
Args:
|
||
data: 任意可JSON序列化的数据
|
||
|
||
Returns:
|
||
字节大小
|
||
"""
|
||
if data is None:
|
||
return 0
|
||
try:
|
||
return len(json.dumps(data, ensure_ascii=False).encode("utf-8"))
|
||
except Exception:
|
||
return 0
|