Search in sources :

Example 1 with SignalServiceAttachmentPointer

use of org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer in project Signal-Android by WhisperSystems.

the class AttachmentDownloadJob method createAttachmentPointer.

@VisibleForTesting
SignalServiceAttachmentPointer createAttachmentPointer(MasterSecret masterSecret, Attachment attachment) throws InvalidPartException {
    if (TextUtils.isEmpty(attachment.getLocation())) {
        throw new InvalidPartException("empty content id");
    }
    if (TextUtils.isEmpty(attachment.getKey())) {
        throw new InvalidPartException("empty encrypted key");
    }
    try {
        AsymmetricMasterSecret asymmetricMasterSecret = MasterSecretUtil.getAsymmetricMasterSecret(context, masterSecret);
        long id = Long.parseLong(attachment.getLocation());
        byte[] key = MediaKey.getDecrypted(masterSecret, asymmetricMasterSecret, attachment.getKey());
        String relay = null;
        if (TextUtils.isEmpty(attachment.getRelay())) {
            relay = attachment.getRelay();
        }
        if (attachment.getDigest() != null) {
            Log.w(TAG, "Downloading attachment with digest: " + Hex.toString(attachment.getDigest()));
        } else {
            Log.w(TAG, "Downloading attachment with no digest...");
        }
        return new SignalServiceAttachmentPointer(id, null, key, relay, Optional.fromNullable(attachment.getDigest()));
    } catch (InvalidMessageException | IOException e) {
        Log.w(TAG, e);
        throw new InvalidPartException(e);
    }
}
Also used : InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) AsymmetricMasterSecret(org.thoughtcrime.securesms.crypto.AsymmetricMasterSecret) SignalServiceAttachmentPointer(org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer) IOException(java.io.IOException) VisibleForTesting(android.support.annotation.VisibleForTesting)

Example 2 with SignalServiceAttachmentPointer

use of org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer in project Signal-Android by WhisperSystems.

the class AttachmentDownloadJob method retrieveAttachment.

private void retrieveAttachment(MasterSecret masterSecret, long messageId, final AttachmentId attachmentId, final Attachment attachment) throws IOException {
    AttachmentDatabase database = DatabaseFactory.getAttachmentDatabase(context);
    File attachmentFile = null;
    try {
        attachmentFile = createTempFile();
        SignalServiceAttachmentPointer pointer = createAttachmentPointer(masterSecret, attachment);
        InputStream stream = messageReceiver.retrieveAttachment(pointer, attachmentFile, MAX_ATTACHMENT_SIZE, new ProgressListener() {

            @Override
            public void onAttachmentProgress(long total, long progress) {
                EventBus.getDefault().postSticky(new PartProgressEvent(attachment, total, progress));
            }
        });
        database.insertAttachmentsForPlaceholder(masterSecret, messageId, attachmentId, stream);
    } catch (InvalidPartException | NonSuccessfulResponseCodeException | InvalidMessageException | MmsException e) {
        Log.w(TAG, e);
        markFailed(messageId, attachmentId);
    } finally {
        if (attachmentFile != null)
            attachmentFile.delete();
    }
}
Also used : InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) InputStream(java.io.InputStream) SignalServiceAttachmentPointer(org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer) NonSuccessfulResponseCodeException(org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException) PartProgressEvent(org.thoughtcrime.securesms.events.PartProgressEvent) AttachmentDatabase(org.thoughtcrime.securesms.database.AttachmentDatabase) MmsException(ws.com.google.android.mms.MmsException) ProgressListener(org.whispersystems.signalservice.api.messages.SignalServiceAttachment.ProgressListener) File(java.io.File)

Example 3 with SignalServiceAttachmentPointer

use of org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer in project libsignal-service-java by signalapp.

the class SignalServiceCipher method createGroupInfo.

