From 7735ca9dab96628d011eb8a8bb5a87806f174271 Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Tue, 10 Sep 2024 20:47:13 -0400 Subject: [PATCH] Fix crash when downloading attachment from S3. --- .../org/thoughtcrime/securesms/database/AttachmentTable.kt | 5 +++-- .../thoughtcrime/securesms/jobs/AttachmentDownloadJob.kt | 6 +++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/database/AttachmentTable.kt b/app/src/main/java/org/thoughtcrime/securesms/database/AttachmentTable.kt index 7fefa077a2..e6056694f5 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/database/AttachmentTable.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/database/AttachmentTable.kt @@ -981,9 +981,10 @@ class AttachmentTable( limitStream.leftoverStream().allMatch { it == 0x00.toByte() } } - val digest = if (paddingAllZeroes) { + // Existing digest may be null for non-user attachments, like things pulled from S3 + val digest = if (existingPlaceholder.remoteDigest != null && paddingAllZeroes) { Log.d(TAG, "[finalizeAttachmentAfterDownload] $attachmentId has all-zero padding. Digest is good.") - existingPlaceholder.remoteDigest!! + existingPlaceholder.remoteDigest } else { Log.w(TAG, "[finalizeAttachmentAfterDownload] $attachmentId has non-zero padding bytes. Recomputing digest.") diff --git a/app/src/main/java/org/thoughtcrime/securesms/jobs/AttachmentDownloadJob.kt b/app/src/main/java/org/thoughtcrime/securesms/jobs/AttachmentDownloadJob.kt index 77ebcdac38..ba72699652 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/jobs/AttachmentDownloadJob.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/jobs/AttachmentDownloadJob.kt @@ -416,11 +416,15 @@ class AttachmentDownloadJob private constructor( if (body.contentLength() > RemoteConfig.maxAttachmentReceiveSizeBytes) { throw MmsException("Attachment too large, failing download") } + + SignalDatabase.attachments.createKeyIvIfNecessary(attachmentId) + val updatedAttachment = SignalDatabase.attachments.getAttachment(attachmentId)!! + SignalDatabase.attachments.finalizeAttachmentAfterDownload( messageId, attachmentId, LimitedInputStream.withoutLimits((body.source() as Source).buffer().inputStream()), - iv = null + iv = updatedAttachment.remoteIv ) } }