Skip to content

Fix directory path validation checks #13029

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 2, 2025
Merged
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ Note that this project **does not** adhere to [Semantic Versioning](https://semv

### Fixed

- 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)
- 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)

### Removed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,24 +196,40 @@ public StringProperty laTexFileDirectoryProperty() {
}

private Path getBrowseDirectory(String configuredDir) {
Optional<Path> libPath = this.databaseContext.getDatabasePath();
Path workingDir = preferences.getFilePreferences().getWorkingDirectory();

if (libPath.isEmpty()) {
Path potentialAbsolutePath = Path.of(configuredDir);
return Files.isDirectory(potentialAbsolutePath) ? potentialAbsolutePath : workingDir;
}
if (configuredDir.isEmpty()) {
return preferences.getFilePreferences().getWorkingDirectory();
return workingDir;
}
Optional<Path> foundPath = this.databaseContext.getFileDirectories(preferences.getFilePreferences()).stream()
.filter(path -> path.toString().endsWith(configuredDir))
.filter(Files::exists).findFirst();

if (foundPath.isEmpty()) {
dialogService.notify(Localization.lang("Path %0 could not be resolved. Using working dir.", configuredDir));
return preferences.getFilePreferences().getWorkingDirectory();
Path configuredPath = libPath.get().getParent().resolve(configuredDir).normalize();

// configuredDir can be input manually, which may lead it to being invalid
if (!Files.isDirectory(configuredPath)) {
dialogService.notify(Localization.lang("Path %0 could not be resolved. Using working directory.", configuredDir));
return workingDir;
}
return foundPath.get();

return configuredPath;
}

private ValidationMessage validateDirectory(String directoryPath, String messageKey) {
Optional<Path> libPath = this.databaseContext.getDatabasePath();
Path potentialAbsolutePath = Path.of(directoryPath);

// check absolute path separately in case of unsaved libraries
if (libPath.isEmpty() && Files.isDirectory(potentialAbsolutePath)) {
return null;
}
try {
Path path = Path.of(directoryPath);
if (!Files.isDirectory(path)) {
if (!libPath.map(p -> p.getParent().resolve(directoryPath).normalize())
.map(Files::isDirectory)
.orElse(false)) {
return ValidationMessage.error(
Localization.lang("File directory '%0' not found.\nCheck \"%1\" file directory path.", directoryPath, messageKey)
);
Expand Down
2 changes: 1 addition & 1 deletion jablib/src/main/resources/l10n/JabRef_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,7 @@ Expected\ syntax\ for\ --fetch\='<name\ of\ fetcher>\:<query>'=Expected syntax f
Library-specific\ file\ directory=Library-specific file directory
User-specific\ file\ directory=User-specific file directory
LaTeX\ file\ directory=LaTeX file directory
Path\ %0\ could\ not\ be\ resolved.\ Using\ working\ dir.=Path %0 could not be resolved. Using working dir.
Path\ %0\ could\ not\ be\ resolved.\ Using\ working\ directory.=Path %0 could not be resolved. Using working directory.


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