private SignalServiceGroup createGroupInfo(SignalServiceEnvelope envelope, DataMessage content) {
    if (!content.hasGroup())
        return null;
    SignalServiceGroup.Type type;
    switch(content.getGroup().getType()) {
        case DELIVER:
            type = SignalServiceGroup.Type.DELIVER;
            break;
        case UPDATE:
            type = SignalServiceGroup.Type.UPDATE;
            break;
        case QUIT:
            type = SignalServiceGroup.Type.QUIT;
            break;
        case REQUEST_INFO:
            type = SignalServiceGroup.Type.REQUEST_INFO;
            break;
        default:
            type = SignalServiceGroup.Type.UNKNOWN;
            break;
    }
    if (content.getGroup().getType() != DELIVER) {
        String name = null;
        List<String> members = null;
        SignalServiceAttachmentPointer avatar = null;
        if (content.getGroup().hasName()) {
            name = content.getGroup().getName();
        }
        if (content.getGroup().getMembersCount() > 0) {
            members = content.getGroup().getMembersList();
        }
        if (content.getGroup().hasAvatar()) {
            AttachmentPointer pointer = content.getGroup().getAvatar();
            avatar = new SignalServiceAttachmentPointer(pointer.getId(), pointer.getContentType(), pointer.getKey().toByteArray(), envelope.getRelay(), Optional.of(pointer.getSize()), Optional.<byte[]>absent(), 0, 0, Optional.fromNullable(pointer.hasDigest() ? pointer.getDigest().toByteArray() : null), Optional.<String>absent(), false);
        }
        return new SignalServiceGroup(type, content.getGroup().getId().toByteArray(), name, members, avatar);
    }
    return new SignalServiceGroup(content.getGroup().getId().toByteArray());
}
Also used : SignalServiceAttachmentPointer(org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer) SignalServiceGroup(org.whispersystems.signalservice.api.messages.SignalServiceGroup) SignalServiceAttachmentPointer(org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer) AttachmentPointer(org.whispersystems.signalservice.internal.push.SignalServiceProtos.AttachmentPointer)

Example 4 with SignalServiceAttachmentPointer

use of org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer in project Signal-Android by signalapp.

the class AvatarDownloadJob method onRun.

@Override
public void onRun(MasterSecret masterSecret) throws IOException {
    String encodeId = GroupUtil.getEncodedId(groupId, false);
    GroupDatabase database = DatabaseFactory.getGroupDatabase(context);
    Optional<GroupRecord> record = database.getGroup(encodeId);
    File attachment = null;
    try {
        if (record.isPresent()) {
            long avatarId = record.get().getAvatarId();
            String contentType = record.get().getAvatarContentType();
            byte[] key = record.get().getAvatarKey();
            String relay = record.get().getRelay();
            Optional<byte[]> digest = Optional.fromNullable(record.get().getAvatarDigest());
            Optional<String> fileName = Optional.absent();
            if (avatarId == -1 || key == null) {
                return;
            }
            if (digest.isPresent()) {
                Log.w(TAG, "Downloading group avatar with digest: " + Hex.toString(digest.get()));
            }
            attachment = File.createTempFile("avatar", "tmp", context.getCacheDir());
            attachment.deleteOnExit();
            SignalServiceAttachmentPointer pointer = new SignalServiceAttachmentPointer(avatarId, contentType, key, relay, Optional.of(0), Optional.absent(), 0, 0, digest, fileName, false);
            InputStream inputStream = receiver.retrieveAttachment(pointer, attachment, MAX_AVATAR_SIZE);
            Bitmap avatar = BitmapUtil.createScaledBitmap(context, new AttachmentModel(attachment, key, 0, digest), 500, 500);
            database.updateAvatar(encodeId, avatar);
            inputStream.close();
        }
    } catch (BitmapDecodingException | NonSuccessfulResponseCodeException | InvalidMessageException e) {
        Log.w(TAG, e);
    } finally {
        if (attachment != null)
            attachment.delete();
    }
}
Also used : InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) InputStream(java.io.InputStream) SignalServiceAttachmentPointer(org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer) NonSuccessfulResponseCodeException(org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException) GroupRecord(org.thoughtcrime.securesms.database.GroupDatabase.GroupRecord) Bitmap(android.graphics.Bitmap) AttachmentModel(org.thoughtcrime.securesms.mms.AttachmentStreamUriLoader.AttachmentModel) GroupDatabase(org.thoughtcrime.securesms.database.GroupDatabase) File(java.io.File) BitmapDecodingException(org.thoughtcrime.securesms.util.BitmapDecodingException)

