Skip to content

Commit 10fae1b

Browse files
kopporSiedlerchr
andcommitted
Enable files/files
Co-authored-by: Christoph <[email protected]>
1 parent a58ffd5 commit 10fae1b

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

src/main/java/org/jabref/model/util/FileHelper.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,12 @@ public static boolean isCharLegal(char c) {
151151
}
152152

153153
/**
154-
* Converts a relative filename to an absolute one, if necessary. Returns
155-
* an empty optional if the file does not exist.
154+
* Converts a relative filename to an absolute one, if necessary.
155+
*
156+
* @param fileName the filename (e.g., a .pdf file), may contain path separators
157+
* @param directory the directory which should be search starting point
158+
*
159+
* @returns an empty optional if the file does not exist, otherwise, the absolute path
156160
*/
157161
public static Optional<Path> find(String fileName, Path directory) {
158162
Objects.requireNonNull(fileName);
@@ -166,11 +170,15 @@ public static Optional<Path> find(String fileName, Path directory) {
166170
if (fileName.isEmpty()) {
167171
return Optional.of(directory);
168172
}
173+
174+
Path resolvedFile = directory.resolve(fileName);
175+
if (Files.exists(resolvedFile)) {
176+
return Optional.of(resolvedFile);
177+
}
178+
169179
// get the furthest path element from root and check if our filename starts with the same name
170180
// workaround for old JabRef behavior
171181
String furthestDirFromRoot = directory.getFileName().toString();
172-
Path resolvedFile = directory.resolve(fileName);
173-
174182
if (fileName.startsWith(furthestDirFromRoot)) {
175183
resolvedFile = directory.resolveSibling(fileName);
176184
}

src/test/java/org/jabref/model/util/FileHelperTest.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,11 @@ public void testFindsFileStartingWithTheSameDirectory(@TempDir Path temp) throws
5858

5959
@Test
6060
public void testDoesNotFindsFileStartingWithTheSameDirectoryHasASubdirectory(@TempDir Path temp) throws Exception {
61-
Path firstFilePath = temp.resolve("files");
62-
Path secondFilesPath = firstFilePath.resolve("files");
61+
Path firstFilesPath = temp.resolve("files");
62+
Path secondFilesPath = firstFilesPath.resolve("files");
6363
Files.createDirectories(secondFilesPath);
64-
Path firstFile = Files.createFile(secondFilesPath.resolve("test.pdf"));
65-
assertEquals(Optional.empty(), FileHelper.find("files/test.pdf", temp.resolve("files")));
64+
Path testFile = secondFilesPath.resolve("test.pdf");
65+
Files.createFile(testFile);
66+
assertEquals(Optional.of(testFile.toAbsolutePath()), FileHelper.find("files/test.pdf", firstFilesPath));
6667
}
6768
}

0 commit comments

Comments
 (0)