This commit is contained in:
2026-11-10 19:24:33 +03:00
parent c594b88a46
commit b55b230533
23 changed files with 1200 additions and 73 deletions
+15 -7
View File
@@ -7,13 +7,13 @@ import threading
import uuid
from dataclasses import dataclass
from datetime import datetime, timedelta, timezone
from typing import Dict, List, Optional, Sequence, Tuple
from typing import BinaryIO, Dict, List, Optional, Sequence, Set, Tuple
import asyncpg
from .errors import conflict, invalid_request, not_found, unavailable
from .migrate import expected_versions
from .objects import MissingObject, ObjectStore, StorageError
from .objects import ORIGINAL_PREFIX, MissingObject, ObjectStore, StorageError
from .pagination import Key
from .passwords import hash_password, password_matches
@@ -376,7 +376,8 @@ class Store:
owner_id: str,
filename: str,
content_type: str,
body: bytes,
body: BinaryIO,
size_bytes: int,
sha256: str,
) -> Attachment:
chat_uuid = _as_uuid(chat_id, 'Некорректный chat_id')
@@ -384,11 +385,12 @@ class Store:
attachment_id = uuid.uuid4()
# The key is not derived from filename. A crash after PUT and before INSERT leaves an
# unreferenced object; a failed INSERT deletes it. 201 is returned only after both succeed.
object_key = 'originals/%s' % attachment_id
object_key = ORIGINAL_PREFIX + str(attachment_id)
created = self._now()
try:
await asyncio.to_thread(self._objects.put, object_key, body, content_type)
await asyncio.to_thread(self._objects.put_stream, object_key, body, size_bytes, content_type)
except StorageError:
await self._discard_object(object_key)
raise unavailable('Хранилище недоступно') from None
try:
async with self._pool.acquire() as conn:
@@ -405,7 +407,7 @@ class Store:
owner_uuid,
filename,
content_type,
len(body),
size_bytes,
sha256,
object_key,
created,
@@ -420,12 +422,18 @@ class Store:
str(owner_uuid),
filename,
content_type,
len(body),
size_bytes,
sha256,
stamp,
stamp,
)
async def listed_object_keys(self) -> Set[str]:
"""Object keys that have a metadata row. Used by the reconciler, not by the HTTP API."""
async with self._pool.acquire() as conn:
rows = await conn.fetch('SELECT object_key FROM attachments')
return {row['object_key'] for row in rows}
async def find_attachment(self, attachment_id: str, user_id: str) -> Optional[Attachment]:
"""Metadata for a chat member. Missing and foreign attachments are both absent."""
attachment_uuid = _as_uuid(attachment_id, 'Некорректный attachment_id')