~singpolyma/cheogram-android

f5b01ad99af32782b8a380aaab84821f5d217d58 — Stephen Paul Weber 3 months ago ae4e3a0
Send cid with known URL without uploading
M src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java => src/main/java/eu/siacs/conversations/persistance/DatabaseBackend.java +11 -0
@@ 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);
    }

M src/main/java/eu/siacs/conversations/persistance/FileBackend.java => src/main/java/eu/siacs/conversations/persistance/FileBackend.java +24 -6
@@ 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) { }
        }
    }


M src/main/java/eu/siacs/conversations/services/XmppConnectionService.java => src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +7 -0
@@ 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()) {