Реализовать асинхронную обработку вложений
This commit is contained in:
+31
-11
@@ -67,11 +67,13 @@ class AttachmentService:
|
||||
storage: S3Storage,
|
||||
max_upload_bytes: int,
|
||||
max_image_pixels: int,
|
||||
job_max_attempts: int,
|
||||
) -> None:
|
||||
self._repository = repository
|
||||
self._storage = storage
|
||||
self._max_upload_bytes = max_upload_bytes
|
||||
self._max_image_pixels = max_image_pixels
|
||||
self._job_max_attempts = job_max_attempts
|
||||
|
||||
async def upload(
|
||||
self,
|
||||
@@ -106,28 +108,31 @@ class AttachmentService:
|
||||
self._max_image_pixels,
|
||||
)
|
||||
attachment_id = uuid4()
|
||||
object_key = f'originals/{attachment_id}'
|
||||
await self._storage.put(object_key, source, size, actual_type)
|
||||
job_id = uuid4()
|
||||
staging_key = f'staging/{attachment_id}/original'
|
||||
await self._storage.put(staging_key, source, size, actual_type)
|
||||
now = datetime.now(timezone.utc)
|
||||
try:
|
||||
return await self._repository.create(
|
||||
return await self._repository.create_queued(
|
||||
attachment_id=attachment_id,
|
||||
job_id=job_id,
|
||||
chat_id=chat_id,
|
||||
owner_id=actor.id,
|
||||
filename=filename,
|
||||
object_key=object_key,
|
||||
staging_key=staging_key,
|
||||
content_type=actual_type,
|
||||
size_bytes=size,
|
||||
sha256=digest.hexdigest(),
|
||||
width=width,
|
||||
height=height,
|
||||
max_attempts=self._job_max_attempts,
|
||||
now=now,
|
||||
)
|
||||
except Exception:
|
||||
try:
|
||||
await self._storage.remove(object_key)
|
||||
await self._storage.remove(staging_key)
|
||||
except Unavailable:
|
||||
logger.warning('failed to compensate orphan object key=%s', object_key)
|
||||
logger.warning('failed to compensate staging object key=%s', staging_key)
|
||||
raise
|
||||
|
||||
async def get(self, actor: User, attachment_id: UUID) -> Attachment:
|
||||
@@ -136,12 +141,27 @@ class AttachmentService:
|
||||
raise NotFound('вложение не найдено')
|
||||
return attachment
|
||||
|
||||
async def content(self, actor: User, attachment_id: UUID) -> tuple[Attachment, ObjectStream]:
|
||||
async def content(
|
||||
self, actor: User, attachment_id: UUID, variant: str
|
||||
) -> tuple[Attachment, ObjectStream, str, int]:
|
||||
attachment = await self.get(actor, attachment_id)
|
||||
if attachment.status != 'ready':
|
||||
raise Conflict('оригинал вложения недоступен')
|
||||
raise Conflict('вложение ещё не готово')
|
||||
if variant == 'original':
|
||||
key = attachment.original_key
|
||||
content_type = attachment.content_type
|
||||
size_bytes = attachment.size_bytes
|
||||
else:
|
||||
found = next((item for item in attachment.variants if item.name == variant), None)
|
||||
if found is None:
|
||||
raise NotFound('вариант вложения не найден')
|
||||
key = found.object_key
|
||||
content_type = found.content_type
|
||||
size_bytes = found.size_bytes
|
||||
if key is None:
|
||||
raise Unavailable('объект вложения отсутствует в метаданных')
|
||||
try:
|
||||
stream = await self._storage.get(attachment.object_key)
|
||||
stream = await self._storage.get(key)
|
||||
except FileNotFoundError as error:
|
||||
raise Unavailable('оригинал вложения отсутствует в хранилище') from error
|
||||
return attachment, stream
|
||||
raise Unavailable('объект вложения отсутствует в хранилище') from error
|
||||
return attachment, stream, content_type, size_bytes
|
||||
|
||||
Reference in New Issue
Block a user