Merge pull request #1244 from vector-im/feature/media_path

Fix download and upload media path
This commit is contained in:
Benoit Marty 2020-04-16 17:58:08 +02:00 committed by GitHub
commit 7961423556
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 33 deletions

View File

@ -7,7 +7,6 @@ Features ✨:
- Cross-Signing | Verify new session from existing session (#1134)
- Cross-Signing | Bootstraping cross signing with 4S from mobile (#985)
Improvements 🙌:
- Verification DM / Handle concurrent .start after .ready (#794)
- Reimplementation of multiple attachment picker
@ -34,6 +33,7 @@ Bugfix 🐛:
- Fix crash when trying to download file without internet connection (#1229)
- Local echo are not updated in timeline (for failed & encrypted states)
- Render image event even if thumbnail_info does not have mimetype defined (#1209)
- Fix issue with media path (#1227)
Translations 🗣:
-

View File

@ -26,6 +26,11 @@ interface ContentUrlResolver {
SCALE("scale")
}
/**
* URL to use to upload content
*/
val uploadUrl: String
/**
* Get the actual URL for accessing the full-size image of a Matrix media content URI.
*

View File

@ -18,46 +18,46 @@ package im.vector.matrix.android.internal.session.content
import im.vector.matrix.android.api.auth.data.HomeServerConnectionConfig
import im.vector.matrix.android.api.session.content.ContentUrlResolver
import im.vector.matrix.android.internal.network.NetworkConstants
import javax.inject.Inject
private const val MATRIX_CONTENT_URI_SCHEME = "mxc://"
private const val URI_PREFIX_CONTENT_API = "_matrix/media/v1/"
internal class DefaultContentUrlResolver @Inject constructor(private val homeServerConnectionConfig: HomeServerConnectionConfig) : ContentUrlResolver {
internal class DefaultContentUrlResolver @Inject constructor(homeServerConnectionConfig: HomeServerConnectionConfig) : ContentUrlResolver {
companion object {
fun getUploadUrl(homeServerConnectionConfig: HomeServerConnectionConfig): String {
val baseUrl = homeServerConnectionConfig.homeServerUri.toString()
val sep = if (baseUrl.endsWith("/")) "" else "/"
private val baseUrl = homeServerConnectionConfig.homeServerUri.toString()
private val sep = if (baseUrl.endsWith("/")) "" else "/"
return baseUrl + sep + URI_PREFIX_CONTENT_API + "upload"
}
}
override val uploadUrl = baseUrl + sep + NetworkConstants.URI_API_MEDIA_PREFIX_PATH_R0 + "upload"
override fun resolveFullSize(contentUrl: String?): String? {
if (contentUrl?.isValidMatrixContentUrl() == true) {
val baseUrl = homeServerConnectionConfig.homeServerUri.toString()
val prefix = URI_PREFIX_CONTENT_API + "download/"
return resolve(baseUrl, contentUrl, prefix)
}
return null
return contentUrl
// do not allow non-mxc content URLs
?.takeIf { it.isValidMatrixContentUrl() }
?.let {
resolve(
contentUrl = it,
prefix = NetworkConstants.URI_API_MEDIA_PREFIX_PATH_R0 + "download/"
)
}
}
override fun resolveThumbnail(contentUrl: String?, width: Int, height: Int, method: ContentUrlResolver.ThumbnailMethod): String? {
if (contentUrl?.isValidMatrixContentUrl() == true) {
val baseUrl = homeServerConnectionConfig.homeServerUri.toString()
val prefix = URI_PREFIX_CONTENT_API + "thumbnail/"
val params = "?width=$width&height=$height&method=${method.value}"
return resolve(baseUrl, contentUrl, prefix, params)
}
// do not allow non-mxc content URLs
return null
return contentUrl
// do not allow non-mxc content URLs
?.takeIf { it.isValidMatrixContentUrl() }
?.let {
resolve(
contentUrl = it,
prefix = NetworkConstants.URI_API_MEDIA_PREFIX_PATH_R0 + "thumbnail/",
params = "?width=$width&height=$height&method=${method.value}"
)
}
}
private fun resolve(baseUrl: String,
contentUrl: String,
private fun resolve(contentUrl: String,
prefix: String,
params: String? = null): String? {
params: String = ""): String? {
var serverAndMediaId = contentUrl.removePrefix(MATRIX_CONTENT_URI_SCHEME)
val fragmentOffset = serverAndMediaId.indexOf("#")
var fragment = ""
@ -66,9 +66,7 @@ internal class DefaultContentUrlResolver @Inject constructor(private val homeSer
serverAndMediaId = serverAndMediaId.substring(0, fragmentOffset)
}
val sep = if (baseUrl.endsWith("/")) "" else "/"
return baseUrl + sep + prefix + serverAndMediaId + (params ?: "") + fragment
return baseUrl + sep + prefix + serverAndMediaId + params + fragment
}
private fun String.isValidMatrixContentUrl(): Boolean {

View File

@ -17,7 +17,7 @@
package im.vector.matrix.android.internal.session.content
import com.squareup.moshi.Moshi
import im.vector.matrix.android.api.auth.data.SessionParams
import im.vector.matrix.android.api.session.content.ContentUrlResolver
import im.vector.matrix.android.internal.di.Authenticated
import im.vector.matrix.android.internal.network.ProgressRequestBody
import im.vector.matrix.android.internal.network.awaitResponse
@ -37,10 +37,10 @@ import javax.inject.Inject
internal class FileUploader @Inject constructor(@Authenticated
private val okHttpClient: OkHttpClient,
private val eventBus: EventBus,
sessionParams: SessionParams,
contentUrlResolver: ContentUrlResolver,
moshi: Moshi) {
private val uploadUrl = DefaultContentUrlResolver.getUploadUrl(sessionParams.homeServerConnectionConfig)
private val uploadUrl = contentUrlResolver.uploadUrl
private val responseAdapter = moshi.adapter(ContentUploadResponse::class.java)
suspend fun uploadFile(file: File,