refactored bodyContainsDownloadable to be more flexible

This commit is contained in:
Daniel Gultsch 2015-06-30 13:52:53 +02:00
parent 5b42b392a1
commit d7de311379
5 changed files with 40 additions and 38 deletions

View file

@ -59,7 +59,7 @@ public class PgpEngine {
message.setEncryption(Message.ENCRYPTION_DECRYPTED); message.setEncryption(Message.ENCRYPTION_DECRYPTED);
final HttpConnectionManager manager = mXmppConnectionService.getHttpConnectionManager(); final HttpConnectionManager manager = mXmppConnectionService.getHttpConnectionManager();
if (message.trusted() if (message.trusted()
&& message.bodyContainsDownloadable() && message.treatAsDownloadable() == Message.Decision.YES
&& manager.getAutoAcceptFileSize() > 0) { && manager.getAutoAcceptFileSize() > 0) {
manager.createNewConnection(message); manager.createNewConnection(message);
} }

View file

@ -375,8 +375,8 @@ public class Message extends AbstractEntity {
(message.getTimeSent() - this.getTimeSent()) <= (Config.MESSAGE_MERGE_WINDOW * 1000) && (message.getTimeSent() - this.getTimeSent()) <= (Config.MESSAGE_MERGE_WINDOW * 1000) &&
!GeoHelper.isGeoUri(message.getBody()) && !GeoHelper.isGeoUri(message.getBody()) &&
!GeoHelper.isGeoUri(this.body) && !GeoHelper.isGeoUri(this.body) &&
!message.bodyContainsDownloadable() && message.treatAsDownloadable() == Decision.NO &&
!this.bodyContainsDownloadable() && this.treatAsDownloadable() == Decision.NO &&
!message.getBody().startsWith(ME_COMMAND) && !message.getBody().startsWith(ME_COMMAND) &&
!this.getBody().startsWith(ME_COMMAND) && !this.getBody().startsWith(ME_COMMAND) &&
!this.bodyIsHeart() && !this.bodyIsHeart() &&
@ -434,48 +434,50 @@ public class Message extends AbstractEntity {
return (status > STATUS_RECEIVED || (contact != null && contact.trusted())); return (status > STATUS_RECEIVED || (contact != null && contact.trusted()));
} }
public boolean bodyContainsDownloadable() { public enum Decision {
/** YES,
* there are a few cases where spaces result in an unwanted behavior, e.g. NO,
* "http://example.com/image.jpg text that will not be shown /abc.png" ASK
* or more than one image link in one message. }
*/
public Decision treatAsDownloadable() {
if (body.trim().contains(" ")) { if (body.trim().contains(" ")) {
return false; return Decision.NO;
} }
try { try {
URL url = new URL(body); URL url = new URL(body);
if (!url.getProtocol().equalsIgnoreCase("http") if (!url.getProtocol().equalsIgnoreCase("http") && !url.getProtocol().equalsIgnoreCase("https")) {
&& !url.getProtocol().equalsIgnoreCase("https")) { return Decision.NO;
return false; }
String path = url.getPath();
if (path == null || path.isEmpty()) {
return Decision.NO;
} }
String sUrlPath = url.getPath(); String filename = path.substring(path.lastIndexOf('/') + 1).toLowerCase();
if (sUrlPath == null || sUrlPath.isEmpty()) { String[] extensionParts = filename.split("\\.");
return false; String extension;
} String ref = url.getRef();
if (extensionParts.length == 2) {
int iSlashIndex = sUrlPath.lastIndexOf('/') + 1; extension = extensionParts[extensionParts.length - 1];
} else if (extensionParts.length == 3 && Arrays
String sLastUrlPath = sUrlPath.substring(iSlashIndex).toLowerCase();
String[] extensionParts = sLastUrlPath.split("\\.");
if (extensionParts.length == 2
&& Arrays.asList(Downloadable.VALID_IMAGE_EXTENSIONS).contains(
extensionParts[extensionParts.length - 1])) {
return true;
} else if (extensionParts.length == 3
&& Arrays
.asList(Downloadable.VALID_CRYPTO_EXTENSIONS) .asList(Downloadable.VALID_CRYPTO_EXTENSIONS)
.contains(extensionParts[extensionParts.length - 1]) .contains(extensionParts[extensionParts.length - 1])) {
&& Arrays.asList(Downloadable.VALID_IMAGE_EXTENSIONS).contains( extension = extensionParts[extensionParts.length -2];
extensionParts[extensionParts.length - 2])) {
return true;
} else { } else {
return false; return Decision.NO;
} }
if (Arrays.asList(Downloadable.VALID_IMAGE_EXTENSIONS).contains(extension)) {
return Decision.YES;
} else if (ref != null && ref.matches("([A-Fa-f0-9]{2}){48}")) {
return Decision.ASK;
} else {
return Decision.NO;
}
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
return false; return Decision.NO;
} }
} }

View file

@ -360,7 +360,7 @@ public class MessageParser extends AbstractParser implements
mXmppConnectionService.databaseBackend.createMessage(message); mXmppConnectionService.databaseBackend.createMessage(message);
} }
final HttpConnectionManager manager = this.mXmppConnectionService.getHttpConnectionManager(); final HttpConnectionManager manager = this.mXmppConnectionService.getHttpConnectionManager();
if (message.trusted() && message.bodyContainsDownloadable() && manager.getAutoAcceptFileSize() > 0) { if (message.trusted() && message.treatAsDownloadable() == Message.Decision.YES && manager.getAutoAcceptFileSize() > 0) {
manager.createNewConnection(message); manager.createNewConnection(message);
} else if (!message.isRead()) { } else if (!message.isRead()) {
mXmppConnectionService.getNotificationService().push(message); mXmppConnectionService.getNotificationService().push(message);

View file

@ -497,7 +497,7 @@ public class FileBackend {
message.setBody(url.toString()+"|"+Long.toString(file.getSize()) + '|' + imageWidth + '|' + imageHeight); message.setBody(url.toString()+"|"+Long.toString(file.getSize()) + '|' + imageWidth + '|' + imageHeight);
} }
} else { } else {
message.setBody(Long.toString(file.getSize())); message.setBody(url.toString()+"|"+Long.toString(file.getSize()));
} }
} }

View file

@ -457,7 +457,7 @@ public class ConversationFragment extends Fragment implements EditMessage.Keyboa
} }
if (m.getType() != Message.TYPE_TEXT if (m.getType() != Message.TYPE_TEXT
|| m.getDownloadable() != null || m.getDownloadable() != null
|| !m.bodyContainsDownloadable()) { || m.treatAsDownloadable() == Message.Decision.NO) {
downloadImage.setVisible(false); downloadImage.setVisible(false);
} }
if (!((m.getDownloadable() != null && !(m.getDownloadable() instanceof DownloadablePlaceholder)) if (!((m.getDownloadable() != null && !(m.getDownloadable() instanceof DownloadablePlaceholder))