Skip to content

Commit

Permalink
feat: Add URL reachability check in ConvertWebsiteToPDF
Browse files Browse the repository at this point in the history
  • Loading branch information
sorydi3 committed Aug 8, 2024
1 parent bfe38c7 commit d716b66
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public ResponseEntity<byte[]> urlToPdf(@ModelAttribute UrlToPdfRequest request)
if (!URL.matches("^https?://.*") || !GeneralUtils.isValidURL(URL)) {
throw new IllegalArgumentException("Invalid URL format provided.");
}

// validate the URL is reachable
if (!GeneralUtils.isURLReachable(URL)) {
throw new IllegalArgumentException("URL is not reachable, please provide a valid URL.");
}

Path tempOutputFile = null;
byte[] pdfBytes;
try {
Expand Down
42 changes: 29 additions & 13 deletions src/main/java/stirling/software/SPDF/utils/GeneralUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import java.nio.file.attribute.BasicFileAttributes;
import java.util.ArrayList;
import java.util.List;
import java.net.URL;
import java.net.HttpURLConnection;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -71,6 +73,21 @@ public static boolean isValidURL(String urlStr) {
} catch (MalformedURLException e) {
return false;
}

}

public static boolean isURLReachable(String urlStr) {
try {
URL url = new URL(urlStr);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("HEAD");
int responseCode = connection.getResponseCode();
return (200 <= responseCode && responseCode <= 399);
} catch (MalformedURLException e) {
return false;
} catch (IOException e) {
return false;
}
}

public static File multipartToFile(MultipartFile multipart) throws IOException {
Expand All @@ -95,19 +112,16 @@ public static Long convertSizeToBytes(String sizeStr) {
sizeStr = sizeStr.replace(",", ".").replace(" ", "");
try {
if (sizeStr.endsWith("KB")) {
return (long)
(Double.parseDouble(sizeStr.substring(0, sizeStr.length() - 2)) * 1024);
return (long) (Double.parseDouble(sizeStr.substring(0, sizeStr.length() - 2)) * 1024);
} else if (sizeStr.endsWith("MB")) {
return (long)
(Double.parseDouble(sizeStr.substring(0, sizeStr.length() - 2))
* 1024
* 1024);
return (long) (Double.parseDouble(sizeStr.substring(0, sizeStr.length() - 2))
* 1024
* 1024);
} else if (sizeStr.endsWith("GB")) {
return (long)
(Double.parseDouble(sizeStr.substring(0, sizeStr.length() - 2))
* 1024
* 1024
* 1024);
return (long) (Double.parseDouble(sizeStr.substring(0, sizeStr.length() - 2))
* 1024
* 1024
* 1024);
} else if (sizeStr.endsWith("B")) {
return Long.parseLong(sizeStr.substring(0, sizeStr.length() - 1));
} else {
Expand Down Expand Up @@ -170,13 +184,15 @@ public static List<Integer> evaluateNFunc(String expression, int maxValue) {

int n = 0;
while (true) {
// Replace 'n' with the current value of n, correctly handling numbers before 'n'
// Replace 'n' with the current value of n, correctly handling numbers before
// 'n'
String sanitizedExpression = insertMultiplicationBeforeN(expression, n);
Double result = evaluator.evaluate(sanitizedExpression);

// Check if the result is null or not within bounds
if (result == null || result <= 0 || result.intValue() > maxValue) {
if (n != 0) break;
if (n != 0)
break;
} else {
results.add(result.intValue());
}
Expand Down

0 comments on commit d716b66

Please sign in to comment.