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
This commit is contained in:
James Ward
2026-04-22 17:26:33 -04:00
committed by Zach Yale
parent 5bcef92b65
commit 5d84097f28
2 changed files with 56 additions and 5 deletions
@@ -148,15 +148,31 @@ public class FileService {
return Paths.get(appProperties.getPathConfig(), "bookdrop_temp", bookdropFileId + ".jpg").toString(); return Paths.get(appProperties.getPathConfig(), "bookdrop_temp", bookdropFileId + ".jpg").toString();
} }
public Path findSystemFile(String filename) { private String getSystemSearchPath() {
// Search first in the application folder's "local" `tools` / `bin` folders // 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. // If not found in those, then search the system $PATH.
String systemSearchPath = System.getenv("PATH"); String systemSearchPath = System.getenv("PATH");
String searchPath = systemSearchPath == null ? "bin:tools" : "bin:tools:".concat(systemSearchPath); if (systemSearchPath != null) {
String[] searchPaths = searchPath.split(":"); localPaths.append(File.pathSeparator).append(systemSearchPath);
}
return localPaths.toString();
}
public Path findSystemFile(String filename) {
String[] searchPaths = getSystemSearchPath().split(":");
for (String path : searchPaths) { 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)) { if (Files.isRegularFile(possiblePath)) {
return possiblePath; return possiblePath;
@@ -14,6 +14,7 @@ import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource; import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockedStatic;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod; 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<Files> 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<Files> filesMock = mockStatic(Files.class);
) {
filesMock.when(() -> Files.isRegularFile(any())).thenReturn(false, true);
Path actual = fileService.findSystemFile("example");
assertEquals(expected, actual);
}
}
}
@Nested @Nested
@DisplayName("Other path methods") @DisplayName("Other path methods")
class OtherPathTests { class OtherPathTests {