modify: 接口转发

This commit is contained in:
2026-08-11 14:47:19 +08:00
parent 74d115d2bb
commit 3e1c568d04
2 changed files with 34 additions and 26 deletions
+22 -14
View File
@@ -38,31 +38,37 @@ logger = logging.getLogger(__name__)
router = APIRouter(prefix="/aibox_sophon_adapter", tags=["aibox_sophon_adapter"])
class _FileStream(httpx.AsyncByteStream):
class _FileStream:
"""
异步文件流 — 用于流式上传视频,避免一次性将整个文件读入内存。
文件流 — 用于流式上传视频,避免一次性将整个文件读入内存。
以 1MB 分块异步读取文件,供 httpx multipart 上传使用。
注意: httpx 0.28 的 multipart 上传(FileField)要求文件对象提供同步的
read()/seek() 方法,httpx 内部以 64KB 分块同步读取;AsyncByteStream 只能
作为整个请求体 content= 使用,无法与 multipart 字段共存。因此这里提供
同步文件接口,配合同步上下文管理器 with 使用(本地磁盘小分块读取,
内存占用依然有界)。
"""
_CHUNK_SIZE = 1024 * 1024
def __init__(self, file_path: str):
self._file_path = file_path
self._file = None
async def __aenter__(self) -> "_FileStream":
self._file = await aiofiles.open(self._file_path, "rb")
def __enter__(self) -> "_FileStream":
self._file = open(self._file_path, "rb")
return self
async def __aexit__(self, exc_type, exc, tb) -> None:
def __exit__(self, exc_type, exc, tb) -> None:
if self._file is not None:
await self._file.close()
self._file.close()
self._file = None
async def __aiter__(self):
assert self._file is not None, "必须在 async with 上下文内使用"
while chunk := await self._file.read(self._CHUNK_SIZE):
yield chunk
def read(self, size: int = -1) -> bytes:
assert self._file is not None, "必须在 with 上下文内使用"
return self._file.read(size)
def seek(self, offset: int, whence: int = 0) -> int:
assert self._file is not None, "必须在 with 上下文内使用"
return self._file.seek(offset, whence)
# ============================================================
@@ -206,7 +212,9 @@ async def _upload_video_to_external(
video_path = os.path.join(video_dir_path, video_files[0])
try:
async with _FileStream(video_path) as video_stream:
# httpx 0.28 的 multipart 以同步 read() 流式读取文件(64KB 分块),
# 因此这里用同步 with 打开文件流
with _FileStream(video_path) as video_stream:
resp = await http_client.post(
video_url,
data={