From 5d84097f282f0a75dd875ee72318ceb0091d35fc Mon Sep 17 00:00:00 2001 From: James Ward Date: Tue, 24 Mar 2026 05:31:12 -0400 Subject: [PATCH] fix(api): check legacy tools path for binaries (#146) the kepubify / ffprobe binaries may be on disk but they're in the "data" directory under `tools` rather than under the current directory --- .../java/org/booklore/util/FileService.java | 26 +++++++++++--- .../org/booklore/util/FileServiceTest.java | 35 +++++++++++++++++++ 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/booklore-api/src/main/java/org/booklore/util/FileService.java b/booklore-api/src/main/java/org/booklore/util/FileService.java index b5e14647a..16421377d 100644 --- a/booklore-api/src/main/java/org/booklore/util/FileService.java +++ b/booklore-api/src/main/java/org/booklore/util/FileService.java @@ -148,15 +148,31 @@ public class FileService { return Paths.get(appProperties.getPathConfig(), "bookdrop_temp", bookdropFileId + ".jpg").toString(); } - public Path findSystemFile(String filename) { - // Search first in the application folder's "local" `tools` / `bin` folders + private String getSystemSearchPath() { + // Search first in the application folder's "local" `bin`. + StringBuilder localPaths = new StringBuilder("bin"); + + // Then, check the legacy "tools" path from previous app versions. + localPaths.append(File.pathSeparator).append(Path.of(appProperties.getPathConfig(), "tools")); + // If not found in those, then search the system $PATH. String systemSearchPath = System.getenv("PATH"); - String searchPath = systemSearchPath == null ? "bin:tools" : "bin:tools:".concat(systemSearchPath); - String[] searchPaths = searchPath.split(":"); + if (systemSearchPath != null) { + localPaths.append(File.pathSeparator).append(systemSearchPath); + } + + return localPaths.toString(); + } + + public Path findSystemFile(String filename) { + String[] searchPaths = getSystemSearchPath().split(":"); for (String path : searchPaths) { - Path possiblePath = Paths.get(path).resolve(filename).toAbsolutePath(); + Path possiblePath = Paths + .get(path) + .resolve(filename) + .toAbsolutePath() + .normalize(); if (Files.isRegularFile(possiblePath)) { return possiblePath; diff --git a/booklore-api/src/test/java/org/booklore/util/FileServiceTest.java b/booklore-api/src/test/java/org/booklore/util/FileServiceTest.java index 14b062d07..7f532555e 100644 --- a/booklore-api/src/test/java/org/booklore/util/FileServiceTest.java +++ b/booklore-api/src/test/java/org/booklore/util/FileServiceTest.java @@ -14,6 +14,7 @@ import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.ValueSource; import org.mockito.ArgumentCaptor; import org.mockito.Mock; +import org.mockito.MockedStatic; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.http.HttpEntity; import org.springframework.http.HttpMethod; @@ -305,6 +306,40 @@ class FileServiceTest { } } + @Nested + @DisplayName("findSystemFile") + class FindSystemFileTest { + @Test + void searchesLocalBinFolderFirst() { + Path expected = Path.of("bin/example").toAbsolutePath().normalize(); + + try ( + MockedStatic filesMock = mockStatic(Files.class); + ) { + filesMock.when(() -> Files.isRegularFile(any())).thenReturn(true); + + Path actual = fileService.findSystemFile("example"); + + assertEquals(expected, actual); + } + } + + @Test + void searchesAppDataToolsFolder() { + Path expected = tempDir.resolve("tools", "example"); + + try ( + MockedStatic filesMock = mockStatic(Files.class); + ) { + filesMock.when(() -> Files.isRegularFile(any())).thenReturn(false, true); + + Path actual = fileService.findSystemFile("example"); + + assertEquals(expected, actual); + } + } + } + @Nested @DisplayName("Other path methods") class OtherPathTests {