Create extension `String.isMxcUrl()`

This commit is contained in:
Benoit Marty 2021-10-04 16:24:57 +02:00
parent 5076369173
commit 068c9393f1
6 changed files with 40 additions and 14 deletions

1
changelog.d/4158.removal Normal file
View File

@ -0,0 +1 @@
Create extension `String.isMxcUrl()`

View File

@ -0,0 +1,26 @@
/*
* Copyright 2021 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.matrix.android.sdk.api
/**
* This class contains pattern to match Matrix Url, aka mxc urls
*/
object MatrixUrls {
const val MATRIX_CONTENT_URI_SCHEME = "mxc://"
fun String.isMxcUrl() = startsWith(MATRIX_CONTENT_URI_SCHEME)
}

View File

@ -16,14 +16,14 @@
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.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.internal.network.NetworkConstants import org.matrix.android.sdk.internal.network.NetworkConstants
import org.matrix.android.sdk.internal.util.ensureTrailingSlash import org.matrix.android.sdk.internal.util.ensureTrailingSlash
import javax.inject.Inject import javax.inject.Inject
private const val MATRIX_CONTENT_URI_SCHEME = "mxc://"
internal class DefaultContentUrlResolver @Inject constructor(homeServerConnectionConfig: HomeServerConnectionConfig) : ContentUrlResolver { internal class DefaultContentUrlResolver @Inject constructor(homeServerConnectionConfig: HomeServerConnectionConfig) : ContentUrlResolver {
private val baseUrl = homeServerConnectionConfig.homeServerUriBase.toString().ensureTrailingSlash() private val baseUrl = homeServerConnectionConfig.homeServerUriBase.toString().ensureTrailingSlash()
@ -33,7 +33,7 @@ internal class DefaultContentUrlResolver @Inject constructor(homeServerConnectio
override fun resolveFullSize(contentUrl: String?): String? { override fun resolveFullSize(contentUrl: String?): String? {
return contentUrl return contentUrl
// do not allow non-mxc content URLs // do not allow non-mxc content URLs
?.takeIf { it.isValidMatrixContentUrl() } ?.takeIf { it.isMxcUrl() }
?.let { ?.let {
resolve( resolve(
contentUrl = it, contentUrl = it,
@ -45,7 +45,7 @@ internal class DefaultContentUrlResolver @Inject constructor(homeServerConnectio
override fun resolveThumbnail(contentUrl: String?, width: Int, height: Int, method: ContentUrlResolver.ThumbnailMethod): String? { override fun resolveThumbnail(contentUrl: String?, width: Int, height: Int, method: ContentUrlResolver.ThumbnailMethod): String? {
return contentUrl return contentUrl
// do not allow non-mxc content URLs // do not allow non-mxc content URLs
?.takeIf { it.isValidMatrixContentUrl() } ?.takeIf { it.isMxcUrl() }
?.let { ?.let {
resolve( resolve(
contentUrl = it, contentUrl = it,
@ -58,7 +58,7 @@ internal class DefaultContentUrlResolver @Inject constructor(homeServerConnectio
private fun resolve(contentUrl: String, private fun resolve(contentUrl: String,
prefix: String, prefix: String,
params: String = ""): String? { params: String = ""): String? {
var serverAndMediaId = contentUrl.removePrefix(MATRIX_CONTENT_URI_SCHEME) var serverAndMediaId = contentUrl.removePrefix(MatrixUrls.MATRIX_CONTENT_URI_SCHEME)
val fragmentOffset = serverAndMediaId.indexOf("#") val fragmentOffset = serverAndMediaId.indexOf("#")
var fragment = "" var fragment = ""
if (fragmentOffset >= 0) { if (fragmentOffset >= 0) {
@ -68,8 +68,4 @@ internal class DefaultContentUrlResolver @Inject constructor(homeServerConnectio
return baseUrl + prefix + serverAndMediaId + params + fragment return baseUrl + prefix + serverAndMediaId + params + fragment
} }
private fun String.isValidMatrixContentUrl(): Boolean {
return startsWith(MATRIX_CONTENT_URI_SCHEME)
}
} }

View File

@ -25,6 +25,7 @@ import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject import dagger.assisted.AssistedInject
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import org.matrix.android.sdk.api.MatrixUrls.isMxcUrl
import org.matrix.android.sdk.api.session.content.ContentAttachmentData import org.matrix.android.sdk.api.session.content.ContentAttachmentData
import org.matrix.android.sdk.api.session.events.model.Event import org.matrix.android.sdk.api.session.events.model.Event
import org.matrix.android.sdk.api.session.events.model.isAttachmentMessage import org.matrix.android.sdk.api.session.events.model.isAttachmentMessage
@ -130,7 +131,7 @@ internal class DefaultSendService @AssistedInject constructor(
val messageContent = clearContent?.toModel<MessageContent>() as? MessageWithAttachmentContent ?: return NoOpCancellable val messageContent = clearContent?.toModel<MessageContent>() as? MessageWithAttachmentContent ?: return NoOpCancellable
val url = messageContent.getFileUrl() ?: return NoOpCancellable val url = messageContent.getFileUrl() ?: return NoOpCancellable
if (url.startsWith("mxc://")) { if (url.isMxcUrl()) {
// We need to resend only the message as the attachment is ok // We need to resend only the message as the attachment is ok
localEchoRepository.updateSendState(localEcho.eventId, roomId, SendState.UNSENT) localEchoRepository.updateSendState(localEcho.eventId, roomId, SendState.UNSENT)
return sendEvent(localEcho.root) return sendEvent(localEcho.root)

View File

@ -20,6 +20,7 @@ import im.vector.app.core.extensions.isEmail
import im.vector.app.core.extensions.isMsisdn import im.vector.app.core.extensions.isMsisdn
import im.vector.app.features.home.room.detail.ChatEffect import im.vector.app.features.home.room.detail.ChatEffect
import org.matrix.android.sdk.api.MatrixPatterns import org.matrix.android.sdk.api.MatrixPatterns
import org.matrix.android.sdk.api.MatrixUrls.isMxcUrl
import org.matrix.android.sdk.api.session.identity.ThreePid import org.matrix.android.sdk.api.session.identity.ThreePid
import timber.log.Timber import timber.log.Timber
@ -92,7 +93,7 @@ object CommandParser {
if (messageParts.size == 2) { if (messageParts.size == 2) {
val url = messageParts[1] val url = messageParts[1]
if (url.startsWith("mxc://")) { if (url.isMxcUrl()) {
ParsedCommand.ChangeRoomAvatar(url) ParsedCommand.ChangeRoomAvatar(url)
} else { } else {
ParsedCommand.ErrorSyntax(Command.ROOM_AVATAR) ParsedCommand.ErrorSyntax(Command.ROOM_AVATAR)
@ -105,7 +106,7 @@ object CommandParser {
if (messageParts.size == 2) { if (messageParts.size == 2) {
val url = messageParts[1] val url = messageParts[1]
if (url.startsWith("mxc://")) { if (url.isMxcUrl()) {
ParsedCommand.ChangeAvatarForRoom(url) ParsedCommand.ChangeAvatarForRoom(url)
} else { } else {
ParsedCommand.ErrorSyntax(Command.CHANGE_AVATAR_FOR_ROOM) ParsedCommand.ErrorSyntax(Command.CHANGE_AVATAR_FOR_ROOM)

View File

@ -69,6 +69,7 @@ import im.vector.app.features.media.ImageContentRenderer
import im.vector.app.features.media.VideoContentRenderer import im.vector.app.features.media.VideoContentRenderer
import me.gujun.android.span.span import me.gujun.android.span.span
import org.commonmark.node.Document import org.commonmark.node.Document
import org.matrix.android.sdk.api.MatrixUrls.isMxcUrl
import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.events.model.RelationType import org.matrix.android.sdk.api.session.events.model.RelationType
import org.matrix.android.sdk.api.session.events.model.toModel import org.matrix.android.sdk.api.session.events.model.toModel
@ -213,7 +214,7 @@ class MessageItemFactory @Inject constructor(
if (informationData.sentByMe && !informationData.sendState.isSent()) { if (informationData.sentByMe && !informationData.sendState.isSent()) {
it it
} else { } else {
it.takeIf { it.startsWith("mxc://") } it.takeIf { it.isMxcUrl() }
} }
} ?: "" } ?: ""
return MessageFileItem_() return MessageFileItem_()
@ -244,7 +245,7 @@ class MessageItemFactory @Inject constructor(
if (informationData.sentByMe && !informationData.sendState.isSent()) { if (informationData.sentByMe && !informationData.sendState.isSent()) {
it it
} else { } else {
it.takeIf { it.startsWith("mxc://") } it.takeIf { it.isMxcUrl() }
} }
} ?: "" } ?: ""