From 2fd9d6610ae8c487aa47b459ed106f7268b2b578 Mon Sep 17 00:00:00 2001 From: Vitor Pamplona Date: Tue, 3 Sep 2024 15:31:16 -0400 Subject: [PATCH] Normalizes cache directories in the Application class --- .../src/main/java/com/vitorpamplona/amethyst/Amethyst.kt | 9 +++++++-- .../java/com/vitorpamplona/amethyst/model/LocalCache.kt | 2 +- .../amethyst/service/playback/VideoCache.kt | 7 +++++-- .../vitorpamplona/amethyst/ui/note/types/FileStorage.kt | 3 ++- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/Amethyst.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/Amethyst.kt index 5260c4bec..27385b737 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/Amethyst.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/Amethyst.kt @@ -59,10 +59,15 @@ class Amethyst : Application() { applicationIOScope.cancel() } + fun nip95cache() = safeCacheDir.resolve("NIP95") + val videoCache: VideoCache by lazy { val newCache = VideoCache() runBlocking { - newCache.initFileCache(this@Amethyst) + newCache.initFileCache( + this@Amethyst, + safeCacheDir.resolve("exoplayer"), + ) } newCache } @@ -70,7 +75,7 @@ class Amethyst : Application() { val coilCache: DiskCache by lazy { DiskCache .Builder() - .directory(applicationContext.safeCacheDir.resolve("image_cache")) + .directory(safeCacheDir.resolve("image_cache")) .maxSizePercent(0.2) .maximumMaxSizeBytes(1024 * 1024 * 1024) // 1GB .build() diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt index 492f691e2..d70fff5cd 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/model/LocalCache.kt @@ -1876,7 +1876,7 @@ object LocalCache { } try { - val cachePath = File(Amethyst.instance.applicationContext.cacheDir, "NIP95") + val cachePath = Amethyst.instance.nip95cache() cachePath.mkdirs() val file = File(cachePath, event.id) if (!file.exists()) { diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/playback/VideoCache.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/playback/VideoCache.kt index cfcd9042d..02de3fe7b 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/playback/VideoCache.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/playback/VideoCache.kt @@ -43,13 +43,16 @@ class VideoCache { lateinit var cacheDataSourceFactory: CacheDataSource.Factory - suspend fun initFileCache(context: Context) { + suspend fun initFileCache( + context: Context, + cachePath: File, + ) { exoDatabaseProvider = StandaloneDatabaseProvider(context) withContext(Dispatchers.IO) { simpleCache = SimpleCache( - File(context.cacheDir, "exoplayer"), + cachePath, leastRecentlyUsedCacheEvictor, exoDatabaseProvider, ) diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/types/FileStorage.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/types/FileStorage.kt index 70321e2ee..091424477 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/types/FileStorage.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/ui/note/types/FileStorage.kt @@ -26,6 +26,7 @@ import androidx.compose.runtime.livedata.observeAsState import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.platform.LocalContext +import com.vitorpamplona.amethyst.Amethyst import com.vitorpamplona.amethyst.commons.richtext.BaseMediaContent import com.vitorpamplona.amethyst.commons.richtext.MediaLocalImage import com.vitorpamplona.amethyst.commons.richtext.MediaLocalVideo @@ -74,7 +75,7 @@ private fun ObserverAndRenderNIP95( // Creates a new object when the event arrives to force an update of the image. val note = noteState?.note val uri = header.toNostrUri() - val localDir = note?.idHex?.let { File(File(appContext.cacheDir, "NIP95"), it) } + val localDir = note?.idHex?.let { File(Amethyst.instance.nip95cache(), it) } val blurHash = eventHeader.blurhash() val dimensions = eventHeader.dimensions() val description = eventHeader.alt() ?: eventHeader.content