Merge pull request #2259 from vector-im/feature/ons/fix_file_info_size

Uploading a file to a room caused it to have a info.size of -1
This commit is contained in:
Benoit Marty 2020-10-16 17:05:26 +02:00 committed by GitHub
commit dc7e0a5f88
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 29 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)
@ -153,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 ?: 0 newAttachmentAttributes = NewAttachmentAttributes(
newImageAttributes = NewImageAttributes( newWidth = options.outWidth,
options.outWidth, newHeight = options.outHeight,
options.outHeight, newFileSize = compressedFile.length()
fileSize
) )
} }
} }
.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) {
@ -205,7 +212,7 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter
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 +292,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 +313,15 @@ 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) newAttachmentAttributes.newFileSize)
is MessageAudioContent -> messageContent.update(url, encryptedFileInfo) is MessageFileContent -> messageContent.update(url, encryptedFileInfo, newAttachmentAttributes.newFileSize)
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 +334,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
) )
) )
} }
@ -341,30 +349,36 @@ 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
) )
) )
} }
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)
) )
} }