From 253cb03de84b8a0593898809fcce19f555da38ac Mon Sep 17 00:00:00 2001 From: FooIbar <118464521+FooIbar@users.noreply.github.com> Date: Mon, 11 Nov 2024 16:23:12 +0800 Subject: [PATCH] Refine AndroidFileSystem exception handling --- .../kotlin/com/hippo/files/AndroidFileSystem.kt | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/app/src/main/kotlin/com/hippo/files/AndroidFileSystem.kt b/app/src/main/kotlin/com/hippo/files/AndroidFileSystem.kt index 736635b9d..8dcb609b4 100644 --- a/app/src/main/kotlin/com/hippo/files/AndroidFileSystem.kt +++ b/app/src/main/kotlin/com/hippo/files/AndroidFileSystem.kt @@ -133,7 +133,7 @@ class AndroidFileSystem(context: Context) : FileSystem() { dir / displayName } } - }.getOrElse { if (throwOnFailure) throw it else null } + }.getOrElse { if (throwOnFailure) throw IOException("Failed to list $dir") else null } } override fun metadataOrNull(path: Path): FileMetadata? { @@ -187,16 +187,16 @@ class AndroidFileSystem(context: Context) : FileSystem() { return ParcelFileDescriptor.open(path.toFile(), ParcelFileDescriptor.parseMode(mode)) } - if ('w' in mode && !exists(path)) { - path.parent?.runCatching { + return runCatching { + if ('w' in mode && !exists(path)) { + val parent = path.parent ?: return@runCatching null val displayName = path.name val extension = displayName.substringAfterLast('.', "").ifEmpty { null }?.lowercase() val mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension) ?: "application/octet-stream" - DocumentsContract.createDocument(contentResolver, toUri(), mimeType, displayName) - }?.getOrNull() ?: throw IOException("Failed to open file: $path") - } - - return contentResolver.openFileDescriptor(path.toUri(), mode) ?: throw IOException("Failed to open file: $path") + DocumentsContract.createDocument(contentResolver, parent.toUri(), mimeType, displayName) + } + contentResolver.openFileDescriptor(path.toUri(), mode) + }.getOrNull() ?: throw IOException("Failed to open file: $path") } }