Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix video preview creating cache files that are not being deleted if the app is killed #1434

Merged
merged 4 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/src/main/java/eu/darken/sdmse/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import coil.ImageLoaderFactory
import dagger.hilt.android.HiltAndroidApp
import eu.darken.sdmse.common.BuildConfigWrap
import eu.darken.sdmse.common.BuildWrap
import eu.darken.sdmse.common.coil.CoilTempFiles
import eu.darken.sdmse.common.coroutine.AppScope
import eu.darken.sdmse.common.coroutine.DispatcherProvider
import eu.darken.sdmse.common.debug.AutomaticBugReporter
Expand All @@ -28,6 +29,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch
import javax.inject.Inject
import kotlin.system.exitProcess

Expand All @@ -45,6 +47,7 @@ open class App : Application(), Configuration.Provider {
@Inject lateinit var curriculumVitae: CurriculumVitae
@Inject lateinit var updateService: UpdateService
@Inject lateinit var theming: Theming
@Inject lateinit var coilTempFiles: CoilTempFiles

private val logCatLogger = LogCatLogger()

Expand Down Expand Up @@ -83,6 +86,7 @@ open class App : Application(), Configuration.Provider {

theming.setup()

appScope.launch { coilTempFiles.cleanUp() }
Coil.setImageLoader(imageLoaderFactory)

curriculumVitae.updateAppLaunch()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import javax.inject.Inject

class BitmapFetcher @Inject constructor(
@ApplicationContext private val context: Context,
private val coilTempFiles: CoilTempFiles,
private val gatewaySwitch: GatewaySwitch,
private val mimeTypeTool: MimeTypeTool,
private val data: Request,
Expand All @@ -38,14 +39,18 @@ class BitmapFetcher @Inject constructor(
val buffer = gatewaySwitch.read(target.lookedUp).buffer()

return SourceResult(
ImageSource(buffer, context),
ImageSource(
buffer,
coilTempFiles.getBaseCachePath(),
),
mimeType,
dataSource = DataSource.DISK
)
}

class Factory @Inject constructor(
@ApplicationContext private val context: Context,
private val coilTempFiles: CoilTempFiles,
private val gatewaySwitch: GatewaySwitch,
private val mimeTypeTool: MimeTypeTool,
) : Fetcher.Factory<Request> {
Expand All @@ -54,7 +59,7 @@ class BitmapFetcher @Inject constructor(
data: Request,
options: Options,
imageLoader: ImageLoader
): Fetcher = BitmapFetcher(context, gatewaySwitch, mimeTypeTool, data, options)
): Fetcher = BitmapFetcher(context, coilTempFiles, gatewaySwitch, mimeTypeTool, data, options)
}

data class Request(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class CoilModule {
}
dispatcher(
dispatcherProvider.Default.limitedParallelism(
(Runtime.getRuntime().availableProcessors() - 1).coerceAtLeast(2)
(Runtime.getRuntime().availableProcessors() / 2).coerceAtLeast(2)
)
)
}.build()
Expand Down
64 changes: 64 additions & 0 deletions app/src/main/java/eu/darken/sdmse/common/coil/CoilTempFiles.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package eu.darken.sdmse.common.coil

import android.content.Context
import dagger.hilt.android.qualifiers.ApplicationContext
import eu.darken.sdmse.common.coroutine.DispatcherProvider
import eu.darken.sdmse.common.debug.logging.Logging.Priority.VERBOSE
import eu.darken.sdmse.common.debug.logging.Logging.Priority.WARN
import eu.darken.sdmse.common.debug.logging.asLog
import eu.darken.sdmse.common.debug.logging.log
import eu.darken.sdmse.common.debug.logging.logTag
import eu.darken.sdmse.common.files.core.local.listFiles2
import kotlinx.coroutines.NonCancellable
import kotlinx.coroutines.withContext
import java.io.File
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class CoilTempFiles @Inject constructor(
@ApplicationContext private val context: Context,
private val dispatcherProvider: DispatcherProvider,
) {

private val basePath: File = File(context.cacheDir, "coil")
private val legacyPath: File = context.cacheDir

suspend fun getBaseCachePath(): File {
val path = basePath.apply {
if (mkdirs()) log(TAG) { "Cache path was created: $this" }
}
return path
}

suspend fun cleanUp() = withContext(dispatcherProvider.IO + NonCancellable) {
try {
log(TAG) { "Checking for legacy files in $legacyPath" }
legacyPath.listFiles2()
.filter { NAME_REGEX.matches(it.name) }
.forEach {
log(TAG) { "Deleting legacy tmp file: $it (${it.length()} Byte)" }
if (it.delete()) log(TAG, VERBOSE) { "Deleted legacy file: $it" }
}
} catch (e: Exception) {
log(TAG, WARN) { "Failed to clean up legacy files $basePath:\n${e.asLog()}" }
}

if (!basePath.exists()) return@withContext

try {
log(TAG) { "Cleaning up $basePath" }
basePath.listFiles2().forEach {
log(TAG) { "Deleting orphaned tmp file: $it (${it.length()} Byte)" }
if (it.delete()) log(TAG, VERBOSE) { "Deleted: $it" }
}
} catch (e: Exception) {
log(TAG, WARN) { "Failed to clean up $basePath:\n${e.asLog()}" }
}
}

companion object {
private val TAG = logTag("Coil", "TempFiles")
internal val NAME_REGEX = Regex("""tmp\d+\.tmp""")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import javax.inject.Inject

class PathPreviewFetcher @Inject constructor(
@ApplicationContext private val context: Context,
private val coilTempFiles: CoilTempFiles,
private val generalSettings: GeneralSettings,
private val gatewaySwitch: GatewaySwitch,
private val mimeTypeTool: MimeTypeTool,
Expand Down Expand Up @@ -56,7 +57,10 @@ class PathPreviewFetcher @Inject constructor(
mimeType.startsWith("image") || mimeType.startsWith("video") -> {
val buffer = gatewaySwitch.read(data.lookedUp).buffer()
SourceResult(
ImageSource(buffer, context),
ImageSource(
buffer,
coilTempFiles.getBaseCachePath(),
),
mimeType,
dataSource = DataSource.DISK
)
Expand Down Expand Up @@ -91,6 +95,7 @@ class PathPreviewFetcher @Inject constructor(

class Factory @Inject constructor(
@ApplicationContext private val context: Context,
private val coilTempFiles: CoilTempFiles,
private val generalSettings: GeneralSettings,
private val gatewaySwitch: GatewaySwitch,
private val mimeTypeTool: MimeTypeTool,
Expand All @@ -100,7 +105,15 @@ class PathPreviewFetcher @Inject constructor(
data: APathLookup<*>,
options: Options,
imageLoader: ImageLoader
): Fetcher = PathPreviewFetcher(context, generalSettings, gatewaySwitch, mimeTypeTool, data, options)
): Fetcher = PathPreviewFetcher(
context,
coilTempFiles,
generalSettings,
gatewaySwitch,
mimeTypeTool,
data,
options,
)
}
}

16 changes: 16 additions & 0 deletions app/src/test/java/eu/darken/sdmse/common/coil/CoilTempFilesTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package eu.darken.sdmse.common.coil

import io.kotest.matchers.shouldBe
import org.junit.jupiter.api.Test
import testhelpers.BaseTest


class CoilTempFilesTest : BaseTest() {

@Test fun `legacy cleanup tmp file path matching`() {
CoilTempFiles.NAME_REGEX.matches("tmp9050482114132890229.tmp") shouldBe true
CoilTempFiles.NAME_REGEX.matches("9050482114132890229.tmp") shouldBe false
CoilTempFiles.NAME_REGEX.matches("tmp9050482114132890229.other") shouldBe false
CoilTempFiles.NAME_REGEX.matches("tmp90504229.tmp") shouldBe true
}
}
Loading