-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Fix #10559: Improve Image Handling and Align with Expected Behavior #12819
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
base: main
Are you sure you want to change the base?
Conversation
try { | ||
Files.createDirectories(imageFolder); | ||
} catch (IOException e) { | ||
e.printStackTrace(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using printStackTrace() for error handling is not appropriate for production code. Should use proper logging mechanism to handle exceptions and provide meaningful error messages.
Files.createDirectories(imageFolder); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
return null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Methods returning null violate modern Java practices. Should use Optional to handle absence of value instead of returning null.
event.consume(); | ||
} | ||
|
||
private boolean isImageFile(File file) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you extract this method into FileUtils
like this: https://github.com/JabRef/jabref/blob/main/src/main/java/org/jabref/logic/util/io/FileUtil.java#L494
} | ||
} | ||
|
||
private File downloadImageFromUrl(String urlString) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If your code may return null
s, please use Java Optional
private File downloadImageFromUrl(String urlString) { | ||
try { | ||
URL url = new URL(urlString); | ||
String fileName = url.getPath().substring(url.getPath().lastIndexOf('/') + 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should be a more proper way to do this.
Some method like getParent()
.
Manually processing paths by characters is not the best approach, as for example on Linux path separator is /
, on Windows \
try { | ||
URL url = new URL(urlString); | ||
String fileName = url.getPath().substring(url.getPath().lastIndexOf('/') + 1); | ||
Path imageFolder = databaseContext.getFirstExistingFileDir(preferences.getFilePreferences()).orElse(Path.of("images")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Introduce a constant for "images"
literal
|
||
private void insertMarkdownImage(File imageFile) { | ||
Path relativePath = databaseContext.getDatabasePath().map(dbPath -> dbPath.getParent().relativize(imageFile.toPath())).orElse(imageFile.toPath()); | ||
String markdownImage = String.format("\n\n", relativePath.toString().replace("\\", "/")); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be also good if that image template would also be a constant
@eslamnumber sorry for the many "JabRef Machine" comments, but one comment is missing - and I am trying to fix https://github.com/koppor/ghprcomment/ using this PR as example. Also refs jbangdev/jbang#1972 somehow. |
@trag-bot didn't find any issues in the code! ✅✨ |
JUnit tests are failing. You can see which checks are failing by locating the box "Some checks were not successful" on the pull request page. To see the test output, locate "Tests / Unit tests (pull_request)" and click on it. You can then run these tests in IntelliJ to reproduce the failing tests locally. We offer a quick test running howto in the section Final build system checks in our setup guide. |
You have removed the "Mandatory Checks" section from your pull request description. Please adhere to our pull request template. |
Note that your PR will not be reviewed/accepted until you have gone through the mandatory checks in the description and marked each of them them exactly in the format of |
Pull Request Title:
Fix #10559: Improve Image Handling in Comment Fields
Closes #10559
Description of Changes:
This pull request fixes the issue where images dragged into the comment field are not properly linked or saved. It ensures that images are correctly referenced and persist even after reopening the application.
Changes Implemented:
Steps to Reproduce (Before Fix):
Expected Behavior:
dev-ai-wide-10.png
) should appear in the editor.dev-ai-wide-10.png
should be stored in the same directory as the.bib
file.Actual Behavior (Before Fix):
TODOs:
Mandatory Checks:
CHANGELOG.md
described in a way that is understandable for the average user (if change is visible to the user).Additional Information:
Deep link to the problematic image:
Reference Feature in VS Code:
Next Steps:
Closes #10559