Skip to content

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

eslamnumber
Copy link

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:

  • Improved JabRef’s download functionality to correctly fetch and store images.
  • Added a direct link in the comment field next to the cursor for better user experience.
  • Modified the "Find unlinked files" feature to ignore files that are linked within comment fields.
  • Enhanced image handling for better compatibility with external sources.

Steps to Reproduce (Before Fix):

  1. Create a new library where the file directory is configured.
  2. Open Firefox and visit: Sequoia AI Tools.
  3. Import the article into JabRef.
  4. Navigate to the Comments tab.
  5. Drag and drop an image into the comment field.

Expected Behavior:

  • The image (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):

  • The image does not get linked correctly.
  • The file is not stored, leading to missing images on restart.

TODOs:

  1. Implement JabRef’s download functionality to store image files.
  2. Ensure image links are retained in the comment field.
  3. Modify "Find unlinked files" to ignore images referenced in comment fields.

Mandatory Checks:

  • I own the copyright of the code submitted and I license it under the MIT license.
  • Change in CHANGELOG.md described in a way that is understandable for the average user (if change is visible to the user).
  • Tests created for changes (if applicable).
  • Manually tested changed features in running JabRef (always required).
  • Screenshots added in PR description (if change is visible to the user).
  • Checked developer's documentation: Is the information available and up to date? If not, I outlined it in this pull request.
  • Checked documentation: Is the information available and up to date? If not, I created an issue at https://github.com/JabRef/user-documentation/issues or, even better, I submitted a pull request to the documentation repository.

Additional Information:


Next Steps:

  • Await feedback from maintainers.
  • Apply requested changes if necessary.

Closes #10559

try {
Files.createDirectories(imageFolder);
} catch (IOException e) {
e.printStackTrace();
Copy link

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;
Copy link

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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}
}

private File downloadImageFromUrl(String urlString) {
Copy link
Member

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 nulls, please use Java Optional

private File downloadImageFromUrl(String urlString) {
try {
URL url = new URL(urlString);
String fileName = url.getPath().substring(url.getPath().lastIndexOf('/') + 1);
Copy link
Member

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"));
Copy link
Member

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![Image](%s)\n", relativePath.toString().replace("\\", "/"));
Copy link
Member

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

@koppor
Copy link
Member

koppor commented Mar 25, 2025

@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.

@JabRef JabRef deleted a comment from jabref-machine Mar 25, 2025
Copy link

trag-bot bot commented Apr 18, 2025

@trag-bot didn't find any issues in the code! ✅✨

@jabref-machine
Copy link
Collaborator

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.

@jabref-machine
Copy link
Collaborator

You have removed the "Mandatory Checks" section from your pull request description. Please adhere to our pull request template.

@jabref-machine
Copy link
Collaborator

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 [x] (done), [ ] (not done yet) or [/] (not applicable).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow file attachment into the comments editor (Markdown)
5 participants