Skip to content

Commit

Permalink
made the requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
laky241 committed Jan 22, 2025
1 parent 3f3ddcb commit 4814a59
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
20 changes: 14 additions & 6 deletions src/main/java/org/jabref/logic/cleanup/RenamePdfCleanup.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

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 @@ -34,9 +33,7 @@ public RenamePdfCleanup(boolean onlyRelativePaths, Supplier<BibDatabaseContext>
}

private boolean allowedFileType(String fileName) {
Path file = Path.of(fileName);

return FileUtil.isPDFFile(file);
return true;
}

@Override
Expand All @@ -48,11 +45,22 @@ public List<FieldChange> cleanup(BibEntry entry) {
if (onlyRelativePaths && Path.of(file.getLink()).isAbsolute()) {
continue;
}
if (!allowedFileType(file.getLink())) {
LOGGER.info("Skipping renaming: {}", file.getLink());

String fullName = Path.of(file.getLink()).getFileName().toString();
int dot = fullName.lastIndexOf('.');
if (dot == -1) {
continue;
}
String extension = fullName.substring(dot + 1);
String baseName = fullName.substring(0, dot);
String newCitationKey = entry.getCitationKey().orElse("");

String newBaseName = newCitationKey;
int dash = baseName.indexOf('-');
if (dash != -1) {
newBaseName += baseName.substring(dash);
}
String newFileName = newBaseName + "." + extension;
LinkedFileHandler fileHandler = new LinkedFileHandler(file, entry, databaseContext.get(), filePreferences);
try {
boolean changedFile = fileHandler.renameToSuggestedName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void cleanupRenamePdfRenamesFileEvenIfOnlyDifferenceIsCase() throws IOException

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

entry.setField(StandardField.TITLE, "test title");
Expand All @@ -85,7 +85,7 @@ void cleanupRenamePdfRenamesWithMultipleFiles() throws IOException {
assertEquals(Optional.of(FileFieldWriter.getStringRepresentation(
Arrays.asList(
new LinkedFile("", Path.of(""), ""),
new LinkedFile("", Path.of("Toot - test title.pdf"), ""),
new LinkedFile("", Path.of("Toot - test title.tmp"), ""),
new LinkedFile("", Path.of(""), "")))),
entry.getField(StandardField.FILE));
}
Expand Down

0 comments on commit 4814a59

Please sign in to comment.