Добавить прямую загрузку по короткой подписи.

Proxy endpoint сохранён. Complete сверяет владельца, размер и checksum, а сравнение трафика API на двух размерах лежит в evidence.
This commit is contained in:
2026-11-09 00:36:52 +03:00
parent 8faf08d8eb
commit 4654de5837
18 changed files with 812 additions and 11 deletions
+53 -1
View File
@@ -1,10 +1,11 @@
import base64
import binascii
import re
from starlette.requests import Request
from starlette.responses import JSONResponse, Response
from app import attachments, cache, config, db, images, rediscli, s3, sessions, store
from app import attachments, cache, config, db, direct, images, rediscli, s3, sessions, store
from app.cursor_codec import decode_cursor, encode_cursor
from app.errors import BASIC_CHALLENGE, BEARER_CHALLENGE, AppError, error_response
from app.passwords import dummy_hash, hash_password, verify_password
@@ -311,6 +312,57 @@ async def upload_attachment(request: Request) -> Response:
return _json(created, rid, 201)
async def begin_direct_upload(request: Request) -> Response:
rid = request.state.request_id
user = await _bearer_user(request)
chat_id = request.path_params["chat_id"]
if validate_uuid(chat_id):
return error_response(400, "invalid chat id", rid)
raw = await request.body()
body, err = parse_json_body(raw)
if err:
return error_response(400, err, rid)
assert body is not None
if require_exact_keys(body, {"filename", "content_type", "size_bytes"}, {"filename", "content_type", "size_bytes"}):
return error_response(400, "invalid upload payload", rid)
if validate_filename(body["filename"]):
return error_response(400, "invalid filename", rid)
content_type = body["content_type"]
if content_type not in ("image/png", "image/jpeg"):
return error_response(415, "unsupported media type", rid)
size = body["size_bytes"]
if isinstance(size, bool) or not isinstance(size, int):
return error_response(400, "invalid size", rid)
if size > config.MAX_UPLOAD_BYTES:
return error_response(413, "file exceeds upload limit", rid)
if size < 1:
return error_response(400, "invalid size", rid)
if await store.load_chat_for_member(chat_id, user["id"]) is None:
return error_response(404, "chat not found", rid)
started = await direct.begin_upload(chat_id, user["id"], body["filename"], content_type, size)
return _json(started, rid, 201)
async def complete_direct_upload(request: Request) -> Response:
rid = request.state.request_id
user = await _bearer_user(request)
upload_id = request.path_params["upload_id"]
if validate_uuid(upload_id):
return error_response(400, "invalid upload id", rid)
raw = await request.body()
body, err = parse_json_body(raw)
if err:
return error_response(400, err, rid)
assert body is not None
if require_exact_keys(body, {"sha256"}, {"sha256"}):
return error_response(400, "invalid upload payload", rid)
digest = body["sha256"]
if not isinstance(digest, str) or re.fullmatch(r"[a-f0-9]{64}", digest) is None:
return error_response(400, "invalid checksum", rid)
created = await direct.complete_upload(upload_id, user["id"], digest)
return _json(created, rid, 201)
async def get_attachment(request: Request) -> Response:
rid = request.state.request_id
user = await _bearer_user(request)