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:
@@ -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;
|
||||
|
||||
@@ -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<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
|
||||
@DisplayName("Other path methods")
|
||||
class OtherPathTests {
|
||||
|
||||
Reference in New Issue
Block a user