Properly handle the compression cancellation

It should never occur though
This commit is contained in:
Benoit Marty 2021-05-05 10:53:52 +02:00
parent 8d52829918
commit 62207f02f0
3 changed files with 20 additions and 5 deletions

View File

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

View File

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

View File

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