Skip to content
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

fixed the cleanup renaming which will only rename ".pdf" files. #12396

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
22 changes: 22 additions & 0 deletions src/main/java/org/jabref/logic/cleanup/RenamePdfCleanup.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

import org.jabref.logic.FilePreferences;
import org.jabref.logic.externalfiles.LinkedFileHandler;
import org.jabref.logic.util.io.FileUtil;
import org.jabref.model.FieldChange;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
Expand All @@ -32,6 +33,10 @@ public RenamePdfCleanup(boolean onlyRelativePaths, Supplier<BibDatabaseContext>
this.filePreferences = filePreferences;
}

private boolean allowedFileType(String fileName) {
return true;
}

@Override
public List<FieldChange> cleanup(BibEntry entry) {
List<LinkedFile> files = entry.getFiles();
Expand All @@ -42,6 +47,23 @@ public List<FieldChange> cleanup(BibEntry entry) {
continue;
}

String fullName = Path.of(file.getLink()).getFileName().toString();
Optional<String> extension = FileUtil.getFileExtension(fullName);
String baseName = FileUtil.getBaseName(fullName);

if (extension.isEmpty()) {
LOGGER.info(" No extension found ");
continue;
}
String extensionFinal = extension.get();
String newCitationKey = entry.getCitationKey().orElse("");

String newBaseName = newCitationKey;
int dash = baseName.indexOf('-');
if (dash != -1) {
newBaseName += baseName.substring(dash);
}
String newFileName = newBaseName + "." + extensionFinal;
LinkedFileHandler fileHandler = new LinkedFileHandler(file, entry, databaseContext.get(), filePreferences);
try {
boolean changedFile = fileHandler.renameToSuggestedName();
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/jabref/logic/cleanup/CleanupWorkerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -253,15 +253,15 @@ void cleanupRelativePathsConvertAbsoluteToRelativePath() throws IOException {
void cleanupRenamePdfRenamesRelativeFile() throws IOException {
CleanupPreferences preset = new CleanupPreferences(CleanupPreferences.CleanupStep.RENAME_PDF);

Path path = pdfPath.resolve("AnotherRandomlyNamedFile.tmp");
Path path = pdfPath.resolve("AnotherRandomlyNamedFile.pdf");
Files.createFile(path);
BibEntry entry = new BibEntry()
.withCitationKey("Toot");
LinkedFile fileField = new LinkedFile("", path.toAbsolutePath(), "");
entry.setField(StandardField.FILE, FileFieldWriter.getStringRepresentation(fileField));

worker.cleanup(preset, entry);
LinkedFile newFileField = new LinkedFile("", Path.of("Toot.tmp"), "");
LinkedFile newFileField = new LinkedFile("", Path.of("Toot.pdf"), "");
assertEquals(Optional.of(FileFieldWriter.getStringRepresentation(newFileField)), entry.getField(StandardField.FILE));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ void setUp(@TempDir Path testFolder) {
*/
@Test
void cleanupRenamePdfRenamesFileEvenIfOnlyDifferenceIsCase() throws IOException {
Path path = testFolder.resolve("toot.tmp");
Path path = testFolder.resolve("toot.pdf");
Files.createFile(path);

LinkedFile fileField = new LinkedFile("", path.toAbsolutePath(), "");
Expand All @@ -63,7 +63,7 @@ void cleanupRenamePdfRenamesFileEvenIfOnlyDifferenceIsCase() throws IOException
when(filePreferences.getFileNamePattern()).thenReturn("[citationkey]");
cleanup.cleanup(entry);

LinkedFile newFileField = new LinkedFile("", Path.of("Toot.tmp"), "");
LinkedFile newFileField = new LinkedFile("", Path.of("Toot.pdf"), "");
assertEquals(Optional.of(FileFieldWriter.getStringRepresentation(newFileField)), entry.getField(StandardField.FILE));
}

Expand Down Expand Up @@ -92,7 +92,7 @@ void cleanupRenamePdfRenamesWithMultipleFiles() throws IOException {

@Test
void cleanupRenamePdfRenamesFileStartingWithCitationKey() throws IOException {
Path path = testFolder.resolve("Toot.tmp");
Path path = testFolder.resolve("Toot.pdf");
Files.createFile(path);

LinkedFile fileField = new LinkedFile("", path.toAbsolutePath(), "");
Expand All @@ -102,7 +102,7 @@ void cleanupRenamePdfRenamesFileStartingWithCitationKey() throws IOException {
when(filePreferences.getFileNamePattern()).thenReturn("[citationkey] - [fulltitle]");
cleanup.cleanup(entry);

LinkedFile newFileField = new LinkedFile("", Path.of("Toot - test title.tmp"), "");
LinkedFile newFileField = new LinkedFile("", Path.of("Toot - test title.pdf"), "");
assertEquals(Optional.of(FileFieldWriter.getStringRepresentation(newFileField)), entry.getField(StandardField.FILE));
}

Expand Down
Loading