Normalizes cache directories in the Application class

This commit is contained in:
Vitor Pamplona
2024-09-03 15:31:16 -04:00
parent 1166fe92e9
commit 2fd9d6610a
4 changed files with 15 additions and 6 deletions
@@ -59,10 +59,15 @@ class Amethyst : Application() {
applicationIOScope.cancel() applicationIOScope.cancel()
} }
fun nip95cache() = safeCacheDir.resolve("NIP95")
val videoCache: VideoCache by lazy { val videoCache: VideoCache by lazy {
val newCache = VideoCache() val newCache = VideoCache()
runBlocking { runBlocking {
newCache.initFileCache(this@Amethyst) newCache.initFileCache(
this@Amethyst,
safeCacheDir.resolve("exoplayer"),
)
} }
newCache newCache
} }
@@ -70,7 +75,7 @@ class Amethyst : Application() {
val coilCache: DiskCache by lazy { val coilCache: DiskCache by lazy {
DiskCache DiskCache
.Builder() .Builder()
.directory(applicationContext.safeCacheDir.resolve("image_cache")) .directory(safeCacheDir.resolve("image_cache"))
.maxSizePercent(0.2) .maxSizePercent(0.2)
.maximumMaxSizeBytes(1024 * 1024 * 1024) // 1GB .maximumMaxSizeBytes(1024 * 1024 * 1024) // 1GB
.build() .build()
@@ -1876,7 +1876,7 @@ object LocalCache {
} }
try { try {
val cachePath = File(Amethyst.instance.applicationContext.cacheDir, "NIP95") val cachePath = Amethyst.instance.nip95cache()
cachePath.mkdirs() cachePath.mkdirs()
val file = File(cachePath, event.id) val file = File(cachePath, event.id)
if (!file.exists()) { if (!file.exists()) {
@@ -43,13 +43,16 @@ class VideoCache {
lateinit var cacheDataSourceFactory: CacheDataSource.Factory lateinit var cacheDataSourceFactory: CacheDataSource.Factory
suspend fun initFileCache(context: Context) { suspend fun initFileCache(
context: Context,
cachePath: File,
) {
exoDatabaseProvider = StandaloneDatabaseProvider(context) exoDatabaseProvider = StandaloneDatabaseProvider(context)
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
simpleCache = simpleCache =
SimpleCache( SimpleCache(
File(context.cacheDir, "exoplayer"), cachePath,
leastRecentlyUsedCacheEvictor, leastRecentlyUsedCacheEvictor,
exoDatabaseProvider, exoDatabaseProvider,
) )
@@ -26,6 +26,7 @@ import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.platform.LocalContext
import com.vitorpamplona.amethyst.Amethyst
import com.vitorpamplona.amethyst.commons.richtext.BaseMediaContent import com.vitorpamplona.amethyst.commons.richtext.BaseMediaContent
import com.vitorpamplona.amethyst.commons.richtext.MediaLocalImage import com.vitorpamplona.amethyst.commons.richtext.MediaLocalImage
import com.vitorpamplona.amethyst.commons.richtext.MediaLocalVideo 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. // Creates a new object when the event arrives to force an update of the image.
val note = noteState?.note val note = noteState?.note
val uri = header.toNostrUri() 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 blurHash = eventHeader.blurhash()
val dimensions = eventHeader.dimensions() val dimensions = eventHeader.dimensions()
val description = eventHeader.alt() ?: eventHeader.content val description = eventHeader.alt() ?: eventHeader.content