diff --git a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java index 708e2630432dfc4049d0791f2b80d31d5c4554c1..8d5db0fea44831974669d1032a3ea89eb0680c6e 100644 --- a/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java @@ -783,6 +783,17 @@ public class DatabaseBackend extends SQLiteOpenHelper { return f; } + public String getUrlForCid(Cid cid) { + SQLiteDatabase db = this.getReadableDatabase(); + Cursor cursor = db.query("cheogram.cids", new String[]{"url"}, "cid=?", new String[]{cid.toString()}, null, null, null); + String url = null; + if (cursor.moveToNext()) { + url = cursor.getString(0); + } + cursor.close(); + return url; + } + public void saveCid(Cid cid, File file) { saveCid(cid, file, null); } diff --git a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java index 4adf756a67b3075d9a6223ddeb3156e06668c64e..46c91a44aece9161c65bd5017445f912790d426d 100644 --- a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java @@ -654,6 +654,9 @@ public class FileBackend { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; try { + for (Cid cid : calculateCids(uri)) { + if (mXmppConnectionService.getUrlForCid(cid) != null) return true; + } final InputStream inputStream = mXmppConnectionService.getContentResolver().openInputStream(uri); BitmapFactory.decodeStream(inputStream, null, options); @@ -664,7 +667,7 @@ public class FileBackend { return (options.outWidth <= Config.IMAGE_SIZE && options.outHeight <= Config.IMAGE_SIZE && options.outMimeType.contains(Config.IMAGE_FORMAT.name().toLowerCase())); - } catch (FileNotFoundException e) { + } catch (final IOException e) { Log.d(Config.LOGTAG, "unable to get image dimensions", e); return false; } @@ -927,6 +930,10 @@ public class FileBackend { } } + public Cid[] calculateCids(final Uri uri) throws IOException { + return calculateCids(mXmppConnectionService.getContentResolver().openInputStream(uri)); + } + public Cid[] calculateCids(final InputStream is) throws IOException { try { return CryptoHelper.cid(is, new String[]{"SHA-256", "SHA-1", "SHA-512"}); @@ -1727,11 +1734,15 @@ public class FileBackend { updateFileParams(message, url, true); } - public void updateFileParams(final Message message, final String url, boolean updateCids) { + public void updateFileParams(final Message message, String url, boolean updateCids) { final boolean encrypted = message.getEncryption() == Message.ENCRYPTION_PGP || message.getEncryption() == Message.ENCRYPTION_DECRYPTED; final DownloadableFile file = getFile(message); + Cid[] cids = new Cid[0]; + try { + cids = calculateCids(new FileInputStream(file)); + } catch (final IOException e) { } final String mime = file.getMimeType(); final boolean privateMessage = message.isPrivateMessage(); final boolean image = @@ -1739,7 +1750,15 @@ public class FileBackend { || (mime != null && mime.startsWith("image/")); Message.FileParams fileParams = message.getFileParams(); if (fileParams == null) fileParams = new Message.FileParams(); - if (url != null) { + if (url == null) { + for (Cid cid : cids) { + url = mXmppConnectionService.getUrlForCid(cid); + if (url != null) { + fileParams.url = url; + break; + } + } + } else { fileParams.url = url; } if (encrypted && !file.exists()) { @@ -1801,11 +1820,10 @@ public class FileBackend { if (updateCids) { try { - Cid[] cids = calculateCids(new FileInputStream(getFile(message))); for (int i = 0; i < cids.length; i++) { - mXmppConnectionService.saveCid(cids[i], file); + mXmppConnectionService.saveCid(cids[i], file, url); } - } catch (final IOException | XmppConnectionService.BlockedMediaException e) { } + } catch (XmppConnectionService.BlockedMediaException e) { } } } diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 974b0c2b484a783df7172cadeafaef9fcd3e8137..22e545295673823a424475519390573ec7fdbbed 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -561,6 +561,10 @@ public class XmppConnectionService extends Service { return this.databaseBackend.getFileForCid(cid); } + public String getUrlForCid(Cid cid) { + return this.databaseBackend.getUrlForCid(cid); + } + public void saveCid(Cid cid, File file) throws BlockedMediaException { saveCid(cid, file, null); } @@ -1614,6 +1618,9 @@ public class XmppConnectionService extends Service { final boolean inProgressJoin = isJoinInProgress(conversation); + if (message.getCounterpart() == null && !message.isPrivateMessage()) { + message.setCounterpart(message.getConversation().getJid().asBareJid()); + } if (account.isOnlineAndConnected() && !inProgressJoin) { switch (message.getEncryption()) {