Small changes

This commit is contained in:
Benoit Marty 2020-10-16 16:31:32 +02:00 committed by Benoit Marty
parent b07e7a4a36
commit 0ffe20d863

View File

@ -157,18 +157,21 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
// Get new Bitmap size // Get new Bitmap size
compressedFile.inputStream().use { compressedFile.inputStream().use {
val options = BitmapFactory.Options().apply { inJustDecodeBounds = true } val options = BitmapFactory.Options().apply { inJustDecodeBounds = true }
val bitmap = BitmapFactory.decodeStream(it, null, options) BitmapFactory.decodeStream(it, null, options)
val fileSize = bitmap?.byteCount?.toLong() ?: compressedFile.length()
newAttachmentAttributes = NewAttachmentAttributes( newAttachmentAttributes = NewAttachmentAttributes(
options.outWidth, newWidth = options.outWidth,
options.outHeight, newHeight = options.outHeight,
fileSize newFileSize = compressedFile.length()
) )
} }
} }
.also { filesToDelete.add(it) } .also { filesToDelete.add(it) }
} else { } else {
fileToUpload = workingFile 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) { val contentUploadResponse = if (params.isEncrypted) {
@ -204,11 +207,6 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
Timber.e(failure, "## FileService: Failed to update file cache") Timber.e(failure, "## FileService: Failed to update file cache")
} }
// Fix: OpenableColumns.SIZE may return -1 or 0
if (params.attachment.size <= 0) {
newAttachmentAttributes = newAttachmentAttributes.copy(newFileSize = fileToUpload.length())
}
handleSuccess(params, handleSuccess(params,
contentUploadResponse.contentUri, contentUploadResponse.contentUri,
uploadedFileEncryptedFileInfo, uploadedFileEncryptedFileInfo,
@ -320,7 +318,8 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
val messageContent: MessageContent? = event.asDomain().content.toModel() val messageContent: MessageContent? = event.asDomain().content.toModel()
val updatedContent = when (messageContent) { val updatedContent = when (messageContent) {
is MessageImageContent -> messageContent.update(url, encryptedFileInfo, newAttachmentAttributes) is MessageImageContent -> messageContent.update(url, encryptedFileInfo, newAttachmentAttributes)
is MessageVideoContent -> messageContent.update(url, encryptedFileInfo, thumbnailUrl, thumbnailEncryptedFileInfo) is MessageVideoContent -> messageContent.update(url, encryptedFileInfo, thumbnailUrl, thumbnailEncryptedFileInfo,
newAttachmentAttributes.newFileSize)
is MessageFileContent -> messageContent.update(url, encryptedFileInfo, newAttachmentAttributes.newFileSize) is MessageFileContent -> messageContent.update(url, encryptedFileInfo, newAttachmentAttributes.newFileSize)
is MessageAudioContent -> messageContent.update(url, encryptedFileInfo, newAttachmentAttributes.newFileSize) is MessageAudioContent -> messageContent.update(url, encryptedFileInfo, newAttachmentAttributes.newFileSize)
else -> messageContent else -> messageContent
@ -350,13 +349,15 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
private fun MessageVideoContent.update(url: String, private fun MessageVideoContent.update(url: String,
encryptedFileInfo: EncryptedFileInfo?, encryptedFileInfo: EncryptedFileInfo?,
thumbnailUrl: String?, thumbnailUrl: String?,
thumbnailEncryptedFileInfo: EncryptedFileInfo?): MessageVideoContent { thumbnailEncryptedFileInfo: EncryptedFileInfo?,
size: Long): MessageVideoContent {
return copy( return copy(
url = if (encryptedFileInfo == null) url else null, url = if (encryptedFileInfo == null) url else null,
encryptedFileInfo = encryptedFileInfo?.copy(url = url), encryptedFileInfo = encryptedFileInfo?.copy(url = url),
videoInfo = videoInfo?.copy( videoInfo = videoInfo?.copy(
thumbnailUrl = if (thumbnailEncryptedFileInfo == null) thumbnailUrl else null, thumbnailUrl = if (thumbnailEncryptedFileInfo == null) thumbnailUrl else null,
thumbnailFile = thumbnailEncryptedFileInfo?.copy(url = thumbnailUrl) thumbnailFile = thumbnailEncryptedFileInfo?.copy(url = thumbnailUrl),
size = size
) )
) )
} }