Send a concrete mime type for media shared with a wildcard one

Some apps share an image with the mime type image/* rather than a
concrete one. That value was carried straight into the uploaded event, so
the message reached the room with an unusable media type and bridges
rejected it as unsupported.

Resolve a wildcard subtype to a default for its top level type before
processing the upload, reusing the mapping the download path already
applies so the SDK can pick the right extension.
This commit is contained in:
hayaksi1
2026-08-17 04:01:05 +03:00
parent 23b75c3423
commit 4c5baf8d60
5 changed files with 76 additions and 18 deletions
@@ -63,4 +63,17 @@ object MimeTypes {
val subType = components.last()
return subType.isNotBlank() && subType != "*"
}
/**
* A mime type with a wildcard subtype is a filter, not a media type, so replace it with a
* concrete default for its top level type.
*/
fun String?.withDefaultSubtype(): String = when {
this == null -> OctetStream
hasSubtype(this) -> this
this == Images -> Jpeg
this == Videos -> Mp4
this == Audio -> Mp3
else -> OctetStream
}
}
@@ -0,0 +1,35 @@
/*
* Copyright (c) 2026 Element Creations Ltd.
*
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
* Please see LICENSE files in the repository root for full details.
*/
package io.element.android.libraries.core.mimetype
import com.google.common.truth.Truth.assertThat
import io.element.android.libraries.core.mimetype.MimeTypes.withDefaultSubtype
import org.junit.Test
class MimeTypesTest {
@Test
fun `withDefaultSubtype keeps a mime type that already has a subtype`() {
assertThat(MimeTypes.Png.withDefaultSubtype()).isEqualTo(MimeTypes.Png)
assertThat(MimeTypes.Mp4.withDefaultSubtype()).isEqualTo(MimeTypes.Mp4)
assertThat(MimeTypes.Pdf.withDefaultSubtype()).isEqualTo(MimeTypes.Pdf)
}
@Test
fun `withDefaultSubtype replaces a wildcard subtype with a concrete one`() {
assertThat(MimeTypes.Images.withDefaultSubtype()).isEqualTo(MimeTypes.Jpeg)
assertThat(MimeTypes.Videos.withDefaultSubtype()).isEqualTo(MimeTypes.Mp4)
assertThat(MimeTypes.Audio.withDefaultSubtype()).isEqualTo(MimeTypes.Mp3)
}
@Test
fun `withDefaultSubtype falls back to octet stream`() {
assertThat(MimeTypes.Any.withDefaultSubtype()).isEqualTo(MimeTypes.OctetStream)
assertThat("text/*".withDefaultSubtype()).isEqualTo(MimeTypes.OctetStream)
assertThat(null.withDefaultSubtype()).isEqualTo(MimeTypes.OctetStream)
}
}