Merge pull request #6035 from element-hq/fix/remove-fragment-part-in-mxc-urls
Add `MediaSource.safeUrl` for removing invalid fragment part from URLs
This commit is contained in:
@@ -9,6 +9,7 @@
|
||||
package io.element.android.libraries.matrix.api.media
|
||||
|
||||
import android.os.Parcelable
|
||||
import kotlinx.parcelize.IgnoredOnParcel
|
||||
import kotlinx.parcelize.Parcelize
|
||||
|
||||
@Parcelize
|
||||
@@ -16,9 +17,20 @@ data class MediaSource(
|
||||
/**
|
||||
* Url of the media.
|
||||
*/
|
||||
val url: String,
|
||||
private val url: String,
|
||||
/**
|
||||
* This is used to hold data for encrypted media.
|
||||
*/
|
||||
val json: String? = null,
|
||||
) : Parcelable
|
||||
) : Parcelable {
|
||||
/**
|
||||
* A URL with invalid parts (like `#fragment`, if it's an MXC url) removed.
|
||||
*/
|
||||
@IgnoredOnParcel
|
||||
val safeUrl = if (url.startsWith("mxc")) {
|
||||
// We've seen some MXC urls in the wild having some `mxc://foo/bar#auto` fragment suffix, which is invalid
|
||||
url.substringBefore("#")
|
||||
} else {
|
||||
url
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
/*
|
||||
* 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.matrix.api.media
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import io.element.android.libraries.matrix.test.media.aMediaSource
|
||||
import org.junit.Test
|
||||
|
||||
class MediaSourceTest {
|
||||
@Test
|
||||
fun `safeUrl removes the fragment part in MXC urls`() {
|
||||
val mediaSource = aMediaSource(url = "mxc://matrix.org/url#fragment")
|
||||
assertThat(mediaSource.safeUrl).isEqualTo("mxc://matrix.org/url")
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `safeUrl keeps the fragment part in a non-MXC url`() {
|
||||
val mediaSource = aMediaSource(url = "https://matrix.org/url#fragment")
|
||||
assertThat(mediaSource.safeUrl).isEqualTo("https://matrix.org/url#fragment")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user