Media: improve media viewer

This commit is contained in:
ganfra 2023-05-12 18:48:24 +02:00
parent 51a7a761b9
commit 80adbd4bd1
20 changed files with 244 additions and 59 deletions

View file

@ -0,0 +1,31 @@
/*
* Copyright (c) 2023 New Vector Ltd
*
* 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 io.element.android.libraries.matrix.impl.media
import io.element.android.libraries.matrix.api.media.MediaFile
import org.matrix.rustcomponents.sdk.MediaFileHandle
class RustMediaFile(private val inner: MediaFileHandle) : MediaFile {
override fun path(): String {
return inner.path()
}
override fun close() {
inner.close()
}
}

View file

@ -16,15 +16,14 @@
package io.element.android.libraries.matrix.impl.media
import android.net.Uri
import io.element.android.libraries.core.coroutine.CoroutineDispatchers
import io.element.android.libraries.matrix.api.media.MatrixMediaLoader
import io.element.android.libraries.matrix.api.media.MatrixMediaSource
import io.element.android.libraries.matrix.api.media.MediaFile
import kotlinx.coroutines.withContext
import org.matrix.rustcomponents.sdk.Client
import org.matrix.rustcomponents.sdk.mediaSourceFromUrl
import org.matrix.rustcomponents.sdk.use
import java.io.File
class RustMediaLoader(
private val dispatchers: CoroutineDispatchers,
@ -59,19 +58,16 @@ class RustMediaLoader(
}
}
override suspend fun loadMediaFile(source: MatrixMediaSource, mimeType: String?): Result<Uri> =
override suspend fun loadMediaFile(source: MatrixMediaSource, mimeType: String?): Result<MediaFile> =
withContext(dispatchers.io) {
runCatching {
mediaSourceFromUrl(source.url).use { mediaSource ->
innerClient.getMediaFile(
val mediaFile = innerClient.getMediaFile(
mediaSource = mediaSource,
mimeType = mimeType ?: "application/octet-stream"
).use {
val file = File(it.path())
Uri.fromFile(file)
}
)
RustMediaFile(mediaFile)
}
}
}
}