Downloads, previews, and device state
This subsystem manages local media around the account archive. It decides when attachments and older history are fetched, places completed downloads in either the account cache or a user folder, keeps images usable on screen without unbounded memory growth, and preserves the linked device state needed to reconnect.
Download destinations
The download destination is the local home for new attachments. The account cache is the safe default. A chosen folder gives people readable files while preserving account and chat boundaries. The worker keeps the archive's folder choices so a chat does not move when names collide or change. (See also Background data services.)
Cache and chosen folders
The media cache stores downloaded attachments when no custom folder is available. A custom destination uses an account folder and a chat folder beneath it. The account folder uses the phone number when known. The chat folder uses a safe chat name and keeps a stable assignment in the archive. Existing files do not move when the destination changes.
References: src/paths.rs• L253-286 src/settings.rs• L496-498 src/…/worker.rs• L3172-3197 src/…/download_folders.rs• L1-20 src/…/download_folders.rs• L64-117
Documents keep their sent names in a chosen folder. Unsafe names become safe file names, and a name that already exists receives a numbered copy instead of replacing the user's file. If the chosen destination cannot be written, the download falls back to the account cache.
References: src/…/worker.rs• L10329-10357 src/…/worker.rs• L12304-12352 src/…/worker.rs• L405-465
Download safety
Downloads are written as hidden staging files and published only after the transfer completes. This keeps a partial or failed transfer from looking like a complete attachment. Automatic downloads accept files up to 64 MiB; a reader-requested download can use the manual path without that automatic limit.
References: src/model.rs• L1811-1840 src/…/worker.rs• L273-331 src/…/worker.rs• L467-505
The worker removes stale staging files only inside the account's real download directories. It does not follow symbolic links during this cleanup. This limits recovery work to the selected account and prevents a bad folder entry from turning cleanup into an unrelated file walk.
References: src/…/worker.rs• L7634-7642 src/…/download_folders.rs• L24-56 src/…/download_folders.rs• L59-62
Attachment fetching
Attachment fetching joins message metadata with a local path and a transient download state. The chat view can start a download as media enters view, while the viewer can request a missing item directly. The archive stores the resulting path, so later views can use the local file without fetching it again.
References: src/model.rs• L1814-1830 src/model.rs• L1916-1922 src/…/worker.rs• L10329-10432 src/…/image_preview.rs• L753-835
Automatic and manual downloads
The account setting controls whether ordinary media and documents download when visible. Stickers still download when visible, and every automatic request checks the shared size limit. A manual request from the message or media viewer can fetch an item that is not yet local.
References: src/settings.rs• L456-458 src/…/settings.rs• L434-448 src/…/conversation.rs• L10466-10482 src/…/image_preview.rs• L452-472
The visible state tells the reader whether an item is idle, downloading, or failed. A missing attachment keeps its own thumbnail or type icon while it waits. A failed item shows the worker's error and offers a retry rather than silently showing another file.
References: src/model.rs• L1916-1922 src/…/image_preview.rs• L753-835 src/…/image_preview.rs• L838-978
Media recovery
Startup checks repair the link between archived messages and local files. If an archived path is missing, the worker first looks for a matching cache copy. If none exists, it clears the path so the message can offer a new download. Clearing the media cache follows the same model: messages remain, chosen download folders remain, and attachments download again when opened.
References: src/…/worker.rs• L2549-2576 src/…/worker.rs• L2579-2654 src/…/dialogs.rs• L1600-1625 src/app.rs• L3692-3722
History prefetch
History prefetch is a separate background service from attachment download. It asks the phone for older message pages so recent work is ready before the reader scrolls there, while limiting phone wake-ups and avoiding a single long chat taking the whole session.
When prefetch is enabled, the service selects the open chat first, then pinned chats, then the ten most recent unpinned and unarchived chats. It asks one chat at a time, waits between requests, stops after a small per-session page budget, and backs off a chat that does not answer. A reconnect starts a new opportunity to ask again.
References: src/…/settings.rs• L442-448 src/…/prefetch.rs• L7-16 src/…/prefetch.rs• L48-128 src/…/prefetch.rs• L151-248 src/…/prefetch.rs• L312-346
The service yields to a reader's direct history request. This keeps background work from delaying the page the reader is actively waiting for. Its recovery behavior is part of the broader sync and reconnect model described in Event sync and recovery.
References: src/…/prefetch.rs• L67-90 src/…/prefetch.rs• L175-183 src/…/prefetch.rs• L884-902
Image caching
Image caching has two layers with different jobs. Stored message thumbnails provide small previews without a download. The UI image cache keeps bytes, decoded pixels, and textures available while images are visible, then releases images that have left the resident view.
The cache tracks images by URI and marks them as used by the current frame. It keeps a bounded resident window of scrolled-away images, never releases images drawn in the current frame, and registers bytes again if a released image returns. The info panel can release its own thumbnails immediately when it closes.
References: src/image_cache.rs• L1-23 src/image_cache.rs• L35-63 src/image_cache.rs• L84-125 src/…/info.rs• L52-84
Message media uses the best local source available: an archived thumbnail first, then a downloaded file, then a type placeholder. The same rule supports the chat, the media list, and the full-window viewer, so a missing file does not get replaced by an unrelated image.
References: src/model.rs• L1150-1178 src/…/info.rs• L1594-1633 src/…/image_preview.rs• L1088-1107
Media previews
The media viewer gives a downloaded photo or playable clip a full-window view with album navigation. It can also show an undownloaded item through its stored thumbnail and offer the download in place. A new album item starts fitted to the window, while zoom and pan apply only to the current item.
References: src/image_preview.rs• L15-45 src/image_preview.rs• L138-160 src/…/image_preview.rs• L32-60 src/…/image_preview.rs• L562-750
The viewer keeps the message identity and chat identity with the displayed file. That lets it return to the source message, save an existing file, or request the missing attachment without losing the place in the album.
References: src/image_preview.rs• L218-248 src/…/image_preview.rs• L226-251 src/…/image_preview.rs• L429-492
Link previews
Link previews turn the first web link in an outgoing message into a small card with title, description, and an optional image. The composer shows loading, ready, or dismissed state. When the message is sent, the worker attaches the preview only if it still matches the first link in the text, so editing the text cannot send a stale card.
References: src/…/conversation.rs• L2533-2618 src/…/worker.rs• L9572-9604 src/model.rs• L619-632
Preview fetching
The fetcher reads page metadata from the HTML head and gives priority to Open Graph data, then Twitter card data, then standard page fields. It resolves a page image against the page URL, makes a bounded thumbnail, and returns no preview when the page or image cannot be safely read.
References: src/…/link_preview.rs• L26-103 src/…/link_preview.rs• L352-391 src/…/link_preview.rs• L462-541
Only public HTTP and HTTPS hosts are eligible. Local names and private, loopback, link-local, multicast, reserved, or embedded-local addresses are refused, including during redirects and image fetches. The fetch also has time, page-size, image-size, redirect, and image-decode limits.
References: src/…/link_preview.rs• L277-350 src/…/link_preview.rs• L398-459 src/…/link_preview.rs• L462-541
Preview privacy setting
The phone's “Disable link previews” setting controls both fetching and sending. Until the phone has provided a trustworthy answer, previews stay off. A complete snapshot with no setting establishes the default of previews enabled; an incomplete snapshot does not. Unlinking clears the stored answer so the next account must be checked again.
References: src/…/link_preview_setting.rs• L1-27 src/…/link_preview_setting.rs• L29-96 src/…/worker.rs• L4552-4567 src/…/worker.rs• L1572-1580
Channel pictures
Channel pictures use channel metadata rather than the profile-picture lookup used for contacts and groups. Followed channels are loaded as one list after connection, so early picture requests wait for that list instead of causing one request per channel. A channel outside the followed list reads its own metadata.
References: src/…/channel_pictures.rs• L1-9 src/…/channel_pictures.rs• L52-83
Picture files have separate preview and full-size roles, with fallback when only one size exists. A changed picture drops both cached sizes and fetches them again. A recent cached picture can remain visible while a fresh lookup runs, which keeps the UI stable during network delays.
References: src/…/channel_pictures.rs• L16-49 src/…/channel_pictures.rs• L55-67 src/…/worker.rs• L10667-10742 src/paths.rs• L253-286
Device-state persistence
Device state is the local identity and session data that lets the linked desktop reconnect to WhatsApp. The worker opens the per-account session.db through the protocol library before starting the connection. The archive and media cache are separate: deleting the session database unlinks the account, while clearing media leaves message history in place.
References: src/paths.rs• L232-241 src/paths.rs• L330-345 src/…/worker.rs• L3108-3124
The device store has a compatibility guard for older ZapFast versions. After the library migration, the store keeps the legacy message-secret column needed by the older version, without reading or rewriting message secrets. This supports a safe return to the older version while leaving ownership of the device schema and secrets with the protocol library.
References: src/…/device_store.rs• L1-21 src/…/device_store.rs• L23-41
Device state follows the account. Each account has its own state and cache directories, and unlinking or removing an account deletes its local chat and device data for that account. This separation prevents one account's session or media from being reused by another.
References: src/paths.rs• L306-357 src/…/dialogs.rs• L1585-1597 src/…/dialogs.rs• L1628-1640
Change checklist
When these files or sections are changed, remember to consider these:
- Keep the archive's stored media paths aligned with cache relocation and missing-file recovery.
- Keep custom-folder naming, document-name sanitization, and collision numbering consistent between path selection and final publication.
- Preserve the rule that automatic size checks apply before streaming, while manual downloads remain distinct.
- Keep link-preview setting recovery ordered with snapshot replay so an absent setting is not mistaken for a stale or failed read.
- Keep preview fetch checks applied to the page, redirects, and preview image rather than only to the typed URL.
- Keep channel-picture invalidation tied to both preview and full-size cache files.
- Keep session database compatibility changes isolated from message-secret reads and account directory ownership.
Important failure modes
| Trigger | Consequence | Guard |
|---|---|---|
| A download destination is unavailable | The attachment cannot land in the chosen folder | Fall back to the account media cache |
| A transfer fails or is cancelled | A partial file could be mistaken for a complete attachment | Stage in a hidden .part file and publish only on success |
| An automatic attachment exceeds the limit | Background work could consume excessive storage or bandwidth | Reject it before and during the streamed download |
| A media path points to a deleted file | The message would show a dead local path | Relocate from the cache or clear the path for re-download |
| The phone's link-preview setting is unknown | A fetch could violate the phone's privacy choice | Keep previews disabled until a stored or complete snapshot answer exists |
| A preview URL resolves to a local or private address | Fetching could reach the user's network | Reject local hosts and checked local IP ranges at every fetch stage |
| A channel picture changes | The UI could keep showing an old cached picture | Remove both cached sizes and refresh from channel metadata |
| The session store schema changes across versions | A prior version may fail to open or write the device store | Preserve the legacy column without touching stored secrets |
Key files
| File | Why to read it |
|---|---|
| src/…/worker.rs | Main worker flow for download paths, attachment publication, media recovery, link-preview composition, avatar refresh, and session startup |
| src/…/download_folders.rs | Stable account and chat folder rules for chosen download destinations |
| src/…/prefetch.rs | Background older-history queue, pacing, limits, and reconnect behavior |
| src/…/link_preview.rs | Link metadata fetch, thumbnail creation, host safety, and fetch limits |
| src/…/link_preview_setting.rs | Privacy-setting recovery and the safe default for previews |
| src/…/channel_pictures.rs | Channel-specific picture metadata and cache refresh behavior |
| src/…/device_store.rs | Device database compatibility boundary |
| src/image_cache.rs | Bounded in-memory and GPU image residency |
| src/image_preview.rs | Media viewer state and album rules |
| src/…/image_preview.rs | User-facing full-window media flow and download actions |
| src/paths.rs | Account state, media cache, avatar cache, and session database locations |
| src/settings.rs | Persistent download, prefetch, and custom-folder settings |
| docs/background-data-services.md | Parent page for the background worker services |
| docs/event-sync-and-recovery.md | Related page for sync, reconnect, and recovery behavior |