docs/attachment-viewing.md
Stored attachment viewing and saving
Stored attachments are never fetched automatically. Persisted rows have local MIME-kind icons: the existing image icon, a folder for directories, a file with T for text, and a file with ? for other types. Tap image/folder/T to preview; the question-mark is an accessible informational icon with the filename, type, and no-in-app-preview meaning, not an active preview button. These are generic icons, not remotely fetched thumbnails; preview controls have 48dp accessible touch targets. Rows show concise MIME/metadata size; omitted size is shown as unknown.
The heading overflow retains Preview attachment (image/text), Open in external app (other files), and Save attachment as… (non-directory files). Directories offer only Preview listing as an attachment action: no Save, external Open, browsing, or other directory operations. These export actions are also rejected by the controller before fetching bytes or creating an export URI. Long-press remains an alternative, not a required step for previews. Metadata copy, Details, and existing canonical turn actions remain available. Live/unpersisted image metadata and undecodable metadata do not invent retrievable attachment IDs. Remote paths are never opened, resolved, or rendered.
Turn.Attachment.index is the original zero-based meta_files ordinal. Invalid
individual metadata entries are skipped without renumbering later entries; missing
size is a display-only zero. Exact meta_files remains in Details/copy.
Preview formats
MIME classification is centralized as image/directory/text/unknown; matching is
case-insensitive and ignores MIME parameters. text/directory is classified before
generic text. A folder preview displays the persisted listing bytes obtained by
the original meta_files ordinal through get_attachment_chunk; it never fetches
or browses a filesystem path. Filename suffixes and arbitrary chat markup do not
declare attachments or directories.
Text preview accepts MIME-declared text/*, application/json, application/xml,
application/yaml, application/x-yaml, and application types ending in +json
or +xml. Text and directory-listing content is decoded as strict UTF-8 and
displayed as selectable literal native text. HTML, XML and JSON are not executed
or rendered as markup. There is no WebView and no
content sniffing of arbitrary binary files.
Text and directory-listing previews are limited to 1 MiB. Images use native sampled bitmap decoding, with a ceiling of 100 million source pixels and approximately 2048 pixels per decoded dimension. Image previews open in a bounded, near-window-size viewport. Pinch to zoom from fitted size up to 8×, then drag to pan along axes that overflow the viewport. Smaller axes stay centered; panning is clamped to the image edges. Double-tap to zoom to 2× or return to fitted size. Accessibility actions provide Zoom in, Zoom out, and Reset zoom without a pinch gesture. Viewport size changes reset to fit, and dismissal releases the decoded image/view references. Zoom operates on the sampled preview; Save as preserves the original attachment bytes.
A preview-size failure identifies the preview limit and, for non-directory files, directs the user to Save attachment as…; directory-listing limit errors do not recommend unavailable Save. Neither falls back to an external application. Unsupported codecs or non-UTF-8 ordinary text files can still be saved explicitly. Formats other than declared image/text/directory have no internal renderer, including PDF: use Android's external/default application or save the file.
Transfer limits and lifecycle
- One active attachment operation process-wide; one initial range obtains actual size, then at most two requests are in flight. Each decoded range is at most 98,304 bytes. Offsets, identity, integer types, actual length, constant total size, canonical padded Base64 and exact EOF relation are validated. The RPC dispatcher owns exact request-ID matching. Bytes are written by offset on a serial worker. EOF alone does not complete a transfer; all requested bytes and pending responses must be accounted for.
- 64 MiB maximum attachment and aggregate attachment-cache budget; at most 32 cache entries. Interrupted-process cache remnants count against that budget, rather than silently exceeding it. A cache-full failure needs cache cleanup; attachment handling never accesses key/profile/private credential storage.
- Decoding, range writes, file copies, and cleanup run off main. Only UI and Intent dispatch occur on main. Cancellation marks work stale immediately, drains worker-owned cleanup, and ignores late replies. Already-admitted remote reads cannot be retracted. Canceling a Save copy can leave a partial destination document: no unrelated provider document is deleted or silently overwritten outside the destination the user explicitly selected.
- External opens use only an exact FileProvider URI beneath cache
attachment-open/, read-only temporary grant and ClipData. The provider is not exported and exposes no files-path/root-path. Access and temporary bytes remain until Close access, scope retirement/change, or activity teardown. Saving uses AndroidACTION_CREATE_DOCUMENT; no broad storage permission is needed. - Configuration changes intentionally cancel transfers; neither pending transfers nor URI access is restored. Results from a canceled picker are discarded; a new operation cannot start until that picker returns, preventing a stale result from saving a newer attachment. Merely backgrounding the activity to open the Save picker or external viewer does not cancel the operation.
AttachmentController owns UI/lifecycle handling and invokes
turns().attachmentChunk(...) through the existing asynchronous Session.request
callback path. It does not block on library futures. AttachmentRange validates
the returned byte range independently of display metadata.