jabref icon indicating copy to clipboard operation
jabref copied to clipboard

Add auto-renaming of linked files on entry data change

Open paudelritij opened this issue 6 months ago • 1 comments

Closes #11316

This PR introduces an option to automatically rename linked files when the associated entry data is modified. The feature is configurable in Preferences under Linked files -> Linked file name conventions, with a default setting of false. It also includes handling for entries with multiple attached files and adds relevant test cases.

Steps to test

  1. Open JabRef and go to Preferences > Linked files.
  2. Enable [ ] Auto rename files if entry changes.
  3. Create a new entry and attach a file.
  4. Modify any field in the entry.
  5. Check that the linked file's name updates to match the new citation key.
  6. Test with multiple files to ensure all are renamed.
image

Mandatory checks

  • [x] I own the copyright of the code submitted and I license it under the MIT license
  • [x] Change in CHANGELOG.md described in a way that is understandable for the average user (if change is visible to the user)
  • [x] Tests created for changes (if applicable)
  • [x] Manually tested changed features in running JabRef (always required)
  • [x] Screenshots added in PR description (if change is visible to the user)
  • [x] Checked developer's documentation: Is the information available and up to date? If not, I outlined it in this pull request.
  • [x] 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.

paudelritij avatar Jun 09 '25 17:06 paudelritij

Ensure if the target file exists, do not use that name, use FileNameUniqueness.getNonOverWritingFileName(targetDirectory, targetFileName) to ensure that there is a unique other file name.

(Note to us: File should be kept even there are binary equal files. Otherwise, it gets complicated. E.g., if the same file is attached to another entry).

koppor avatar Jul 07 '25 19:07 koppor

Ensure if the target file exists, do not use that name, use FileNameUniqueness.getNonOverWritingFileName(targetDirectory, targetFileName) to ensure that there is a unique other file name.

(Note to us: File should be kept even there are binary equal files. Otherwise, it gets complicated. E.g., if the same file is attached to another entry).

I am trying implementing it in org.jabref.logic.externalfiles.LinkedFileHandler#renameToSuggestedName to put uniqueFileName. However, I'm encountering an issue with org.jabref.logic.search.indexing.DocumentReader#addMetaData, which is causing errors when reading timestamps. There may be creation of a loop where file changes name from e.g. abc.pdf to abc(1).pdf to abc.pdf again repeatedly. Any insights on how to resolve this would be greatly appreciated.

" ERROR: Could not read timestamp for /Users//Documents/JabRef/Gen28_ - Hands on Machine Learning with Scikit Learn, Keras, and TensorFlow.pdf: java.nio.file.NoSuchFileException: /Users//Documents/JabRef/Gen28_ - Hands on Machine Learning with Scikit Learn, Keras, and TensorFlow.pdf "

paudelritij avatar Jul 15 '25 06:07 paudelritij

@LoayGhreeb Do you have some hints maybe?

koppor avatar Jul 15 '25 06:07 koppor

I am trying implementing it in org.jabref.logic.externalfiles.LinkedFileHandler#renameToSuggestedName to put uniqueFileName. However, I'm encountering an issue with org.jabref.logic.search.indexing.DocumentReader#addMetaData, which is causing errors when reading timestamps. There may be creation of a loop where file changes name from e.g. abc.pdf to abc(1).pdf to abc.pdf again repeatedly. Any insights on how to resolve this would be greatly appreciated.

I don't think the problem is with the indexing itself. The NoSuchFileException means it's trying to access a file that doesn't exist, maybe it's still trying to use the old name, or the new one, but since there's a loop renaming the file back and forth, the file might not be available at the expected time during indexing.

I guess this loop could be happening if the same file is linked to two different entries. Could that be the case?

Please try to investigate further by adding some breakpoints to understand why, and where this loop is happening. Also, if possible, please push the commits that reproduce this issue, either here or in a new branch so we can take a proper look and help without guessing what's going on.

LoayGhreeb avatar Jul 16 '25 13:07 LoayGhreeb

when using FileNameUniqueness.getNonOverWritingFileName(), a loop was occurring why?

  • Example: The original file name is paper_2082.pdf.
  • After renaming, the new name becomes paper1_2082.pdf. This triggers a file field change event.
  • On the next iteration, getSuggestedFileName() generates paper1_2082.pdf as the suggested name.
  • Since the file paper1_2082.pdf already exists (only we know that it is already linked to same entry), FileNameUniqueness.getNonOverWritingFileName() renames it to paper1_2082 (1).pdf.
  • This triggers another file field change event by generating file name again as paper1_2082.pdf (as it was in 1st place), and the process repeats, causing an infinite loop.

Solution Implemented

Case 1: File Exists but is Not Linked to the Entry: A new unique file name is suggested. Case 2: File Exists and is Linked to the Entry: The file name change is discarded to prevent unnecessary renaming and event triggering.

paudelritij avatar Jul 24 '25 05:07 paudelritij

I got following exceptions in the log if I had opened the PDF - therefore, I removed the exception at the log

image

koppor avatar Aug 23 '25 01:08 koppor

I wired the listener to our CoarseGraindListener, because not at every single letter changes, the file should be renamed.

I also fixed the logger output - not each fired event should be logged as default. More reading on logging available at https://devdocs.jabref.org/code-howtos/logging.html.

Finally, I fixed the patterns for (2) suffixes etc. There should be tests, but I think I captered this wish at https://github.com/JabRef/jabref-koppor/issues/719.

koppor avatar Aug 23 '25 01:08 koppor

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

trag-bot[bot] avatar Aug 23 '25 01:08 trag-bot[bot]

Introduces some test failres on Windows, but I don't know yet why. Just documenting --> https://github.com/JabRef/jabref/issues/13766

koppor avatar Aug 27 '25 11:08 koppor