Skip to content

fix(media): stop re-downloading attachments the device already holds - #15

Merged
jsfraz merged 1 commit into
developmentfrom
fix/media-redownload
Nov 17, 2026
Merged

jsfraz merged 1 commit into
developmentfrom
fix/media-redownload

Conversation

@jsfraz

@jsfraz jsfraz commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Symptom: an attachment received while the chat is open shows fine; reopening the chat replaces it with "Stažení selhalo" / "Příloha neexistuje".

Root cause: every chat open fetches the newest archive page (_loadHistory) and runs it through the same decrypt-and-store path as a live message (Utils.storeHistoryPage → _storeMessages). That path collected a media reference for every message in the page — including ones already stored and downloaded — and fired downloadMediaMessage for each. The server deletes its copy once delivery is confirmed, so the second attempt returned 404 and updatePrivateMessageMedia wrote that failure onto the healthy message.

Fix:

  • downloadMediaMessage returns early when the attachment is already on disk (CacheUtils.hasMediaLocally), covering the history page, the sync delta and a reconnect redelivery in one place. A message whose download genuinely failed has no local file, so a redelivery still retries it.
  • Rendering heals rows that already carry the stale status: with a local file present, the bubble draws from that file whatever downloadStatus says. The status only decides what to show when there is no file.
  • media_guard_test covers the local-file check for single media, albums, pending placeholders and another conversation.

Verified on the running debug build (Android emulator, via the Flutter MCP toolkit): before the change the open chat had several "Stažení selhalo" nodes; after a hot reload of this code the same conversation shows image nodes (with its caption) and the voice player. Full end-to-end re-open with a live peer was not repeated.

Verification: flutter analyze --no-fatal-infos clean, flutter test 152/152.

Every chat open fetches the newest archive page and hands it to the same
decrypt-and-store path as a live message. That path collected a media
reference for every message in the page, including messages that were
already stored and downloaded. The server deletes its copy once delivery
is confirmed, so the second attempt came back 404 and
`updatePrivateMessageMedia` wrote that failure onto the message: a
perfectly good attachment flipped to "download failed" / "expired" the
moment the chat was opened again.

`downloadMediaMessage` now returns early when the message already has the
attachment on disk, which covers the history page, the sync delta and a
redelivery after reconnect in one place.

Rows that already carry the stale status are healed by rendering: a
message with a local file is drawn from that file whatever its status
says, because the file is the truth. The status only decides what to show
when there is no file.

Verified on the running debug build: the same conversation that showed
"Stažení selhalo" for several bubbles rendered its images and voice player
after a hot reload.
@jsfraz
jsfraz merged commit 61190f6 into development Nov 17, 2026
1 check passed
@jsfraz
jsfraz deleted the fix/media-redownload branch November 17, 2026 23:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant