From c7cae413f7c9f41c49311dbb78daeec76b4bfe9a Mon Sep 17 00:00:00 2001 From: Acclorite <2ma0bhpk@anonaddy.com> Date: Sat, 27 Dec 2025 14:35:22 +0200 Subject: [PATCH] fix: possible issues with renaming files when caching * Write files to cache directly (previously first to .tmp, then rename, which may've led to issues on some devices) --- .../book_story/data/model/file/CachedFile.kt | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/app/src/main/java/ua/acclorite/book_story/data/model/file/CachedFile.kt b/app/src/main/java/ua/acclorite/book_story/data/model/file/CachedFile.kt index 19b577a2..9c759281 100644 --- a/app/src/main/java/ua/acclorite/book_story/data/model/file/CachedFile.kt +++ b/app/src/main/java/ua/acclorite/book_story/data/model/file/CachedFile.kt @@ -174,25 +174,16 @@ class CachedFile( */ private fun storeInCache(): File? { if (isDirectory) return null - - val cacheDir = context.cacheDir - val fileName = path.replace("_", "-").replace("/", "_").take(80) + "_${path.length}" - val cacheFile = File(cacheDir, fileName) - val tempFile = File(cacheDir, "$fileName.tmp") + val cacheFile = File(context.cacheDir, UUID.randomUUID().toString()) try { context.contentResolver.openInputStream(uri)?.use { input -> - BufferedOutputStream(FileOutputStream(tempFile)).use { output -> + BufferedOutputStream(FileOutputStream(cacheFile)).use { output -> input.copyTo(output) } } ?: throw IllegalStateException("Failed to open InputStream.") - - if (!tempFile.renameTo(cacheFile)) { - throw IllegalStateException("Failed to rename .tmp file.") - } } catch (e: Exception) { e.printStackTrace() - tempFile.delete() return null }