Skip to content

Commit 049c3e1

Browse files
committed
Move getFileNameFromURL helper method to URLUtil class
1 parent efa619c commit 049c3e1

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

src/main/java/org/jabref/gui/externalfiles/ImportHandler.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ private List<BibEntry> handlePdfUrl(String pdfUrl) throws IOException {
469469
return List.of();
470470
}
471471
URLDownload urlDownload = new URLDownload(pdfUrl);
472-
String filename = deriveFileNameFromUrl(pdfUrl);
472+
String filename = URLUtil.getFileNameFromUrl(pdfUrl);
473473
Path targetFile = targetDirectory.get().resolve(filename);
474474
try {
475475
urlDownload.toFile(targetFile);
@@ -501,12 +501,4 @@ private List<BibEntry> handlePdfUrl(String pdfUrl) throws IOException {
501501
return List.of();
502502
}
503503
}
504-
505-
private String deriveFileNameFromUrl(String url) {
506-
String fileName = url.substring(url.lastIndexOf('/') + 1);
507-
if (fileName.isBlank()) {
508-
fileName = "downloaded.pdf";
509-
}
510-
return FileUtil.getValidFileName(fileName);
511-
}
512504
}

src/main/java/org/jabref/logic/util/URLUtil.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import java.util.Objects;
1010
import java.util.regex.Pattern;
1111

12+
import org.jabref.logic.util.io.FileUtil;
13+
1214
/**
1315
* URL utilities for URLs in the JabRef logic.
1416
* <p>
@@ -122,4 +124,19 @@ public static URI createUri(String url) {
122124
throw new IllegalArgumentException(e);
123125
}
124126
}
127+
128+
/**
129+
* Extracts the filename from a URL.
130+
* If the URL doesn't have a filename (ends with '/'), returns a default name.
131+
*
132+
* @param url the URL string to extract the filename from
133+
* @return the extracted filename or a default name if none found
134+
*/
135+
public static String getFileNameFromUrl(String url) {
136+
String fileName = url.substring(url.lastIndexOf('/') + 1);
137+
if (fileName.isBlank()) {
138+
fileName = "downloaded.pdf";
139+
}
140+
return FileUtil.getValidFileName(fileName);
141+
}
125142
}

0 commit comments

Comments
 (0)