Make sure we clean up the pre-processed and uploaded media (#5039)

* Add `MediaSender.cleanUp()` and `MediaPreProcessor.cleanUp()` methods: this will remove the temporary files created when pre-processing media before sending them.

* Make sure we clean up also the previous temporary media.

* Fix the condition for the custom back handler in the attachments preview screen

* Tests: check the clean up is performed when needed
This commit is contained in:
Jorge Martin Espinosa
2025-07-23 08:55:20 +02:00
committed by GitHub
parent 746699b468
commit b219c05d06
7 changed files with 65 additions and 8 deletions

View File

@@ -22,5 +22,10 @@ interface MediaPreProcessor {
compressIfPossible: Boolean,
): Result<MediaUploadInfo>
/**
* Clean up any temporary files or resources used during the media processing.
*/
fun cleanUp()
data class Failure(override val cause: Throwable?) : Exception(cause)
}

View File

@@ -200,4 +200,9 @@ class MediaSender @Inject constructor(
uploadHandler.await()
}
}
/**
* Clean up any temporary files or resources used during the media processing.
*/
fun cleanUp() = preProcessor.cleanUp()
}

View File

@@ -41,6 +41,7 @@ import kotlinx.coroutines.withContext
import timber.log.Timber
import java.io.File
import java.io.InputStream
import java.util.UUID
import javax.inject.Inject
import kotlin.time.Duration
import kotlin.time.Duration.Companion.milliseconds
@@ -68,6 +69,9 @@ class AndroidMediaPreProcessor @Inject constructor(
private val contentResolver = context.contentResolver
private val cacheDir = context.cacheDir
private val baseTmpFileDir = File(cacheDir, "uploads")
override suspend fun process(
uri: Uri,
mimeType: String,
@@ -100,6 +104,28 @@ class AndroidMediaPreProcessor @Inject constructor(
}
}.mapFailure { MediaPreProcessor.Failure(it) }
override fun cleanUp() {
// Clear temporary files created in older versions of the app
cacheDir.listFiles()?.onEach { file ->
if (file.isFile) {
val nameWithoutExtension = file.nameWithoutExtension
// UUIDs are 36 characters long, so we check if we can take those 36 characters
val nameWithoutExtensionAndRandom = if (nameWithoutExtension.length > 36) {
nameWithoutExtension.substring(0, 36)
} else {
// Not a temp file
return@onEach
}
val isUUID = tryOrNull { UUID.fromString(nameWithoutExtensionAndRandom) } != null
if (isUUID && file.extension.isNotEmpty()) {
file.delete()
}
}
}
// Clear temporary files created by this pre-processor in the separate uploads directory
baseTmpFileDir.listFiles()?.onEach { it.delete() }
}
private suspend fun processFile(uri: Uri, mimeType: String): MediaUploadInfo {
val file = copyToTmpFile(uri)
val info = FileInfo(
@@ -280,7 +306,10 @@ class AndroidMediaPreProcessor @Inject constructor(
private suspend fun createTmpFileWithInput(inputStream: InputStream): File? {
return withContext(coroutineDispatchers.io) {
tryOrNull {
val tmpFile = context.createTmpFile()
if (!baseTmpFileDir.exists()) {
baseTmpFileDir.mkdirs()
}
val tmpFile = context.createTmpFile(baseTmpFileDir)
tmpFile.outputStream().use { inputStream.copyTo(it) }
tmpFile
}

View File

@@ -26,6 +26,9 @@ class FakeMediaPreProcessor(
var processCallCount = 0
private set
var cleanUpCallCount = 0
private set
private var result: Result<MediaUploadInfo> = Result.success(
MediaUploadInfo.AnyFile(
File("test"),
@@ -108,4 +111,8 @@ class FakeMediaPreProcessor(
)
)
}
override fun cleanUp() {
cleanUpCallCount += 1
}
}