diff options
author | lookshe <github@lookshe.org> | 2015-04-12 23:09:53 +0200 |
---|---|---|
committer | lookshe <github@lookshe.org> | 2015-04-12 23:09:53 +0200 |
commit | 3ef609cfbda8117414351330216a9e74ac5da4a5 (patch) | |
tree | adb6bab2ce91028f31cea8f33cba5694a69ea9e0 /src | |
parent | 03bc7dcdc9cc3436412fa4fd7db507472763ad86 (diff) |
better and perhaps faster way for checking if a message is downloadable
Diffstat (limited to 'src')
-rw-r--r-- | src/main/java/eu/siacs/conversations/entities/Message.java | 28 |
1 files changed, 18 insertions, 10 deletions
diff --git a/src/main/java/eu/siacs/conversations/entities/Message.java b/src/main/java/eu/siacs/conversations/entities/Message.java index fc2fdafd..a8018088 100644 --- a/src/main/java/eu/siacs/conversations/entities/Message.java +++ b/src/main/java/eu/siacs/conversations/entities/Message.java @@ -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) { - return false; - } - String[] pathParts = url.getPath().split("/"); - String filename; - if (pathParts.length > 0) { - filename = pathParts[pathParts.length - 1].toLowerCase(); - } else { + + String sUrlPath = url.getPath(); + if (sUrlPath == null || sUrlPath.isEmpty()) { 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])) { |