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

@ -16,8 +16,6 @@
package io.element.android.libraries.matrix.api.media
import android.net.Uri
interface MatrixMediaLoader {
/**
* @param url to fetch the content for.
@ -36,7 +34,7 @@ interface MatrixMediaLoader {
/**
* @param url to fetch the data for.
* @param mimeType: optional mime type
* @return a [Result] of [Uri]. It's the uri of the downloaded file.
* @return a [Result] of [MediaFile]
*/
suspend fun loadMediaFile(source: MatrixMediaSource, mimeType: String?): Result<Uri>
suspend fun loadMediaFile(source: MatrixMediaSource, mimeType: String?): Result<MediaFile>
}

View file

@ -0,0 +1,27 @@
/*
* 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.api.media
import java.io.Closeable
/**
* A wrapper around a media file on the disk.
* When closed the file will be removed from the disk.
*/
interface MediaFile : Closeable {
fun path(): String
}

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)
}
}
}
}

View file

@ -0,0 +1,27 @@
/*
* 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.test.media
import io.element.android.libraries.matrix.api.media.MediaFile
class FakeMediaFile(private val path: String) : MediaFile {
override fun path(): String {
return path
}
override fun close() = Unit
}

View file

@ -16,10 +16,9 @@
package io.element.android.libraries.matrix.test.media
import android.net.Uri
import io.element.android.libraries.matrix.api.media.MatrixMediaLoader
import io.element.android.libraries.matrix.api.media.MatrixMediaSource
import java.io.File
import io.element.android.libraries.matrix.api.media.MediaFile
class FakeMediaLoader : MatrixMediaLoader {
@ -41,11 +40,11 @@ class FakeMediaLoader : MatrixMediaLoader {
}
}
override suspend fun loadMediaFile(source: MatrixMediaSource, mimeType: String?): Result<Uri> {
override suspend fun loadMediaFile(source: MatrixMediaSource, mimeType: String?): Result<MediaFile> {
return if (shouldFail) {
Result.failure(RuntimeException())
} else {
return Result.success(Uri.fromFile(File("path")))
return Result.success(FakeMediaFile(""))
}
}
}