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
10 changes: 10 additions & 0 deletions src/main/java/org/jabref/logic/cleanup/RenamePdfCleanup.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ public RenamePdfCleanup(boolean onlyRelativePaths, Supplier<BibDatabaseContext>
this.filePreferences = filePreferences;
}

private boolean allowedFileType(String filePath) {
String lowerCase = filePath.toLowerCase();

return lowerCase.endsWith(".pdf");
Copy link
Collaborator

Choose a reason for hiding this comment

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

Use

public static boolean isPDFFile(Path file) {

}

@Override
public List<FieldChange> cleanup(BibEntry entry) {
List<LinkedFile> files = entry.getFiles();
Expand All @@ -41,6 +47,10 @@ public List<FieldChange> cleanup(BibEntry entry) {
if (onlyRelativePaths && Path.of(file.getLink()).isAbsolute()) {
continue;
}
if (!allowedFileType(file.getLink())) {
LOGGER.info("Skipping renaming: {}", file.getLink());
continue;
}

LinkedFileHandler fileHandler = new LinkedFileHandler(file, entry, databaseContext.get(), filePreferences);
try {
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
12 changes: 6 additions & 6 deletions src/test/java/org/jabref/logic/cleanup/RenamePdfCleanupTest.java
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,13 +63,13 @@ 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));
}

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

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

@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