Skip to content

Commit 9d09b24

Browse files
committed
refactor
Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
1 parent dd8d358 commit 9d09b24

File tree

5 files changed

+20
-10
lines changed

5 files changed

+20
-10
lines changed

core/src/main/java/org/apache/cloudstack/direct/download/HttpsDirectTemplateDownloader.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,7 @@ public Long getRemoteFileSize(String url, String format) {
186186
SSLContext context = getSSLContext();
187187
urlConnection.setSSLSocketFactory(context.getSocketFactory());
188188
urlConnection.connect();
189-
boolean isCompressed = UriUtils.COMMPRESSION_FORMATS.stream().anyMatch(f -> !url.toLowerCase().endsWith(f));
190-
return QCOW2Utils.getVirtualSize(urlObj.openStream(), isCompressed);
189+
return QCOW2Utils.getVirtualSize(urlObj.openStream(), UriUtils.isUrlForCompressedFile(url));
191190
} catch (IOException e) {
192191
throw new CloudRuntimeException(String.format("Cannot obtain qcow2 virtual size due to: %s", e.getMessage()), e);
193192
}

utils/src/main/java/com/cloud/utils/UriUtils.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -408,14 +408,14 @@ public static List<String> getMetalinkUrls(String metalinkUrl) {
408408
return urls;
409409
}
410410

411-
public static final Set<String> COMMPRESSION_FORMATS = ImmutableSet.of("zip", "bz2", "gz");
411+
public static final Set<String> COMPRESSION_FORMATS = ImmutableSet.of("zip", "bz2", "gz");
412412

413413
public static final Set<String> buildExtensionSet(boolean metalink, String... baseExtensions) {
414414
final ImmutableSet.Builder<String> builder = ImmutableSet.builder();
415415

416416
for (String baseExtension : baseExtensions) {
417417
builder.add("." + baseExtension);
418-
for (String format : COMMPRESSION_FORMATS) {
418+
for (String format : COMPRESSION_FORMATS) {
419419
builder.add("." + baseExtension + "." + format);
420420
}
421421
}
@@ -647,4 +647,8 @@ private static UriInfo getRbdUrlInfo(String url) {
647647
throw new CloudRuntimeException(url + " is not a valid uri for RBD");
648648
}
649649
}
650+
651+
public static boolean isUrlForCompressedFile(String url) {
652+
return UriUtils.COMPRESSION_FORMATS.stream().anyMatch(f -> url.toLowerCase().endsWith(f));
653+
}
650654
}

utils/src/main/java/com/cloud/utils/storage/QCOW2Utils.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,7 @@ private static long getVirtualSizeFromInputStream(InputStream inputStream) throw
115115
public static long getVirtualSize(String urlStr) {
116116
try {
117117
URL url = new URL(urlStr);
118-
boolean isCompressed = UriUtils.COMMPRESSION_FORMATS.stream().anyMatch(f -> !urlStr.toLowerCase().endsWith(f));
119-
return getVirtualSize(url.openStream(), isCompressed);
118+
return getVirtualSize(url.openStream(), UriUtils.isUrlForCompressedFile(urlStr));
120119
} catch (MalformedURLException e) {
121120
LOGGER.warn("Failed to validate for qcow2, malformed URL: " + urlStr + ", error: " + e.getMessage());
122121
throw new IllegalArgumentException("Invalid URL: " + urlStr);

utils/src/main/java/org/apache/cloudstack/utils/imagestore/ImageStoreUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public static boolean isCorrectExtension(String path, String format) {
101101

102102
public static boolean isCompressedExtension(String path) {
103103
final String lowerCasePath = path.toLowerCase();
104-
return UriUtils.COMMPRESSION_FORMATS
104+
return UriUtils.COMPRESSION_FORMATS
105105
.stream()
106106
.map(extension -> "." + extension)
107107
.anyMatch(lowerCasePath::endsWith);

utils/src/test/java/com/cloud/utils/UriUtilsTest.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919

2020
package com.cloud.utils;
2121

22-
import org.junit.Assert;
23-
import org.junit.Test;
24-
2522
import java.util.Arrays;
2623
import java.util.Collections;
2724
import java.util.List;
2825

26+
import org.junit.Assert;
27+
import org.junit.Test;
28+
2929
public class UriUtilsTest {
3030
@Test
3131
public void encodeURIComponent() {
@@ -265,4 +265,12 @@ public void testGetUriInfoIpv6() {
265265
testGetUriInfoInternal(url11, host);
266266
testGetUriInfoInternal(url12, host);
267267
}
268+
269+
@Test
270+
public void testIsUrlForCompressedFile() {
271+
Assert.assertTrue(UriUtils.isUrlForCompressedFile("https://abc.com/xyz.bz2"));
272+
Assert.assertTrue(UriUtils.isUrlForCompressedFile("http://abc.com/xyz.zip"));
273+
Assert.assertTrue(UriUtils.isUrlForCompressedFile("https://abc.com/xyz.gz"));
274+
Assert.assertFalse(UriUtils.isUrlForCompressedFile("http://abc.com/xyz.qcow2"));
275+
}
268276
}

0 commit comments

Comments
 (0)