From 0d02a197a5fb8e7d53537d074a6501580cab024b Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 4 May 2021 20:54:59 +0200 Subject: [PATCH 1/2] Remove redundant mimetype (vector-im/element-web#2547) --- changelog.d/3273.feature | 1 + .../room/model/message/MessageAudioContent.kt | 2 +- .../room/model/message/MessageFileContent.kt | 3 +-- .../room/model/message/MessageImageContent.kt | 3 +-- .../room/model/message/MessageStickerContent.kt | 2 +- .../room/model/message/MessageVideoContent.kt | 2 +- .../crypto/attachments/MXEncryptedAttachments.kt | 13 +++++++------ .../internal/crypto/model/rest/EncryptedFileInfo.kt | 6 ------ .../internal/session/content/UploadContentWorker.kt | 4 ++-- 9 files changed, 15 insertions(+), 21 deletions(-) create mode 100644 changelog.d/3273.feature diff --git a/changelog.d/3273.feature b/changelog.d/3273.feature new file mode 100644 index 0000000000..0edc81796b --- /dev/null +++ b/changelog.d/3273.feature @@ -0,0 +1 @@ +Remove redundant mimetype (vector-im/element-web#2547) \ No newline at end of file diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/MessageAudioContent.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/MessageAudioContent.kt index fcf3d73cbe..b4ba5c0a66 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/MessageAudioContent.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/MessageAudioContent.kt @@ -54,5 +54,5 @@ data class MessageAudioContent( ) : MessageWithAttachmentContent { override val mimeType: String? - get() = encryptedFileInfo?.mimetype ?: audioInfo?.mimeType + get() = audioInfo?.mimeType } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/MessageFileContent.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/MessageFileContent.kt index d93f115322..96877b4d9f 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/MessageFileContent.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/MessageFileContent.kt @@ -60,8 +60,7 @@ data class MessageFileContent( ) : MessageWithAttachmentContent { override val mimeType: String? - get() = encryptedFileInfo?.mimetype - ?: info?.mimeType + get() = info?.mimeType ?: MimeTypeMap.getFileExtensionFromUrl(filename ?: body)?.let { extension -> MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension) } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/MessageImageContent.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/MessageImageContent.kt index 73e27b64e3..73fd1eab56 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/MessageImageContent.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/MessageImageContent.kt @@ -20,7 +20,6 @@ import com.squareup.moshi.Json import com.squareup.moshi.JsonClass import org.matrix.android.sdk.api.session.events.model.Content import org.matrix.android.sdk.api.session.room.model.relation.RelationDefaultContent -import org.matrix.android.sdk.api.util.MimeTypes import org.matrix.android.sdk.internal.crypto.model.rest.EncryptedFileInfo @JsonClass(generateAdapter = true) @@ -55,5 +54,5 @@ data class MessageImageContent( @Json(name = "file") override val encryptedFileInfo: EncryptedFileInfo? = null ) : MessageImageInfoContent { override val mimeType: String? - get() = encryptedFileInfo?.mimetype ?: info?.mimeType ?: MimeTypes.Images + get() = info?.mimeType } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/MessageStickerContent.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/MessageStickerContent.kt index 280316d4b5..8e1d4d3d75 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/MessageStickerContent.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/MessageStickerContent.kt @@ -55,5 +55,5 @@ data class MessageStickerContent( @Json(name = "file") override val encryptedFileInfo: EncryptedFileInfo? = null ) : MessageImageInfoContent { override val mimeType: String? - get() = encryptedFileInfo?.mimetype ?: info?.mimeType + get() = info?.mimeType } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/MessageVideoContent.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/MessageVideoContent.kt index b7581c9fbf..3f5d2dab2e 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/MessageVideoContent.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/api/session/room/model/message/MessageVideoContent.kt @@ -53,5 +53,5 @@ data class MessageVideoContent( @Json(name = "file") override val encryptedFileInfo: EncryptedFileInfo? = null ) : MessageWithAttachmentContent { override val mimeType: String? - get() = encryptedFileInfo?.mimetype ?: videoInfo?.mimeType + get() = videoInfo?.mimeType } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/attachments/MXEncryptedAttachments.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/attachments/MXEncryptedAttachments.kt index 5a9852b6db..70730326da 100755 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/attachments/MXEncryptedAttachments.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/attachments/MXEncryptedAttachments.kt @@ -39,7 +39,9 @@ internal object MXEncryptedAttachments { private const val SECRET_KEY_SPEC_ALGORITHM = "AES" private const val MESSAGE_DIGEST_ALGORITHM = "SHA-256" - fun encrypt(clearStream: InputStream, mimetype: String?, outputFile: File, progress: ((current: Int, total: Int) -> Unit)): EncryptedFileInfo { + fun encrypt(clearStream: InputStream, + outputFile: File, + progress: ((current: Int, total: Int) -> Unit)): EncryptedFileInfo { val t0 = System.currentTimeMillis() val secureRandom = SecureRandom() val initVectorBytes = ByteArray(16) { 0.toByte() } @@ -86,7 +88,6 @@ internal object MXEncryptedAttachments { return EncryptedFileInfo( url = null, - mimetype = mimetype, key = EncryptedFileKey( alg = "A256CTR", ext = true, @@ -155,10 +156,9 @@ internal object MXEncryptedAttachments { * Encrypt an attachment stream. * DO NOT USE for big files, it will load all in memory * @param attachmentStream the attachment stream. Will be closed after this method call. - * @param mimetype the mime type * @return the encryption file info */ - fun encryptAttachment(attachmentStream: InputStream, mimetype: String?): EncryptionResult { + fun encryptAttachment(attachmentStream: InputStream): EncryptionResult { val t0 = System.currentTimeMillis() val secureRandom = SecureRandom() @@ -207,7 +207,6 @@ internal object MXEncryptedAttachments { return EncryptionResult( encryptedFileInfo = EncryptedFileInfo( url = null, - mimetype = mimetype, key = EncryptedFileKey( alg = "A256CTR", ext = true, @@ -232,7 +231,9 @@ internal object MXEncryptedAttachments { * @param outputStream the outputStream where the decrypted attachment will be write. * @return true in case of success, false in case of error */ - fun decryptAttachment(attachmentStream: InputStream?, elementToDecrypt: ElementToDecrypt?, outputStream: OutputStream): Boolean { + fun decryptAttachment(attachmentStream: InputStream?, + elementToDecrypt: ElementToDecrypt?, + outputStream: OutputStream): Boolean { // sanity checks if (null == attachmentStream || elementToDecrypt == null) { Timber.e("## decryptAttachment() : null stream") diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/model/rest/EncryptedFileInfo.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/model/rest/EncryptedFileInfo.kt index 0ed6c0dac9..4fc3adb42c 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/model/rest/EncryptedFileInfo.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/model/rest/EncryptedFileInfo.kt @@ -29,12 +29,6 @@ data class EncryptedFileInfo( @Json(name = "url") val url: String? = null, - /** - * Not documented - */ - @Json(name = "mimetype") - val mimetype: String? = null, - /** * Required. A JSON Web Key object. */ 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 237411db53..f14c85cf80 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 @@ -234,7 +234,7 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter .also { filesToDelete.add(it) } uploadedFileEncryptedFileInfo = - MXEncryptedAttachments.encrypt(fileToUpload.inputStream(), attachment.getSafeMimeType(), encryptedFile) { read, total -> + MXEncryptedAttachments.encrypt(fileToUpload.inputStream(), encryptedFile) { read, total -> notifyTracker(params) { contentUploadStateTracker.setEncrypting(it, read.toLong(), total.toLong()) } @@ -315,7 +315,7 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter if (params.isEncrypted) { Timber.v("Encrypt thumbnail") notifyTracker(params) { contentUploadStateTracker.setEncryptingThumbnail(it) } - val encryptionResult = MXEncryptedAttachments.encryptAttachment(thumbnailData.bytes.inputStream(), thumbnailData.mimeType) + val encryptionResult = MXEncryptedAttachments.encryptAttachment(thumbnailData.bytes.inputStream()) val contentUploadResponse = fileUploader.uploadByteArray( byteArray = encryptionResult.encryptedByteArray, filename = null, From 7f70a03f41a15fc015c2dea7a8515bb60b1dbf55 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 4 May 2021 21:00:06 +0200 Subject: [PATCH 2/2] Use interface field --- .../sdk/internal/session/room/send/DefaultSendService.kt | 6 +++--- .../home/room/detail/timeline/factory/MessageItemFactory.kt | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/DefaultSendService.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/DefaultSendService.kt index 449189e6b5..092fec4d72 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/DefaultSendService.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/send/DefaultSendService.kt @@ -142,7 +142,7 @@ internal class DefaultSendService @AssistedInject constructor( // The image has not yet been sent val attachmentData = ContentAttachmentData( size = messageContent.info!!.size, - mimeType = messageContent.info.mimeType!!, + mimeType = messageContent.mimeType, width = messageContent.info.width.toLong(), height = messageContent.info.height.toLong(), name = messageContent.body, @@ -169,7 +169,7 @@ internal class DefaultSendService @AssistedInject constructor( is MessageFileContent -> { val attachmentData = ContentAttachmentData( size = messageContent.info!!.size, - mimeType = messageContent.info.mimeType!!, + mimeType = messageContent.mimeType, name = messageContent.getFileName(), queryUri = Uri.parse(messageContent.url), type = ContentAttachmentData.Type.FILE @@ -181,7 +181,7 @@ internal class DefaultSendService @AssistedInject constructor( val attachmentData = ContentAttachmentData( size = messageContent.audioInfo?.size ?: 0, duration = messageContent.audioInfo?.duration?.toLong() ?: 0L, - mimeType = messageContent.audioInfo?.mimeType, + mimeType = messageContent.mimeType, name = messageContent.body, queryUri = Uri.parse(messageContent.url), type = ContentAttachmentData.Type.AUDIO diff --git a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/factory/MessageItemFactory.kt b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/factory/MessageItemFactory.kt index b1bac3378e..4767269833 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/factory/MessageItemFactory.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/detail/timeline/factory/MessageItemFactory.kt @@ -314,7 +314,7 @@ class MessageItemFactory @Inject constructor( .leftGuideline(avatarSizeProvider.leftGuideline) .imageContentRenderer(imageContentRenderer) .contentUploadStateTrackerBinder(contentUploadStateTrackerBinder) - .playable(messageContent.info?.mimeType == MimeTypes.Gif) + .playable(messageContent.mimeType == MimeTypes.Gif) .highlighted(highlight) .mediaData(data) .apply {