Search in sources :

Example 6 with SignalServiceAttachmentRemoteId

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

the class AvatarGroupsV1DownloadJob method onRun.

@Override
public void onRun() throws IOException {
    GroupDatabase database = SignalDatabase.groups();
    Optional<GroupRecord> record = database.getGroup(groupId);
    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.i(TAG, "Downloading group avatar with digest: " + Hex.toString(digest.get()));
            }
            attachment = File.createTempFile("avatar", "tmp", context.getCacheDir());
            attachment.deleteOnExit();
            SignalServiceMessageReceiver receiver = ApplicationDependencies.getSignalServiceMessageReceiver();
            SignalServiceAttachmentPointer pointer = new SignalServiceAttachmentPointer(0, new SignalServiceAttachmentRemoteId(avatarId), contentType, key, Optional.of(0), Optional.absent(), 0, 0, digest, fileName, false, false, false, Optional.absent(), Optional.absent(), System.currentTimeMillis());
            InputStream inputStream = receiver.retrieveAttachment(pointer, attachment, AvatarHelper.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE);
            AvatarHelper.setAvatar(context, record.get().getRecipientId(), inputStream);
            SignalDatabase.groups().onAvatarUpdated(groupId, true);
            inputStream.close();
        }
    } catch (NonSuccessfulResponseCodeException | InvalidMessageException | MissingConfigurationException 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) MissingConfigurationException(org.whispersystems.signalservice.api.push.exceptions.MissingConfigurationException) SignalServiceMessageReceiver(org.whispersystems.signalservice.api.SignalServiceMessageReceiver) SignalServiceAttachmentRemoteId(org.whispersystems.signalservice.api.messages.SignalServiceAttachmentRemoteId) GroupDatabase(org.thoughtcrime.securesms.database.GroupDatabase) File(java.io.File)

Example 7 with SignalServiceAttachmentRemoteId

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

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)

Example 8 with SignalServiceAttachmentRemoteId

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

the class PushSendJob method getAttachmentPointerFor.

@Nullable
protected SignalServiceAttachment getAttachmentPointerFor(Attachment attachment) {
    if (TextUtils.isEmpty(attachment.getLocation())) {
        Log.w(TAG, "empty content id");
        return null;
    }
    if (TextUtils.isEmpty(attachment.getKey())) {
        Log.w(TAG, "empty encrypted key");
        return null;
    }
    try {
        final SignalServiceAttachmentRemoteId remoteId = SignalServiceAttachmentRemoteId.from(attachment.getLocation());
        final byte[] key = Base64.decode(attachment.getKey());
        int width = attachment.getWidth();
        int height = attachment.getHeight();
        if ((width == 0 || height == 0) && MediaUtil.hasVideoThumbnail(context, attachment.getUri())) {
            Bitmap thumbnail = MediaUtil.getVideoThumbnail(context, attachment.getUri(), 1000);
            if (thumbnail != null) {
                width = thumbnail.getWidth();
                height = thumbnail.getHeight();
            }
        }
        return new SignalServiceAttachmentPointer(attachment.getCdnNumber(), remoteId, attachment.getContentType(), key, Optional.of(Util.toIntExact(attachment.getSize())), Optional.absent(), width, height, Optional.fromNullable(attachment.getDigest()), Optional.fromNullable(attachment.getFileName()), attachment.isVoiceNote(), attachment.isBorderless(), attachment.isVideoGif(), Optional.fromNullable(attachment.getCaption()), Optional.fromNullable(attachment.getBlurHash()).transform(BlurHash::getHash), attachment.getUploadTimestamp());
    } catch (IOException | ArithmeticException e) {
        Log.w(TAG, e);
        return null;
    }
}
Also used : Bitmap(android.graphics.Bitmap) SignalServiceAttachmentRemoteId(org.whispersystems.signalservice.api.messages.SignalServiceAttachmentRemoteId) SignalServiceAttachmentPointer(org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer) IOException(java.io.IOException) NetworkConstraint(org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint) Nullable(androidx.annotation.Nullable)

Aggregations

SignalServiceAttachmentPointer (org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer)8 SignalServiceAttachmentRemoteId (org.whispersystems.signalservice.api.messages.SignalServiceAttachmentRemoteId)8 IOException (java.io.IOException)6 Bitmap (android.graphics.Bitmap)2 Nullable (androidx.annotation.Nullable)2 File (java.io.File)2 InputStream (java.io.InputStream)2 GroupDatabase (org.thoughtcrime.securesms.database.GroupDatabase)2 GroupRecord (org.thoughtcrime.securesms.database.GroupDatabase.GroupRecord)2 NetworkConstraint (org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint)2 InvalidMessageException (org.whispersystems.libsignal.InvalidMessageException)2 SignalServiceMessageReceiver (org.whispersystems.signalservice.api.SignalServiceMessageReceiver)2 MissingConfigurationException (org.whispersystems.signalservice.api.push.exceptions.MissingConfigurationException)2 NonSuccessfulResponseCodeException (org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException)2 WebSocketUnavailableException (org.whispersystems.signalservice.api.websocket.WebSocketUnavailableException)2 AttachmentV2UploadAttributes (org.whispersystems.signalservice.internal.push.AttachmentV2UploadAttributes)2