# Copyright (c) 2026 Krzysztof Rudnicki. MIT licence. """Turning the attachments on one message into stripped, named files. Split out of pack_add.py at the 250-line cap, along the seam that was already there: that half decides *whether* a command runs and what it answers, this half does the reading, the sniffing and the stripping. Nothing here writes to the album: since the approval queue, the only place an upload goes is ``pending/``, and ``pack_pending`` owns that. A file the album already has, or one that is already waiting, is counted rather than stored again, and counted separately so the reply can say which it was -- "already there" and "already queued" ask for different things of the sender. """ from __future__ import annotations import logging from dataclasses import dataclass, field from typing import TYPE_CHECKING from signal_bot.media_sniff import sniff from signal_bot.pack import MEDIA_DIR from signal_bot.pack_pending import names as queued_names from signal_bot.pack_store import content_name if TYPE_CHECKING: from signal_bot.context import CommandContext from signal_bot.media_strip import Stripper from signal_bot.pack_holder import PackHolder logger = logging.getLogger(__name__) @dataclass(slots=True) class Ingested: """What one message's attachments turned into.""" fresh: dict[str, bytes] = field(default_factory=dict) """Files new to both the album and the queue, by their content name.""" duplicates: int = 0 """How many were already in the album.""" queued: int = 0 """How many were already waiting for approval.""" async def ingest(ctx: CommandContext, holder: PackHolder, stripper: Stripper) -> Ingested: """Fetch, sniff, strip and name every attachment on the message. The album check is against ``media/`` on disk, not the loaded pack's tag table: a file the tagger has not opened yet is in the album all the same, and the point of the check is that no byte-identical copy is stored twice. """ directory = holder.pack.directory waiting = queued_names(directory) result = Ingested() for attachment_id in ctx.message.attachment_ids: payload = await _fetch(ctx, attachment_id) cleaned = _clean(payload, stripper) if cleaned is None: continue name, body = cleaned if (directory / MEDIA_DIR / name).exists(): result.duplicates += 1 elif name in waiting or name in result.fresh: # Twice in one message is "already waiting" too: it is, as of a # moment ago. result.queued += 1 else: result.fresh[name] = body return result async def _fetch(ctx: CommandContext, attachment_id: str) -> bytes | None: """Read one attachment, then delete signal-cli's own copy of it. Deleted whether the upload is accepted or refused: the point is that no copy survives outside the album, and a refused file is exactly the one nobody wants left lying in the container. """ try: payload = await ctx.transport.fetch_attachment(attachment_id) except Exception: logger.exception("could not fetch an attachment") payload = None try: await ctx.transport.delete_attachment(attachment_id) except Exception: logger.exception("could not delete the server-side attachment copy") return payload def _clean(payload: bytes | None, stripper: Stripper) -> tuple[str, bytes] | None: """Sniff and strip one upload, or None when it is not storable.""" if payload is None: return None kind = sniff(payload) if kind is None: logger.warning("refusing an upload whose format is not on the allowlist") return None cleaned = stripper.strip(payload, kind) if cleaned is None: return None return content_name(cleaned, kind.suffix), cleaned