From 74c496fe3ef544d45c94365799c061ddfe898330 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Thu, 31 Mar 2016 21:56:59 +0200 Subject: [PATCH] add methods to check max file size for http upload --- .../siacs/conversations/entities/Account.java | 6 +++- .../entities/ServiceDiscoveryResult.java | 13 +++++++++ .../services/XmppConnectionService.java | 3 +- .../conversations/ui/EditAccountActivity.java | 2 +- .../conversations/xmpp/XmppConnection.java | 29 ++++++++++++++----- 5 files changed, 41 insertions(+), 12 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/entities/Account.java b/src/main/java/eu/siacs/conversations/entities/Account.java index 356b34e57..2356ffb94 100644 --- a/src/main/java/eu/siacs/conversations/entities/Account.java +++ b/src/main/java/eu/siacs/conversations/entities/Account.java @@ -52,8 +52,12 @@ public class Account extends AbstractEntity { public static final int OPTION_USECOMPRESSION = 3; public final HashSet> inProgressDiscoFetches = new HashSet<>(); + public boolean httpUploadAvailable(long filesize) { + return xmppConnection != null && xmppConnection.getFeatures().httpUpload(filesize); + } + public boolean httpUploadAvailable() { - return xmppConnection != null && xmppConnection.getFeatures().httpUpload(); + return httpUploadAvailable(0); } public void setDisplayName(String displayName) { diff --git a/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java b/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java index 31570d2d6..c4fc30abc 100644 --- a/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java +++ b/src/main/java/eu/siacs/conversations/entities/ServiceDiscoveryResult.java @@ -190,6 +190,19 @@ public class ServiceDiscoveryResult { return false; } + public String getExtendedDiscoInformation(String formType, String name) { + for(Data form : this.forms) { + if (formType.equals(form.getFormType())) { + for(Field field: form.getFields()) { + if (name.equals(field.getFieldName())) { + return field.getValue(); + } + } + } + } + return null; + } + protected byte[] mkCapHash() { StringBuilder s = new StringBuilder(); diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index cc0a8f703..1d7a4600e 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -847,8 +847,7 @@ public class XmppConnectionService extends Service implements OnPhoneContactsLoa private void sendFileMessage(final Message message, final boolean delay) { Log.d(Config.LOGTAG, "send file message"); final Account account = message.getConversation().getAccount(); - final XmppConnection connection = account.getXmppConnection(); - if (connection != null && connection.getFeatures().httpUpload()) { + if (account.httpUploadAvailable()) { mHttpConnectionManager.createNewUploadConnection(message, delay); } else { mJingleConnectionManager.createNewConnection(message); diff --git a/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java b/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java index ed80a509f..9d73290a5 100644 --- a/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java +++ b/src/main/java/eu/siacs/conversations/ui/EditAccountActivity.java @@ -688,7 +688,7 @@ public class EditAccountActivity extends XmppActivity implements OnAccountUpdate } else { this.mServerInfoPep.setText(R.string.server_info_unavailable); } - if (features.httpUpload()) { + if (features.httpUpload(0)) { this.mServerInfoHttpUpload.setText(R.string.server_info_available); } else { this.mServerInfoHttpUpload.setText(R.string.server_info_unavailable); diff --git a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java index fd0e355d1..1552fe1a8 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/XmppConnection.java @@ -1346,12 +1346,12 @@ public class XmppConnection implements Runnable { this.streamId = null; } - public List findDiscoItemsByFeature(final String feature) { + private List> findDiscoItemsByFeature(final String feature) { synchronized (this.disco) { - final List items = new ArrayList<>(); + final List> items = new ArrayList<>(); for (final Entry cursor : this.disco.entrySet()) { if (cursor.getValue().getFeatures().contains(feature)) { - items.add(cursor.getKey()); + items.add(cursor); } } return items; @@ -1359,9 +1359,9 @@ public class XmppConnection implements Runnable { } public Jid findDiscoItemByFeature(final String feature) { - final List items = findDiscoItemsByFeature(feature); + final List> items = findDiscoItemsByFeature(feature); if (items.size() >= 1) { - return items.get(0); + return items.get(0).getKey(); } return null; } @@ -1505,7 +1505,6 @@ public class XmppConnection implements Runnable { public boolean pep() { synchronized (XmppConnection.this.disco) { - final Pair needle = new Pair<>("pubsub", "pep"); ServiceDiscoveryResult info = disco.get(account.getServer()); if (info != null && info.hasIdentity("pubsub", "pep")) { return true; @@ -1534,8 +1533,22 @@ public class XmppConnection implements Runnable { this.blockListRequested = value; } - public boolean httpUpload() { - return !Config.DISABLE_HTTP_UPLOAD && findDiscoItemsByFeature(Xmlns.HTTP_UPLOAD).size() > 0; + public boolean httpUpload(long filesize) { + if (Config.DISABLE_HTTP_UPLOAD) { + return false; + } else { + List> items = findDiscoItemsByFeature(Xmlns.HTTP_UPLOAD); + if (items.size() > 0) { + try { + long maxsize = Long.parseLong(items.get(0).getValue().getExtendedDiscoInformation(Xmlns.HTTP_UPLOAD, "max-file-size")); + return maxsize <= filesize; + } catch (Exception e) { + return filesize <= 0; + } + } else { + return false; + } + } } }