Better Kotlin code

This commit is contained in:
Benoit Marty 2020-04-16 17:42:55 +02:00
parent 5652140f5d
commit ac07fb47d7

View file

@ -31,27 +31,33 @@ internal class DefaultContentUrlResolver @Inject constructor(homeServerConnectio
override val uploadUrl = baseUrl + sep + NetworkConstants.URI_API_MEDIA_PREFIX_PATH_R0 + "upload" override val uploadUrl = baseUrl + sep + NetworkConstants.URI_API_MEDIA_PREFIX_PATH_R0 + "upload"
override fun resolveFullSize(contentUrl: String?): String? { override fun resolveFullSize(contentUrl: String?): String? {
if (contentUrl?.isValidMatrixContentUrl() == true) { return contentUrl
val prefix = NetworkConstants.URI_API_MEDIA_PREFIX_PATH_R0 + "download/" // do not allow non-mxc content URLs
return resolve(contentUrl, prefix) ?.takeIf { it.isValidMatrixContentUrl() }
} ?.let {
// do not allow non-mxc content URLs resolve(
return null contentUrl = it,
prefix = NetworkConstants.URI_API_MEDIA_PREFIX_PATH_R0 + "download/"
)
}
} }
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? {
if (contentUrl?.isValidMatrixContentUrl() == true) { return contentUrl
val prefix = NetworkConstants.URI_API_MEDIA_PREFIX_PATH_R0 + "thumbnail/" // do not allow non-mxc content URLs
val params = "?width=$width&height=$height&method=${method.value}" ?.takeIf { it.isValidMatrixContentUrl() }
return resolve(contentUrl, prefix, params) ?.let {
} resolve(
// do not allow non-mxc content URLs contentUrl = it,
return null prefix = NetworkConstants.URI_API_MEDIA_PREFIX_PATH_R0 + "thumbnail/",
params = "?width=$width&height=$height&method=${method.value}"
)
}
} }
private fun resolve(contentUrl: String, private fun resolve(contentUrl: String,
prefix: String, prefix: String,
params: String? = null): String? { params: String = ""): String? {
var serverAndMediaId = contentUrl.removePrefix(MATRIX_CONTENT_URI_SCHEME) var serverAndMediaId = contentUrl.removePrefix(MATRIX_CONTENT_URI_SCHEME)
val fragmentOffset = serverAndMediaId.indexOf("#") val fragmentOffset = serverAndMediaId.indexOf("#")
var fragment = "" var fragment = ""
@ -60,7 +66,7 @@ internal class DefaultContentUrlResolver @Inject constructor(homeServerConnectio
serverAndMediaId = serverAndMediaId.substring(0, fragmentOffset) serverAndMediaId = serverAndMediaId.substring(0, fragmentOffset)
} }
return baseUrl + sep + prefix + serverAndMediaId + (params ?: "") + fragment return baseUrl + sep + prefix + serverAndMediaId + params + fragment
} }
private fun String.isValidMatrixContentUrl(): Boolean { private fun String.isValidMatrixContentUrl(): Boolean {