Add MediaSource.withCleanUrl method that removes invalid fragment data from MXC urls

We've seen some MXC urls in the wild having some `mxc://foo/bar#auto` fragment suffix, which is invalid, but the URL before that fragment part is valid and can be displayed
This commit is contained in:
Jorge Martín
2026-01-19 09:55:45 +01:00
parent 992a1133c9
commit 7fe0cc4d45
3 changed files with 36 additions and 7 deletions

View File

@@ -9,6 +9,7 @@
package io.element.android.libraries.matrix.api.media
import android.os.Parcelable
import androidx.core.net.toUri
import kotlinx.parcelize.Parcelize
@Parcelize
@@ -22,3 +23,25 @@ data class MediaSource(
*/
val json: String? = null,
) : Parcelable
/**
* Returns a new [MediaSource] with a valid URL.
*/
fun MediaSource.withCleanUrl(): MediaSource {
val uri = this.url.toUri()
if (uri.scheme != "mxc") return this
// We've seen some MXC urls in the wild having some `mxc://foo/bar#auto` fragment suffix, which is invalid
val cleanedUrl = buildString {
append(uri.scheme)
if (!this.endsWith("://")) {
append("://")
}
append(uri.host)
if (uri.path != null) {
append(uri.path)
}
}
return this.copy(url = cleanedUrl)
}