Use file.length if cursor returns -1 as file size.

Author: Onuray (squashed)
This commit is contained in:
Benoit Marty 2020-10-16 16:10:50 +02:00
parent 94f7cfeb86
commit b07e7a4a36
2 changed files with 36 additions and 22 deletions

View File

@ -36,6 +36,7 @@ Bugfix 🐛:
- Be robust against `StrandHogg` task injection - Be robust against `StrandHogg` task injection
- Clear alerts if user sign out - Clear alerts if user sign out
- Fix rows are hidden in Textinput (#2234) - Fix rows are hidden in Textinput (#2234)
- Uploading a file to a room caused it to have a info.size of -1 (#2141)
Translations 🗣: Translations 🗣:
- Move store data to `/fastlane/metadata/android` (#812) - Move store data to `/fastlane/metadata/android` (#812)

View File

@ -48,10 +48,10 @@ import java.io.File
import java.util.UUID import java.util.UUID
import javax.inject.Inject import javax.inject.Inject
private data class NewImageAttributes( private data class NewAttachmentAttributes(
val newWidth: Int?, val newWidth: Int? = null,
val newHeight: Int?, val newHeight: Int? = null,
val newFileSize: Int val newFileSize: Long
) )
/** /**
@ -145,7 +145,11 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
return try { return try {
val fileToUpload: File 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) { if (attachment.type == ContentAttachmentData.Type.IMAGE && params.compressBeforeSending) {
fileToUpload = imageCompressor.compress(context, workingFile, MAX_IMAGE_SIZE, MAX_IMAGE_SIZE) fileToUpload = imageCompressor.compress(context, workingFile, MAX_IMAGE_SIZE, MAX_IMAGE_SIZE)
@ -154,8 +158,8 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
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) val bitmap = BitmapFactory.decodeStream(it, null, options)
val fileSize = bitmap?.byteCount ?: 0 val fileSize = bitmap?.byteCount?.toLong() ?: compressedFile.length()
newImageAttributes = NewImageAttributes( newAttachmentAttributes = NewAttachmentAttributes(
options.outWidth, options.outWidth,
options.outHeight, options.outHeight,
fileSize fileSize
@ -200,12 +204,17 @@ 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,
uploadThumbnailResult?.uploadedThumbnailUrl, uploadThumbnailResult?.uploadedThumbnailUrl,
uploadThumbnailResult?.uploadedThumbnailEncryptedFileInfo, uploadThumbnailResult?.uploadedThumbnailEncryptedFileInfo,
newImageAttributes) newAttachmentAttributes)
} catch (t: Throwable) { } catch (t: Throwable) {
Timber.e(t, "## FileService: ERROR ${t.localizedMessage}") Timber.e(t, "## FileService: ERROR ${t.localizedMessage}")
handleFailure(params, t) handleFailure(params, t)
@ -285,10 +294,10 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
encryptedFileInfo: EncryptedFileInfo?, encryptedFileInfo: EncryptedFileInfo?,
thumbnailUrl: String?, thumbnailUrl: String?,
thumbnailEncryptedFileInfo: EncryptedFileInfo?, thumbnailEncryptedFileInfo: EncryptedFileInfo?,
newImageAttributes: NewImageAttributes?): Result { newAttachmentAttributes: NewAttachmentAttributes): Result {
notifyTracker(params) { contentUploadStateTracker.setSuccess(it) } notifyTracker(params) { contentUploadStateTracker.setSuccess(it) }
params.localEchoIds.forEach { params.localEchoIds.forEach {
updateEvent(it.eventId, attachmentUrl, encryptedFileInfo, thumbnailUrl, thumbnailEncryptedFileInfo, newImageAttributes) updateEvent(it.eventId, attachmentUrl, encryptedFileInfo, thumbnailUrl, thumbnailEncryptedFileInfo, newAttachmentAttributes)
} }
val sendParams = MultipleEventSendingDispatcherWorker.Params( val sendParams = MultipleEventSendingDispatcherWorker.Params(
@ -306,14 +315,14 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
encryptedFileInfo: EncryptedFileInfo?, encryptedFileInfo: EncryptedFileInfo?,
thumbnailUrl: String? = null, thumbnailUrl: String? = null,
thumbnailEncryptedFileInfo: EncryptedFileInfo?, thumbnailEncryptedFileInfo: EncryptedFileInfo?,
newImageAttributes: NewImageAttributes?) { newAttachmentAttributes: NewAttachmentAttributes) {
localEchoRepository.updateEcho(eventId) { _, event -> localEchoRepository.updateEcho(eventId) { _, event ->
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, newImageAttributes) is MessageImageContent -> messageContent.update(url, encryptedFileInfo, newAttachmentAttributes)
is MessageVideoContent -> messageContent.update(url, encryptedFileInfo, thumbnailUrl, thumbnailEncryptedFileInfo) is MessageVideoContent -> messageContent.update(url, encryptedFileInfo, thumbnailUrl, thumbnailEncryptedFileInfo)
is MessageFileContent -> messageContent.update(url, encryptedFileInfo) is MessageFileContent -> messageContent.update(url, encryptedFileInfo, newAttachmentAttributes.newFileSize)
is MessageAudioContent -> messageContent.update(url, encryptedFileInfo) is MessageAudioContent -> messageContent.update(url, encryptedFileInfo, newAttachmentAttributes.newFileSize)
else -> messageContent else -> messageContent
} }
event.content = ContentMapper.map(updatedContent.toContent()) event.content = ContentMapper.map(updatedContent.toContent())
@ -326,14 +335,14 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
private fun MessageImageContent.update(url: String, private fun MessageImageContent.update(url: String,
encryptedFileInfo: EncryptedFileInfo?, encryptedFileInfo: EncryptedFileInfo?,
newImageAttributes: NewImageAttributes?): MessageImageContent { newAttachmentAttributes: NewAttachmentAttributes?): MessageImageContent {
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),
info = info?.copy( info = info?.copy(
width = newImageAttributes?.newWidth ?: info.width, width = newAttachmentAttributes?.newWidth ?: info.width,
height = newImageAttributes?.newHeight ?: info.height, height = newAttachmentAttributes?.newHeight ?: info.height,
size = newImageAttributes?.newFileSize ?: info.size size = newAttachmentAttributes?.newFileSize?.toInt() ?: info.size
) )
) )
} }
@ -353,18 +362,22 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
} }
private fun MessageFileContent.update(url: String, private fun MessageFileContent.update(url: String,
encryptedFileInfo: EncryptedFileInfo?): MessageFileContent { encryptedFileInfo: EncryptedFileInfo?,
size: Long): MessageFileContent {
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),
info = info?.copy(size = size)
) )
} }
private fun MessageAudioContent.update(url: String, private fun MessageAudioContent.update(url: String,
encryptedFileInfo: EncryptedFileInfo?): MessageAudioContent { encryptedFileInfo: EncryptedFileInfo?,
size: Long): MessageAudioContent {
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),
audioInfo = audioInfo?.copy(size = size)
) )
} }