From 9ebee56dbae9d6388083d58f8c1c47383074622f Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 11 Jun 2026 20:16:37 +0000 Subject: [PATCH] fix: recreate thumbnail cache dir before writing temp file The profile_thumbnails_v2 dir was only created in ThumbnailDiskCache's constructor, but Android can delete cache subdirectories while the app runs (system cache trim under storage pressure, or the user tapping Clear cache in Settings). After that, every generateFromFile call failed with ENOENT on the temp-file write until process restart, silently disabling thumbnail caching. Recreate the dir right before the write, and add instrumented regression tests covering the cleared-at-runtime path. https://claude.ai/code/session_01RQinCw5QKpaYXqtyb4gf3h --- .../ThumbnailDiskCacheInstrumentedTest.kt | 80 +++++++++++++++++++ .../service/images/ThumbnailDiskCache.kt | 3 + 2 files changed, 83 insertions(+) create mode 100644 amethyst/src/androidTest/java/com/vitorpamplona/amethyst/service/images/ThumbnailDiskCacheInstrumentedTest.kt diff --git a/amethyst/src/androidTest/java/com/vitorpamplona/amethyst/service/images/ThumbnailDiskCacheInstrumentedTest.kt b/amethyst/src/androidTest/java/com/vitorpamplona/amethyst/service/images/ThumbnailDiskCacheInstrumentedTest.kt new file mode 100644 index 0000000000..04f5e861e6 --- /dev/null +++ b/amethyst/src/androidTest/java/com/vitorpamplona/amethyst/service/images/ThumbnailDiskCacheInstrumentedTest.kt @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2025 Vitor Pamplona + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to use, + * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the + * Software, and to permit persons to whom the Software is furnished to do so, + * subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS + * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR + * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN + * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.vitorpamplona.amethyst.service.images + +import android.graphics.Bitmap +import androidx.test.ext.junit.runners.AndroidJUnit4 +import androidx.test.platform.app.InstrumentationRegistry +import org.junit.After +import org.junit.Assert.assertNotNull +import org.junit.Assert.assertTrue +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import java.io.File +import java.util.UUID + +@RunWith(AndroidJUnit4::class) +class ThumbnailDiskCacheInstrumentedTest { + private val appContext = InstrumentationRegistry.getInstrumentation().targetContext + private lateinit var cacheDir: File + private lateinit var cache: ThumbnailDiskCache + private lateinit var sourceFile: File + + @Before + fun setUp() { + cacheDir = File(appContext.cacheDir, "thumbnail-test-${UUID.randomUUID()}") + cache = ThumbnailDiskCache(cacheDir) + sourceFile = File(appContext.cacheDir, "source-${UUID.randomUUID()}.jpg") + val bitmap = Bitmap.createBitmap(64, 64, Bitmap.Config.ARGB_8888) + sourceFile.outputStream().use { bitmap.compress(Bitmap.CompressFormat.JPEG, 90, it) } + bitmap.recycle() + } + + @After + fun tearDown() { + cacheDir.deleteRecursively() + sourceFile.delete() + } + + @Test + fun generatesThumbnailFromJpeg() { + val url = "https://example.com/profile-pic-${UUID.randomUUID()}.jpg" + + assertTrue("generateFromFile must return true for a valid JPEG", cache.generateFromFile(url, sourceFile)) + assertNotNull("load() must return a non-null Bitmap for a cached JPEG", cache.load(url)) + } + + @Test + fun recreatesCacheDirClearedAtRuntime() { + // The system (cache trim) or the user (Settings > Clear cache) can delete + // the cache dir while the app runs; the next write must recreate it + // instead of failing with ENOENT. + val url = "https://example.com/profile-pic-${UUID.randomUUID()}.jpg" + assertTrue(cacheDir.deleteRecursively()) + + assertTrue( + "generateFromFile must recreate the cache dir after it is cleared at runtime", + cache.generateFromFile(url, sourceFile), + ) + assertNotNull("load() must return the thumbnail written after dir recreation", cache.load(url)) + } +} diff --git a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/images/ThumbnailDiskCache.kt b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/images/ThumbnailDiskCache.kt index 8b20239483..1a8cf64c42 100644 --- a/amethyst/src/main/java/com/vitorpamplona/amethyst/service/images/ThumbnailDiskCache.kt +++ b/amethyst/src/main/java/com/vitorpamplona/amethyst/service/images/ThumbnailDiskCache.kt @@ -146,6 +146,9 @@ class ThumbnailDiskCache( } val tempFile = File(cacheDir, "$key.tmp") + // The cache dir can be wiped while the app runs (system cache trim, + // user "Clear cache"); recreate it or the write fails with ENOENT. + cacheDir.mkdirs() tempFile.outputStream().use { out -> scaled.compress(Bitmap.CompressFormat.JPEG, JPEG_QUALITY, out) }