From 7662c059ace958ad25c793c25d44b00a364723d6 Mon Sep 17 00:00:00 2001 From: Michael Totschnig Date: Mon, 27 Jan 2025 21:05:27 +0100 Subject: [PATCH] Return NotFound instead of Error --- .../java/org/totschnig/webui/WebInputService.kt | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/webui/src/main/java/org/totschnig/webui/WebInputService.kt b/webui/src/main/java/org/totschnig/webui/WebInputService.kt index 9b3132fd4f..bd4c6f69a7 100644 --- a/webui/src/main/java/org/totschnig/webui/WebInputService.kt +++ b/webui/src/main/java/org/totschnig/webui/WebInputService.kt @@ -553,13 +553,16 @@ class WebInputService : LifecycleService(), IWebInputService { get("/download/{file}") { val appDir = AppDirHelper.getAppDir(this@WebInputService).getOrThrow() - val inputStream = contentResolver.openInputStream( - appDir.findFile(call.parameters["file"]!!)!!.uri.let { - AppDirHelper.ensureContentUri(it, this@WebInputService) - } - )!! - val channel = inputStream.toByteReadChannel() - call.respond(channel) + val file = appDir.findFile(call.parameters["file"]!!)?.uri?.let { + AppDirHelper.ensureContentUri(it, this@WebInputService) + } + if (file == null) { + call.respond(HttpStatusCode.NotFound, "File not found") + } else { + call.respond( + contentResolver.openInputStream(file)!!.toByteReadChannel() + ) + } } }