Media : some improvements and cleaning

This commit is contained in:
ganfra 2023-05-17 17:48:18 +02:00
parent 129ad0be09
commit 62b66b2111
19 changed files with 90 additions and 89 deletions

View file

@ -209,28 +209,27 @@ class RustMatrixRoom(
}
}
override suspend fun sendImage(file: File, thumbnailFile: File, imageInfo: ImageInfo): Result<Unit> {
return runCatching {
override suspend fun sendImage(file: File, thumbnailFile: File, imageInfo: ImageInfo): Result<Unit> = withContext(coroutineDispatchers.io) {
runCatching {
innerRoom.sendImage(file.path, thumbnailFile.path, imageInfo.map())
}
}
override suspend fun sendVideo(file: File, thumbnailFile: File, videoInfo: VideoInfo): Result<Unit> {
return runCatching {
override suspend fun sendVideo(file: File, thumbnailFile: File, videoInfo: VideoInfo): Result<Unit> = withContext(coroutineDispatchers.io) {
runCatching {
innerRoom.sendVideo(file.path, thumbnailFile.path, videoInfo.map())
}
}
override suspend fun sendAudio(file: File, audioInfo: AudioInfo): Result<Unit> {
return runCatching {
override suspend fun sendAudio(file: File, audioInfo: AudioInfo): Result<Unit> = withContext(coroutineDispatchers.io) {
runCatching {
innerRoom.sendAudio(file.path, audioInfo.map())
}
}
override suspend fun sendFile(file: File, fileInfo: FileInfo): Result<Unit> {
return runCatching {
override suspend fun sendFile(file: File, fileInfo: FileInfo): Result<Unit> = withContext(coroutineDispatchers.io) {
runCatching {
innerRoom.sendFile(file.path, fileInfo.map())
}
}
}

View file

@ -0,0 +1,55 @@
/*
* 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.mediaupload.api
import android.net.Uri
import io.element.android.libraries.core.extensions.flatMap
import io.element.android.libraries.matrix.api.room.MatrixRoom
import javax.inject.Inject
class MediaSender @Inject constructor(
private val preProcessor: MediaPreProcessor,
private val room: MatrixRoom,
) {
suspend fun sendMedia(uri: Uri, mimeType: String): Result<Unit> {
return preProcessor
.process(uri, mimeType, deleteOriginal = true)
.flatMap { info ->
room.sendMedia(info)
}
}
private suspend fun MatrixRoom.sendMedia(
info: MediaUploadInfo,
): Result<Unit> {
return when (info) {
is MediaUploadInfo.Image -> {
sendImage(info.file, info.thumbnailInfo.file, info.info)
}
is MediaUploadInfo.Video -> {
sendVideo(info.file, info.thumbnailInfo.file, info.info)
}
is MediaUploadInfo.AnyFile -> {
sendFile(info.file, info.info)
}
else -> error("Unexpected MediaUploadInfo format: $info")
}
}
}

View file

@ -1,39 +0,0 @@
/*
* 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.mediaupload.api
import io.element.android.libraries.matrix.api.room.MatrixRoom
suspend fun MatrixRoom.sendMedia(
info: MediaUploadInfo,
): Result<Unit> {
return when (info) {
is MediaUploadInfo.Image -> {
sendImage(info.file, info.thumbnailInfo.file, info.info)
}
is MediaUploadInfo.Video -> {
sendVideo(info.file, info.thumbnailInfo.file, info.info)
}
is MediaUploadInfo.AnyFile -> {
sendFile(info.file, info.info)
}
else -> error("Unexpected MediaUploadInfo format: $info")
}
}

View file

@ -119,7 +119,9 @@ class AndroidMediaPreProcessor @Inject constructor(
MediaUploadInfo.AnyFile(file, info)
}
if (deleteOriginal) {
contentResolver.delete(uri, null, null)
tryOrNull {
contentResolver.delete(uri, null, null)
}
}
result
}.mapFailure { MediaPreProcessor.Failure(it) }