Example 5 with SignalServiceAttachmentPointer

use of org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer in project Signal-Android by WhisperSystems.

the class AttachmentDownloadJob method createAttachmentPointer.

private SignalServiceAttachmentPointer createAttachmentPointer(Attachment attachment) throws InvalidPartException {
    if (TextUtils.isEmpty(attachment.getLocation())) {
        throw new InvalidPartException("empty content id");
    }
    if (TextUtils.isEmpty(attachment.getKey())) {
        throw new InvalidPartException("empty encrypted key");
    }
    try {
        final SignalServiceAttachmentRemoteId remoteId = SignalServiceAttachmentRemoteId.from(attachment.getLocation());
        final byte[] key = Base64.decode(attachment.getKey());
        if (attachment.getDigest() != null) {
            Log.i(TAG, "Downloading attachment with digest: " + Hex.toString(attachment.getDigest()));
        } else {
            Log.i(TAG, "Downloading attachment with no digest...");
        }
        return new SignalServiceAttachmentPointer(attachment.getCdnNumber(), remoteId, null, key, Optional.of(Util.toIntExact(attachment.getSize())), Optional.absent(), 0, 0, Optional.fromNullable(attachment.getDigest()), Optional.fromNullable(attachment.getFileName()), attachment.isVoiceNote(), attachment.isBorderless(), attachment.isVideoGif(), Optional.absent(), Optional.fromNullable(attachment.getBlurHash()).transform(BlurHash::getHash), attachment.getUploadTimestamp());
    } catch (IOException | ArithmeticException e) {
        Log.w(TAG, e);
        throw new InvalidPartException(e);
    }
}
Also used : SignalServiceAttachmentRemoteId(org.whispersystems.signalservice.api.messages.SignalServiceAttachmentRemoteId) SignalServiceAttachmentPointer(org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer) IOException(java.io.IOException)

Aggregations

SignalServiceAttachmentPointer (org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer)14 File (java.io.File)7 InvalidMessageException (org.whispersystems.libsignal.InvalidMessageException)7 InputStream (java.io.InputStream)6 NonSuccessfulResponseCodeException (org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException)6 IOException (java.io.IOException)5 AttachmentDatabase (org.thoughtcrime.securesms.database.AttachmentDatabase)4 SignalServiceAttachmentRemoteId (org.whispersystems.signalservice.api.messages.SignalServiceAttachmentRemoteId)4 Bitmap (android.graphics.Bitmap)3 GroupDatabase (org.thoughtcrime.securesms.database.GroupDatabase)3 PartProgressEvent (org.thoughtcrime.securesms.events.PartProgressEvent)3 GroupRecord (org.thoughtcrime.securesms.database.GroupDatabase.GroupRecord)2 AttachmentModel (org.thoughtcrime.securesms.mms.AttachmentStreamUriLoader.AttachmentModel)2 MmsException (org.thoughtcrime.securesms.mms.MmsException)2 BitmapDecodingException (org.thoughtcrime.securesms.util.BitmapDecodingException)2 SignalServiceMessageReceiver (org.whispersystems.signalservice.api.SignalServiceMessageReceiver)2 MissingConfigurationException (org.whispersystems.signalservice.api.push.exceptions.MissingConfigurationException)2 VisibleForTesting (android.support.annotation.VisibleForTesting)1 Nullable (androidx.annotation.Nullable)1 Attachment (org.thoughtcrime.securesms.attachments.Attachment)1