mirror of
https://github.com/Novaloop-AG/element-x-android.git
synced 2026-11-02 21:32:34 +00:00
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:
+13
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
+35
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user