From 590d8fc8493ec22dd034c29dd9dfcde3dbce14b3 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 5 May 2021 11:12:06 +0200 Subject: [PATCH] Also test the effect of completeExceptionally() --- .../session/content/UploadContentWorker.kt | 5 ++-- .../session/content/VideoCompressionResult.kt | 1 + .../session/content/VideoCompressor.kt | 29 +++++++++++++++---- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/UploadContentWorker.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/UploadContentWorker.kt index 6a7d265308..822c53cc9b 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/UploadContentWorker.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/UploadContentWorker.kt @@ -210,7 +210,8 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter .also { filesToDelete.add(it) } } VideoCompressionResult.CompressionNotNeeded, - VideoCompressionResult.CompressionCancelled -> { + VideoCompressionResult.CompressionCancelled, + is VideoCompressionResult.CompressionFailed -> { workingFile } } @@ -380,7 +381,7 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter val updatedContent = when (messageContent) { is MessageImageContent -> messageContent.update(url, encryptedFileInfo, newAttachmentAttributes) is MessageVideoContent -> messageContent.update(url, encryptedFileInfo, thumbnailUrl, thumbnailEncryptedFileInfo, newAttachmentAttributes) - is MessageFileContent -> messageContent.update(url, encryptedFileInfo, newAttachmentAttributes.newFileSize) + is MessageFileContent -> messageContent.update(url, encryptedFileInfo, newAttachmentAttributes.newFileSize) is MessageAudioContent -> messageContent.update(url, encryptedFileInfo, newAttachmentAttributes.newFileSize) else -> messageContent } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/VideoCompressionResult.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/VideoCompressionResult.kt index 6d576a79a3..87d5c7e6a3 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/VideoCompressionResult.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/VideoCompressionResult.kt @@ -22,4 +22,5 @@ internal sealed class VideoCompressionResult { data class Success(val compressedFile: File) : VideoCompressionResult() object CompressionNotNeeded : VideoCompressionResult() object CompressionCancelled : VideoCompressionResult() + data class CompressionFailed(val failure: Throwable) : VideoCompressionResult() } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/VideoCompressor.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/VideoCompressor.kt index 297e453087..8749d1f84a 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/VideoCompressor.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/content/VideoCompressor.kt @@ -42,6 +42,7 @@ internal class VideoCompressor @Inject constructor(private val context: Context) progressListener?.onProgress(0, 100) var result: Int = -1 + var failure: Throwable? = null Transcoder.into(destinationFile.path) .addDataSource(videoFile.path) .setListener(object : TranscoderListener { @@ -63,6 +64,7 @@ internal class VideoCompressor @Inject constructor(private val context: Context) override fun onTranscodeFailed(exception: Throwable) { Timber.w(exception, "Compressing: failure") + failure = exception job.completeExceptionally(exception) } }) @@ -70,13 +72,23 @@ internal class VideoCompressor @Inject constructor(private val context: Context) job.join() + // Note: job is also cancelled if completeExceptionally() was called if (job.isCancelled) { - Timber.w("Compressing: Job cancelled") // Delete now the temporary file deleteFile(destinationFile) - // We do not throw a CancellationException, because it's not critical, we will try to send the original file - // Anyway this should never occurs, since we never cancel the return value of transcode() - return VideoCompressionResult.CompressionCancelled + return when (val finalFailure = failure) { + null -> { + // We do not throw a CancellationException, because it's not critical, we will try to send the original file + // Anyway this should never occurs, since we never cancel the return value of transcode() + Timber.w("Compressing: A failure occurred") + VideoCompressionResult.CompressionCancelled + } + else -> { + // Compression failure can also be considered as not critical, but let the caller decide + Timber.w("Compressing: Job cancelled") + VideoCompressionResult.CompressionFailed(finalFailure) + } + } } progressListener?.onProgress(100, 100) @@ -90,8 +102,13 @@ internal class VideoCompressor @Inject constructor(private val context: Context) deleteFile(destinationFile) VideoCompressionResult.CompressionNotNeeded } - else -> - throw IllegalStateException("Unknown result: $result") + else -> { + // Should not happen... + // Delete now the temporary file + deleteFile(destinationFile) + Timber.w("Unknown result: $result") + VideoCompressionResult.CompressionFailed(IllegalStateException("Unknown result: $result")) + } } }