feat(work): add task-scoped attachment intake and durable file delivery

This commit is contained in:
ex_tianzy
2026-11-10 14:19:50 +08:00
parent ce64167627
commit 79c15ee7ce
16 changed files with 680 additions and 12 deletions
+14 -2
View File
@@ -23,19 +23,31 @@ async def rate_limit(redis, bucket: str, limit=60, seconds=60):
class BodyLimitMiddleware:
def __init__(self, app, max_body=262144):
def __init__(self, app, max_body=262144, path_limits=None):
self.app, self.max_body = app, max_body
self.path_limits=path_limits or {}
async def __call__(self, scope, receive, send):
if scope["type"] != "http":
return await self.app(scope, receive, send)
if scope.get('path')in self.path_limits:
# Authenticate upload metadata before buffering its binary body.
class FileBodyTooLarge(Exception):pass
size=0
async def bounded():
nonlocal size
message=await receive();size+=len(message.get('body',b''))
if size>self.path_limits[scope['path']]:raise FileBodyTooLarge()
return message
try:return await self.app(scope,bounded,send)
except FileBodyTooLarge:return await JSONResponse({'detail':'body too large'},status_code=413)(scope,receive,send)
messages, size = [], 0
while True:
message = await receive()
if message["type"] == "http.disconnect":
return
size += len(message.get("body", b""))
if size > self.max_body:
if size > self.path_limits.get(scope.get('path'),self.max_body):
return await JSONResponse({"detail": "body too large"}, status_code=413)(scope, receive, send)
messages.append(message)
if not message.get("more_body", False):