From 79f11ad686d5baadf24ce556da555f31e205fccc Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 17 Dec 2019 17:22:26 +0100 Subject: [PATCH] Prevent crash when mimetype is null --- .../api/session/content/ContentAttachmentData.kt | 2 +- .../api/session/room/model/message/VideoInfo.kt | 2 +- .../crypto/attachments/MXEncryptedAttachments.kt | 2 +- .../internal/session/content/FileUploader.kt | 4 ++-- .../session/room/send/LocalEchoEventFactory.kt | 4 ++-- .../features/attachments/AttachmentsMapper.kt | 15 ++++++++++----- 6 files changed, 17 insertions(+), 12 deletions(-) diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/content/ContentAttachmentData.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/content/ContentAttachmentData.kt index 933657b2fb..0d8ef2c52b 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/content/ContentAttachmentData.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/content/ContentAttachmentData.kt @@ -30,7 +30,7 @@ data class ContentAttachmentData( val exifOrientation: Int = ExifInterface.ORIENTATION_UNDEFINED, val name: String? = null, val path: String, - val mimeType: String, + val mimeType: String?, val type: Type ) : Parcelable { diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/message/VideoInfo.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/message/VideoInfo.kt index a7d4708d33..da247810cb 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/message/VideoInfo.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/api/session/room/model/message/VideoInfo.kt @@ -25,7 +25,7 @@ data class VideoInfo( /** * The mimetype of the video e.g. "video/mp4". */ - @Json(name = "mimetype") val mimeType: String, + @Json(name = "mimetype") val mimeType: String?, /** * The width of the video in pixels. diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/attachments/MXEncryptedAttachments.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/attachments/MXEncryptedAttachments.kt index 5f90b636ac..503bf8e4ff 100755 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/attachments/MXEncryptedAttachments.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/crypto/attachments/MXEncryptedAttachments.kt @@ -49,7 +49,7 @@ object MXEncryptedAttachments { * @param mimetype the mime type * @return the encryption file info */ - fun encryptAttachment(attachmentStream: InputStream, mimetype: String): EncryptionResult { + fun encryptAttachment(attachmentStream: InputStream, mimetype: String?): EncryptionResult { val t0 = System.currentTimeMillis() val secureRandom = SecureRandom() diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/FileUploader.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/FileUploader.kt index 209f03ad9d..2f4e991e62 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/FileUploader.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/content/FileUploader.kt @@ -43,9 +43,9 @@ internal class FileUploader @Inject constructor(@Authenticated suspend fun uploadFile(file: File, filename: String?, - mimeType: String, + mimeType: String?, progressListener: ProgressRequestBody.Listener? = null): ContentUploadResponse { - val uploadBody = file.asRequestBody(mimeType.toMediaTypeOrNull()) + val uploadBody = file.asRequestBody(mimeType?.toMediaTypeOrNull()) return upload(uploadBody, filename, progressListener) } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/LocalEchoEventFactory.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/LocalEchoEventFactory.kt index 0fed1ca6f5..7a935783cf 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/LocalEchoEventFactory.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/send/LocalEchoEventFactory.kt @@ -251,7 +251,7 @@ internal class LocalEchoEventFactory @Inject constructor( type = MessageType.MSGTYPE_AUDIO, body = attachment.name ?: "audio", audioInfo = AudioInfo( - mimeType = attachment.mimeType.takeIf { it.isNotBlank() } ?: "audio/mpeg", + mimeType = attachment.mimeType?.takeIf { it.isNotBlank() } ?: "audio/mpeg", size = attachment.size ), url = attachment.path @@ -264,7 +264,7 @@ internal class LocalEchoEventFactory @Inject constructor( type = MessageType.MSGTYPE_FILE, body = attachment.name ?: "file", info = FileInfo( - mimeType = attachment.mimeType.takeIf { it.isNotBlank() } + mimeType = attachment.mimeType?.takeIf { it.isNotBlank() } ?: "application/octet-stream", size = attachment.size ), diff --git a/vector/src/main/java/im/vector/riotx/features/attachments/AttachmentsMapper.kt b/vector/src/main/java/im/vector/riotx/features/attachments/AttachmentsMapper.kt index 5e843fcdfd..4b51c548a7 100644 --- a/vector/src/main/java/im/vector/riotx/features/attachments/AttachmentsMapper.kt +++ b/vector/src/main/java/im/vector/riotx/features/attachments/AttachmentsMapper.kt @@ -18,6 +18,7 @@ package im.vector.riotx.features.attachments import com.kbeanie.multipicker.api.entity.* import im.vector.matrix.android.api.session.content.ContentAttachmentData +import timber.log.Timber fun ChosenContact.toContactAttachment(): ContactAttachment { return ContactAttachment( @@ -29,6 +30,7 @@ fun ChosenContact.toContactAttachment(): ContactAttachment { } fun ChosenFile.toContentAttachmentData(): ContentAttachmentData { + if (mimeType == null) Timber.w("No mimeType") return ContentAttachmentData( path = originalPath, mimeType = mimeType, @@ -40,6 +42,7 @@ fun ChosenFile.toContentAttachmentData(): ContentAttachmentData { } fun ChosenAudio.toContentAttachmentData(): ContentAttachmentData { + if (mimeType == null) Timber.w("No mimeType") return ContentAttachmentData( path = originalPath, mimeType = mimeType, @@ -51,16 +54,17 @@ fun ChosenAudio.toContentAttachmentData(): ContentAttachmentData { ) } -fun ChosenFile.mapType(): ContentAttachmentData.Type { +private fun ChosenFile.mapType(): ContentAttachmentData.Type { return when { - mimeType.startsWith("image/") -> ContentAttachmentData.Type.IMAGE - mimeType.startsWith("video/") -> ContentAttachmentData.Type.VIDEO - mimeType.startsWith("audio/") -> ContentAttachmentData.Type.AUDIO - else -> ContentAttachmentData.Type.FILE + mimeType?.startsWith("image/") == true -> ContentAttachmentData.Type.IMAGE + mimeType?.startsWith("video/") == true -> ContentAttachmentData.Type.VIDEO + mimeType?.startsWith("audio/") == true -> ContentAttachmentData.Type.AUDIO + else -> ContentAttachmentData.Type.FILE } } fun ChosenImage.toContentAttachmentData(): ContentAttachmentData { + if (mimeType == null) Timber.w("No mimeType") return ContentAttachmentData( path = originalPath, mimeType = mimeType, @@ -75,6 +79,7 @@ fun ChosenImage.toContentAttachmentData(): ContentAttachmentData { } fun ChosenVideo.toContentAttachmentData(): ContentAttachmentData { + if (mimeType == null) Timber.w("No mimeType") return ContentAttachmentData( path = originalPath, mimeType = mimeType,