Skip to content

Commit 869e80e

Browse files
paudelritijkoppor
andauthored
Fix directory path validation checks (#13029)
* Fix directory path validation checks * Fix Checkstyle * Fix CHANGELOG.md entry * Fix particular library location path * Improve code quality * Update JabRef_en.properties * Improve validation for unsaved libraries * Fix minor typo and comment * stringfix * stringfix --------- Co-authored-by: Oliver Kopp <[email protected]>
1 parent 8159964 commit 869e80e

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv
1919

2020
### Fixed
2121

22+
- We fixed an issue where directory check for relative path was not handled properly under library properties. [#13017](https://github.com/JabRef/jabref/issues/13017)
2223
- We fixed an issue where the option for which method to use when parsing plaintext citations was unavailable in the 'Create New Entry' tool. [#8808](https://github.com/JabRef/jabref/issues/8808)
2324

2425
### Removed

jabgui/src/main/java/org/jabref/gui/libraryproperties/general/GeneralPropertiesViewModel.java

+26-10
Original file line numberDiff line numberDiff line change
@@ -196,24 +196,40 @@ public StringProperty laTexFileDirectoryProperty() {
196196
}
197197

198198
private Path getBrowseDirectory(String configuredDir) {
199+
Optional<Path> libPath = this.databaseContext.getDatabasePath();
200+
Path workingDir = preferences.getFilePreferences().getWorkingDirectory();
201+
202+
if (libPath.isEmpty()) {
203+
Path potentialAbsolutePath = Path.of(configuredDir);
204+
return Files.isDirectory(potentialAbsolutePath) ? potentialAbsolutePath : workingDir;
205+
}
199206
if (configuredDir.isEmpty()) {
200-
return preferences.getFilePreferences().getWorkingDirectory();
207+
return workingDir;
201208
}
202-
Optional<Path> foundPath = this.databaseContext.getFileDirectories(preferences.getFilePreferences()).stream()
203-
.filter(path -> path.toString().endsWith(configuredDir))
204-
.filter(Files::exists).findFirst();
205209

206-
if (foundPath.isEmpty()) {
207-
dialogService.notify(Localization.lang("Path %0 could not be resolved. Using working dir.", configuredDir));
208-
return preferences.getFilePreferences().getWorkingDirectory();
210+
Path configuredPath = libPath.get().getParent().resolve(configuredDir).normalize();
211+
212+
// configuredDir can be input manually, which may lead it to being invalid
213+
if (!Files.isDirectory(configuredPath)) {
214+
dialogService.notify(Localization.lang("Path %0 could not be resolved. Using working directory.", configuredDir));
215+
return workingDir;
209216
}
210-
return foundPath.get();
217+
218+
return configuredPath;
211219
}
212220

213221
private ValidationMessage validateDirectory(String directoryPath, String messageKey) {
222+
Optional<Path> libPath = this.databaseContext.getDatabasePath();
223+
Path potentialAbsolutePath = Path.of(directoryPath);
224+
225+
// check absolute path separately in case of unsaved libraries
226+
if (libPath.isEmpty() && Files.isDirectory(potentialAbsolutePath)) {
227+
return null;
228+
}
214229
try {
215-
Path path = Path.of(directoryPath);
216-
if (!Files.isDirectory(path)) {
230+
if (!libPath.map(p -> p.getParent().resolve(directoryPath).normalize())
231+
.map(Files::isDirectory)
232+
.orElse(false)) {
217233
return ValidationMessage.error(
218234
Localization.lang("File directory '%0' not found.\nCheck \"%1\" file directory path.", directoryPath, messageKey)
219235
);

jablib/src/main/resources/l10n/JabRef_en.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1090,7 +1090,7 @@ Expected\ syntax\ for\ --fetch\='<name\ of\ fetcher>\:<query>'=Expected syntax f
10901090
Library-specific\ file\ directory=Library-specific file directory
10911091
User-specific\ file\ directory=User-specific file directory
10921092
LaTeX\ file\ directory=LaTeX file directory
1093-
Path\ %0\ could\ not\ be\ resolved.\ Using\ working\ dir.=Path %0 could not be resolved. Using working dir.
1093+
Path\ %0\ could\ not\ be\ resolved.\ Using\ working\ directory.=Path %0 could not be resolved. Using working directory.
10941094

10951095

10961096
You\ must\ enter\ an\ integer\ value\ in\ the\ interval\ 1025-65535=You must enter an integer value in the interval 1025-65535

0 commit comments

Comments
 (0)