Improve and use MatrixUrls

This commit is contained in:
Benoit Marty 2021-11-17 11:24:57 +01:00
parent adea1db87a
commit c0af8214a6
3 changed files with 17 additions and 4 deletions

View File

@ -20,7 +20,18 @@ package org.matrix.android.sdk.api
* This class contains pattern to match Matrix Url, aka mxc urls * This class contains pattern to match Matrix Url, aka mxc urls
*/ */
object MatrixUrls { object MatrixUrls {
/**
* "mxc" scheme, including "://". So "mxc://"
*/
const val MATRIX_CONTENT_URI_SCHEME = "mxc://" const val MATRIX_CONTENT_URI_SCHEME = "mxc://"
/**
* Return true if the String starts with "mxc://"
*/
fun String.isMxcUrl() = startsWith(MATRIX_CONTENT_URI_SCHEME) fun String.isMxcUrl() = startsWith(MATRIX_CONTENT_URI_SCHEME)
/**
* Remove the "mxc://" prefix. No op if the String is not a Mxc URL
*/
fun String.removeMxcPrefix() = removePrefix(MATRIX_CONTENT_URI_SCHEME)
} }

View File

@ -16,8 +16,8 @@
package org.matrix.android.sdk.internal.session.content package org.matrix.android.sdk.internal.session.content
import org.matrix.android.sdk.api.MatrixUrls
import org.matrix.android.sdk.api.MatrixUrls.isMxcUrl import org.matrix.android.sdk.api.MatrixUrls.isMxcUrl
import org.matrix.android.sdk.api.MatrixUrls.removeMxcPrefix
import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig
import org.matrix.android.sdk.api.session.content.ContentUrlResolver import org.matrix.android.sdk.api.session.content.ContentUrlResolver
import org.matrix.android.sdk.api.session.contentscanner.ContentScannerService import org.matrix.android.sdk.api.session.contentscanner.ContentScannerService
@ -83,7 +83,7 @@ internal class DefaultContentUrlResolver @Inject constructor(
private fun resolve(contentUrl: String, private fun resolve(contentUrl: String,
toThumbnail: Boolean, toThumbnail: Boolean,
params: String = ""): String { params: String = ""): String {
var serverAndMediaId = contentUrl.removePrefix(MatrixUrls.MATRIX_CONTENT_URI_SCHEME) var serverAndMediaId = contentUrl.removeMxcPrefix()
val apiPath = if (scannerService.isScannerEnabled()) { val apiPath = if (scannerService.isScannerEnabled()) {
NetworkConstants.URI_API_PREFIX_PATH_MEDIA_PROXY_UNSTABLE NetworkConstants.URI_API_PREFIX_PATH_MEDIA_PROXY_UNSTABLE

View File

@ -16,6 +16,8 @@
package org.matrix.android.sdk.internal.session.contentscanner.tasks package org.matrix.android.sdk.internal.session.contentscanner.tasks
import org.matrix.android.sdk.api.MatrixUrls.isMxcUrl
import org.matrix.android.sdk.api.MatrixUrls.removeMxcPrefix
import org.matrix.android.sdk.api.failure.toScanFailure import org.matrix.android.sdk.api.failure.toScanFailure
import org.matrix.android.sdk.api.session.contentscanner.ScanState import org.matrix.android.sdk.api.session.contentscanner.ScanState
import org.matrix.android.sdk.internal.network.executeRequest import org.matrix.android.sdk.internal.network.executeRequest
@ -38,13 +40,13 @@ internal class DefaultScanMediaTask @Inject constructor(
override suspend fun execute(params: ScanMediaTask.Params): ScanResponse { override suspend fun execute(params: ScanMediaTask.Params): ScanResponse {
// "mxc://server.org/QNDpzLopkoQYNikJfoZCQuCXJ" // "mxc://server.org/QNDpzLopkoQYNikJfoZCQuCXJ"
if (!params.mxcUrl.startsWith("mxc://")) { if (!params.mxcUrl.isMxcUrl()) {
throw IllegalAccessException("Invalid mxc url") throw IllegalAccessException("Invalid mxc url")
} }
val scannerUrl = contentScannerStore.getScannerUrl() val scannerUrl = contentScannerStore.getScannerUrl()
contentScannerStore.updateStateForContent(params.mxcUrl, ScanState.IN_PROGRESS, scannerUrl) contentScannerStore.updateStateForContent(params.mxcUrl, ScanState.IN_PROGRESS, scannerUrl)
var serverAndMediaId = params.mxcUrl.removePrefix("mxc://") var serverAndMediaId = params.mxcUrl.removeMxcPrefix()
val fragmentOffset = serverAndMediaId.indexOf("#") val fragmentOffset = serverAndMediaId.indexOf("#")
if (fragmentOffset >= 0) { if (fragmentOffset >= 0) {
serverAndMediaId = serverAndMediaId.substring(0, fragmentOffset) serverAndMediaId = serverAndMediaId.substring(0, fragmentOffset)