Fix throwing NullPointerException when getting a fileName with a query param.

downgrade-to-1.8
Nipuni Kavindya 1 month ago
parent 16b7c4f3e7
commit 5853e529c7

@ -132,9 +132,21 @@ public class FileDownloaderServiceProvider {
} }
/** /**
* Extracting file name segments by parsing the URL * Extracts file name segments (name and extension) by parsing the given URL.
* @param url Remote URL to extract file name segments * This method handles two types of URL formats:
* @return Array containing file name segments or null when failed to extract * - If the URL includes a query parameter in the format `?fileName=`, the file name
* is extracted from this query parameter (ex: when referencing an existing
* screenshot or icon from the main release)
* - If the URL does not have the `fileName` query parameter, the method attempts to
* extract the file name from the URL path. (ex: this applies to cases where new files are
* uploaded, and only a path-based URL is provided)
* After locating the file name (from either the query parameter or path), the method
* splits the name into segments based on the last dot (`.`), returning the base name and
* extension as a two-element array. If file name cannot be extracted, `null` is returned.
*
* @param url Remote URL to extract file name segments from, which may contain a file name
* as either a query parameter (`fileName=...`) or in the path.
* @return An array containing the file name and extension segments, or null if extraction fails.
*/ */
public static String[] extractFileNameSegmentsFromUrl(URL url) { public static String[] extractFileNameSegmentsFromUrl(URL url) {
if (url == null) { if (url == null) {
@ -143,24 +155,35 @@ public class FileDownloaderServiceProvider {
} }
return null; return null;
} }
String fullQualifiedName = null;
String []urlSegments = url.toString().split("/"); String query = url.getQuery();
if (urlSegments.length < 1) { if (query != null && query.startsWith("fileName=")) {
if (log.isDebugEnabled()) { String[] queryParts = query.split("=", 2);
log.debug("Cannot determine the file name for the remote file"); if (queryParts.length > 1 && !queryParts[1].isEmpty()) {
fullQualifiedName = queryParts[1];
} }
return null;
} }
if (fullQualifiedName == null) {
String fullQualifiedName = urlSegments[urlSegments.length - 1]; String[] urlSegments = url.getPath().split("/");
String []fileNameSegments = fullQualifiedName.split("\\.(?=[^.]+$)"); if (urlSegments.length > 0) {
if (fileNameSegments.length != 2) { fullQualifiedName = urlSegments[urlSegments.length - 1];
}
}
if (fullQualifiedName != null) {
String[] fileNameSegments = fullQualifiedName.split("\\.(?=[^.]+$)");
if (fileNameSegments.length == 2) {
return fileNameSegments;
} else {
if (log.isDebugEnabled()) {
log.debug("Error encountered when constructing file name");
}
}
} else {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("Error encountered when constructing file name"); log.debug("Error encountered when constructing file name");
} }
return null;
} }
return fileNameSegments; return null;
} }
/** /**

@ -183,6 +183,15 @@ public class FileTransferServiceHelperUtil {
return fileDescriptorResolvedFromRelease; return fileDescriptorResolvedFromRelease;
} }
String file = urlPathSegments[urlPathSegments.length - 1];
String query = downloadUrl.getQuery();
if (query != null && query.startsWith("fileName=")) {
String[] queryParts = query.split("=", 2);
if (queryParts.length > 1 && !queryParts[1].isEmpty()) {
file = queryParts[1];
}
}
if (urlPathSegments.length < 2) { if (urlPathSegments.length < 2) {
if (log.isDebugEnabled()) { if (log.isDebugEnabled()) {
log.debug("URL patch segments contain less than 2 segments"); log.debug("URL patch segments contain less than 2 segments");
@ -190,7 +199,6 @@ public class FileTransferServiceHelperUtil {
return null; return null;
} }
String file = urlPathSegments[urlPathSegments.length - 1];
String artifactHolder = urlPathSegments[urlPathSegments.length - 2]; String artifactHolder = urlPathSegments[urlPathSegments.length - 2];
try { try {
FileDescriptor fileDescriptor = new FileDescriptor(); FileDescriptor fileDescriptor = new FileDescriptor();

Loading…
Cancel
Save