From 62207f02f0eaf5cedc6bd5e0d3f67917388aa964 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Wed, 5 May 2021 10:53:52 +0200 Subject: [PATCH] Properly handle the compression cancellation It should never occur though --- .../session/content/UploadContentWorker.kt | 5 +++-- .../session/content/VideoCompressionResult.kt | 1 + .../session/content/VideoCompressor.kt | 19 ++++++++++++++++--- 3 files changed, 20 insertions(+), 5 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 f0a861587e..6a7d265308 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 @@ -186,7 +186,7 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter }) .let { videoCompressionResult -> when (videoCompressionResult) { - is VideoCompressionResult.Success -> { + is VideoCompressionResult.Success -> { val compressedFile = videoCompressionResult.compressedFile var compressedWidth: Int? = null var compressedHeight: Int? = null @@ -209,7 +209,8 @@ internal class UploadContentWorker(val context: Context, params: WorkerParameter compressedFile .also { filesToDelete.add(it) } } - is VideoCompressionResult.CompressionNotNeeded -> { + VideoCompressionResult.CompressionNotNeeded, + VideoCompressionResult.CompressionCancelled -> { workingFile } } 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 b1362317ba..6d576a79a3 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 @@ -21,4 +21,5 @@ import java.io.File internal sealed class VideoCompressionResult { data class Success(val compressedFile: File) : VideoCompressionResult() object CompressionNotNeeded : VideoCompressionResult() + object CompressionCancelled : 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 53113e2d86..297e453087 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 @@ -70,6 +70,15 @@ internal class VideoCompressor @Inject constructor(private val context: Context) job.join() + 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 + } + progressListener?.onProgress(100, 100) return when (result) { @@ -78,9 +87,7 @@ internal class VideoCompressor @Inject constructor(private val context: Context) } Transcoder.SUCCESS_NOT_NEEDED -> { // Delete now the temporary file - withContext(Dispatchers.IO) { - destinationFile.delete() - } + deleteFile(destinationFile) VideoCompressionResult.CompressionNotNeeded } else -> @@ -88,6 +95,12 @@ internal class VideoCompressor @Inject constructor(private val context: Context) } } + private suspend fun deleteFile(file: File) { + withContext(Dispatchers.IO) { + file.delete() + } + } + private fun createDestinationFile(): File { return File.createTempFile(UUID.randomUUID().toString(), null, context.cacheDir) }