Search in sources :

Example 11 with SignalServiceAttachmentPointer

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

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 12 with SignalServiceAttachmentPointer

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

the class SignalServiceMessageSender method uploadAttachmentV2.

private SignalServiceAttachmentPointer uploadAttachmentV2(SignalServiceAttachmentStream attachment, byte[] attachmentKey, PushAttachmentData attachmentData) throws NonSuccessfulResponseCodeException, PushNetworkException, MalformedResponseException {
    AttachmentV2UploadAttributes v2UploadAttributes = null;
    Log.d(TAG, "Using pipe to retrieve attachment upload attributes...");
    try {
        v2UploadAttributes = new AttachmentService.AttachmentAttributesResponseProcessor<>(attachmentService.getAttachmentV2UploadAttributes().blockingGet()).getResultOrThrow();
    } catch (WebSocketUnavailableException e) {
        Log.w(TAG, "[uploadAttachmentV2] Pipe unavailable, falling back... (" + e.getClass().getSimpleName() + ": " + e.getMessage() + ")");
    } catch (IOException e) {
        Log.w(TAG, "Failed to retrieve attachment upload attributes using pipe. Falling back...");
    }
    if (v2UploadAttributes == null) {
        Log.d(TAG, "Not using pipe to retrieve attachment upload attributes...");
        v2UploadAttributes = socket.getAttachmentV2UploadAttributes();
    }
    Pair<Long, byte[]> attachmentIdAndDigest = socket.uploadAttachment(attachmentData, v2UploadAttributes);
    return new SignalServiceAttachmentPointer(0, new SignalServiceAttachmentRemoteId(attachmentIdAndDigest.first()), attachment.getContentType(), attachmentKey, Optional.of(Util.toIntExact(attachment.getLength())), attachment.getPreview(), attachment.getWidth(), attachment.getHeight(), Optional.of(attachmentIdAndDigest.second()), attachment.getFileName(), attachment.getVoiceNote(), attachment.isBorderless(), attachment.isGif(), attachment.getCaption(), attachment.getBlurHash(), attachment.getUploadTimestamp());
}
Also used : AttachmentV2UploadAttributes(org.whispersystems.signalservice.internal.push.AttachmentV2UploadAttributes) SignalServiceAttachmentRemoteId(org.whispersystems.signalservice.api.messages.SignalServiceAttachmentRemoteId) SignalServiceAttachmentPointer(org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer) WebSocketUnavailableException(org.whispersystems.signalservice.api.websocket.WebSocketUnavailableException) IOException(java.io.IOException)

Example 13 with SignalServiceAttachmentPointer

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

the class AvatarDownloadJob method onRun.

@Override
public void onRun(MasterSecret masterSecret) throws IOException {
    GroupDatabase database = DatabaseFactory.getGroupDatabase(context);
    GroupDatabase.GroupRecord record = database.getGroup(groupId);
    File attachment = null;
    try {
        if (record != null) {
            long avatarId = record.getAvatarId();
            String contentType = record.getAvatarContentType();
            byte[] key = record.getAvatarKey();
            String relay = record.getRelay();
            Optional<byte[]> digest = Optional.fromNullable(record.getAvatarDigest());
            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, digest);
            InputStream inputStream = receiver.retrieveAttachment(pointer, attachment, MAX_AVATAR_SIZE);
            Bitmap avatar = BitmapUtil.createScaledBitmap(context, new AttachmentModel(attachment, key), 500, 500);
            database.updateAvatar(groupId, 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) 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 14 with SignalServiceAttachmentPointer

use of org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer in project toshi-android-client by toshiapp.

the class Avatar method processFromSignalGroup.

public static Single<Avatar> processFromSignalGroup(final SignalServiceGroup group, final SignalServiceMessageReceiver messageReceiver) {
    if (group.getAvatar().isPresent()) {
        return Single.fromCallable(() -> {
            final SignalServiceAttachmentPointer attachment = group.getAvatar().get().asPointer();
            final String groupId = Hex.toHexString(group.getGroupId());
            return FileUtil.writeAvatarToFileFromMessageReceiver(attachment, messageReceiver, groupId);
        }).flatMap(Avatar::compressImage).map(File::getAbsolutePath).map(BitmapFactory::decodeFile).map(Avatar::new);
    }
    return Single.just(new Avatar());
}
Also used : SignalServiceAttachmentPointer(org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer) File(java.io.File)

Example 15 with SignalServiceAttachmentPointer

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

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)22 IOException (java.io.IOException)10 InvalidMessageException (org.whispersystems.libsignal.InvalidMessageException)9 File (java.io.File)8 InputStream (java.io.InputStream)8 SignalServiceAttachmentRemoteId (org.whispersystems.signalservice.api.messages.SignalServiceAttachmentRemoteId)8 NonSuccessfulResponseCodeException (org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException)7 AttachmentDatabase (org.thoughtcrime.securesms.database.AttachmentDatabase)5 Bitmap (android.graphics.Bitmap)4 GroupDatabase (org.thoughtcrime.securesms.database.GroupDatabase)4 SignalServiceMessageReceiver (org.whispersystems.signalservice.api.SignalServiceMessageReceiver)4 MissingConfigurationException (org.whispersystems.signalservice.api.push.exceptions.MissingConfigurationException)4 GroupRecord (org.thoughtcrime.securesms.database.GroupDatabase.GroupRecord)3 PartProgressEvent (org.thoughtcrime.securesms.events.PartProgressEvent)3 Nullable (androidx.annotation.Nullable)2 Attachment (org.thoughtcrime.securesms.attachments.Attachment)2 DatabaseAttachment (org.thoughtcrime.securesms.attachments.DatabaseAttachment)2 PointerAttachment (org.thoughtcrime.securesms.attachments.PointerAttachment)2 Data (org.thoughtcrime.securesms.jobmanager.Data)2 NetworkConstraint (org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint)2