diff --git a/CHANGES.md b/CHANGES.md index 0d3bc066fd..d7c700a0c9 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -36,6 +36,7 @@ Bugfix 🐛: - Be robust against `StrandHogg` task injection - Clear alerts if user sign out - Fix rows are hidden in Textinput (#2234) + - Uploading a file to a room caused it to have a info.size of -1 (#2141) Translations 🗣: - Move store data to `/fastlane/metadata/android` (#812) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/UploadContentWorker.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/UploadContentWorker.kt index 03827b90e2..4a30d6c1e6 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/UploadContentWorker.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/UploadContentWorker.kt @@ -48,10 +48,10 @@ import java.io.File import java.util.UUID import javax.inject.Inject -private data class NewImageAttributes( - val newWidth: Int?, - val newHeight: Int?, - val newFileSize: Int +private data class NewAttachmentAttributes( + val newWidth: Int? = null, + val newHeight: Int? = null, + val newFileSize: Long ) /** @@ -145,7 +145,11 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter return try { val fileToUpload: File - var newImageAttributes: NewImageAttributes? = null + var newAttachmentAttributes = NewAttachmentAttributes( + params.attachment.width?.toInt(), + params.attachment.height?.toInt(), + params.attachment.size + ) if (attachment.type == ContentAttachmentData.Type.IMAGE && params.compressBeforeSending) { fileToUpload = imageCompressor.compress(context, workingFile, MAX_IMAGE_SIZE, MAX_IMAGE_SIZE) @@ -153,18 +157,21 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter // Get new Bitmap size compressedFile.inputStream().use { val options = BitmapFactory.Options().apply { inJustDecodeBounds = true } - val bitmap = BitmapFactory.decodeStream(it, null, options) - val fileSize = bitmap?.byteCount ?: 0 - newImageAttributes = NewImageAttributes( - options.outWidth, - options.outHeight, - fileSize + BitmapFactory.decodeStream(it, null, options) + newAttachmentAttributes = NewAttachmentAttributes( + newWidth = options.outWidth, + newHeight = options.outHeight, + newFileSize = compressedFile.length() ) } } .also { filesToDelete.add(it) } } else { fileToUpload = workingFile + // Fix: OpenableColumns.SIZE may return -1 or 0 + if (params.attachment.size <= 0) { + newAttachmentAttributes = newAttachmentAttributes.copy(newFileSize = fileToUpload.length()) + } } val contentUploadResponse = if (params.isEncrypted) { @@ -205,7 +212,7 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter uploadedFileEncryptedFileInfo, uploadThumbnailResult?.uploadedThumbnailUrl, uploadThumbnailResult?.uploadedThumbnailEncryptedFileInfo, - newImageAttributes) + newAttachmentAttributes) } catch (t: Throwable) { Timber.e(t, "## FileService: ERROR ${t.localizedMessage}") handleFailure(params, t) @@ -285,10 +292,10 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter encryptedFileInfo: EncryptedFileInfo?, thumbnailUrl: String?, thumbnailEncryptedFileInfo: EncryptedFileInfo?, - newImageAttributes: NewImageAttributes?): Result { + newAttachmentAttributes: NewAttachmentAttributes): Result { notifyTracker(params) { contentUploadStateTracker.setSuccess(it) } params.localEchoIds.forEach { - updateEvent(it.eventId, attachmentUrl, encryptedFileInfo, thumbnailUrl, thumbnailEncryptedFileInfo, newImageAttributes) + updateEvent(it.eventId, attachmentUrl, encryptedFileInfo, thumbnailUrl, thumbnailEncryptedFileInfo, newAttachmentAttributes) } val sendParams = MultipleEventSendingDispatcherWorker.Params( @@ -306,14 +313,15 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter encryptedFileInfo: EncryptedFileInfo?, thumbnailUrl: String? = null, thumbnailEncryptedFileInfo: EncryptedFileInfo?, - newImageAttributes: NewImageAttributes?) { + newAttachmentAttributes: NewAttachmentAttributes) { localEchoRepository.updateEcho(eventId) { _, event -> val messageContent: MessageContent? = event.asDomain().content.toModel() val updatedContent = when (messageContent) { - is MessageImageContent -> messageContent.update(url, encryptedFileInfo, newImageAttributes) - is MessageVideoContent -> messageContent.update(url, encryptedFileInfo, thumbnailUrl, thumbnailEncryptedFileInfo) - is MessageFileContent -> messageContent.update(url, encryptedFileInfo) - is MessageAudioContent -> messageContent.update(url, encryptedFileInfo) + is MessageImageContent -> messageContent.update(url, encryptedFileInfo, newAttachmentAttributes) + is MessageVideoContent -> messageContent.update(url, encryptedFileInfo, thumbnailUrl, thumbnailEncryptedFileInfo, + newAttachmentAttributes.newFileSize) + is MessageFileContent -> messageContent.update(url, encryptedFileInfo, newAttachmentAttributes.newFileSize) + is MessageAudioContent -> messageContent.update(url, encryptedFileInfo, newAttachmentAttributes.newFileSize) else -> messageContent } event.content = ContentMapper.map(updatedContent.toContent()) @@ -326,14 +334,14 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter private fun MessageImageContent.update(url: String, encryptedFileInfo: EncryptedFileInfo?, - newImageAttributes: NewImageAttributes?): MessageImageContent { + newAttachmentAttributes: NewAttachmentAttributes?): MessageImageContent { return copy( url = if (encryptedFileInfo == null) url else null, encryptedFileInfo = encryptedFileInfo?.copy(url = url), info = info?.copy( - width = newImageAttributes?.newWidth ?: info.width, - height = newImageAttributes?.newHeight ?: info.height, - size = newImageAttributes?.newFileSize ?: info.size + width = newAttachmentAttributes?.newWidth ?: info.width, + height = newAttachmentAttributes?.newHeight ?: info.height, + size = newAttachmentAttributes?.newFileSize?.toInt() ?: info.size ) ) } @@ -341,30 +349,36 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter private fun MessageVideoContent.update(url: String, encryptedFileInfo: EncryptedFileInfo?, thumbnailUrl: String?, - thumbnailEncryptedFileInfo: EncryptedFileInfo?): MessageVideoContent { + thumbnailEncryptedFileInfo: EncryptedFileInfo?, + size: Long): MessageVideoContent { return copy( url = if (encryptedFileInfo == null) url else null, encryptedFileInfo = encryptedFileInfo?.copy(url = url), videoInfo = videoInfo?.copy( thumbnailUrl = if (thumbnailEncryptedFileInfo == null) thumbnailUrl else null, - thumbnailFile = thumbnailEncryptedFileInfo?.copy(url = thumbnailUrl) + thumbnailFile = thumbnailEncryptedFileInfo?.copy(url = thumbnailUrl), + size = size ) ) } private fun MessageFileContent.update(url: String, - encryptedFileInfo: EncryptedFileInfo?): MessageFileContent { + encryptedFileInfo: EncryptedFileInfo?, + size: Long): MessageFileContent { return copy( url = if (encryptedFileInfo == null) url else null, - encryptedFileInfo = encryptedFileInfo?.copy(url = url) + encryptedFileInfo = encryptedFileInfo?.copy(url = url), + info = info?.copy(size = size) ) } private fun MessageAudioContent.update(url: String, - encryptedFileInfo: EncryptedFileInfo?): MessageAudioContent { + encryptedFileInfo: EncryptedFileInfo?, + size: Long): MessageAudioContent { return copy( url = if (encryptedFileInfo == null) url else null, - encryptedFileInfo = encryptedFileInfo?.copy(url = url) + encryptedFileInfo = encryptedFileInfo?.copy(url = url), + audioInfo = audioInfo?.copy(size = size) ) }