better and perhaps faster way for checking if a message is downloadable

This commit is contained in:
lookshe 2015-04-12 23:09:53 +02:00
parent 03bc7dcdc9
commit 3ef609cfbd

View file

@ -430,23 +430,31 @@ public class Message extends AbstractEntity {
}
public boolean bodyContainsDownloadable() {
/**
* there are a few cases where spaces result in an unwanted behavior, e.g.
* "http://upload.mitsu-freunde-bw.de/uploads/2015/03/i43b4bpr8.png /abc.png"
* or more than one image link in one message.
*/
if (body.contains(" ")) {
return false;
}
try {
URL url = new URL(this.getBody());
URL url = new URL(body);
if (!url.getProtocol().equalsIgnoreCase("http")
&& !url.getProtocol().equalsIgnoreCase("https")) {
return false;
}
if (url.getPath() == null) {
String sUrlPath = url.getPath();
if (sUrlPath == null || sUrlPath.isEmpty()) {
return false;
}
String[] pathParts = url.getPath().split("/");
String filename;
if (pathParts.length > 0) {
filename = pathParts[pathParts.length - 1].toLowerCase();
} else {
return false;
}
String[] extensionParts = filename.split("\\.");
int iSlashIndex = sUrlPath.lastIndexOf('/') + 1;
String sLastUrlPath = sUrlPath.substring(iSlashIndex);
String[] extensionParts = sLastUrlPath.split("\\.");
if (extensionParts.length == 2
&& Arrays.asList(Downloadable.VALID_IMAGE_EXTENSIONS).contains(
extensionParts[extensionParts.length - 1])) {