Search in sources :

Example 1 with SignalServiceMessageReceiver

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

the class AttachmentDownloadJob method retrieveAttachment.

private void retrieveAttachment(long messageId, final AttachmentId attachmentId, final Attachment attachment) throws IOException, RetryLaterException {
    AttachmentDatabase database = SignalDatabase.attachments();
    File attachmentFile = database.getOrCreateTransferFile(attachmentId);
    try {
        SignalServiceMessageReceiver messageReceiver = ApplicationDependencies.getSignalServiceMessageReceiver();
        SignalServiceAttachmentPointer pointer = createAttachmentPointer(attachment);
        InputStream stream = messageReceiver.retrieveAttachment(pointer, attachmentFile, MAX_ATTACHMENT_SIZE, (total, progress) -> EventBus.getDefault().postSticky(new PartProgressEvent(attachment, PartProgressEvent.Type.NETWORK, total, progress)));
        database.insertAttachmentsForPlaceholder(messageId, attachmentId, stream);
    } catch (RangeException e) {
        Log.w(TAG, "Range exception, file size " + attachmentFile.length(), e);
        if (attachmentFile.delete()) {
            Log.i(TAG, "Deleted temp download file to recover");
            throw new RetryLaterException(e);
        } else {
            throw new IOException("Failed to delete temp download file following range exception");
        }
    } catch (InvalidPartException | NonSuccessfulResponseCodeException | InvalidMessageException | MmsException | MissingConfigurationException e) {
        Log.w(TAG, "Experienced exception while trying to download an attachment.", e);
        markFailed(messageId, attachmentId);
    }
}
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) IOException(java.io.IOException) AttachmentDatabase(org.thoughtcrime.securesms.database.AttachmentDatabase) MmsException(org.thoughtcrime.securesms.mms.MmsException) MissingConfigurationException(org.whispersystems.signalservice.api.push.exceptions.MissingConfigurationException) SignalServiceMessageReceiver(org.whispersystems.signalservice.api.SignalServiceMessageReceiver) RangeException(org.whispersystems.signalservice.api.push.exceptions.RangeException) RetryLaterException(org.thoughtcrime.securesms.transport.RetryLaterException) File(java.io.File)

Example 2 with SignalServiceMessageReceiver

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

the class AvatarGroupsV2DownloadJob method downloadGroupAvatarBytes.

@Nullable
public static byte[] downloadGroupAvatarBytes(@NonNull Context context, @NonNull GroupMasterKey groupMasterKey, @NonNull String cdnKey) throws IOException {
    if (cdnKey.length() == 0) {
        return null;
    }
    GroupSecretParams groupSecretParams = GroupSecretParams.deriveFromMasterKey(groupMasterKey);
    File attachment = File.createTempFile("avatar", "gv2", context.getCacheDir());
    attachment.deleteOnExit();
    SignalServiceMessageReceiver receiver = ApplicationDependencies.getSignalServiceMessageReceiver();
    byte[] encryptedData;
    try (FileInputStream inputStream = receiver.retrieveGroupsV2ProfileAvatar(cdnKey, attachment, AVATAR_DOWNLOAD_FAIL_SAFE_MAX_SIZE)) {
        encryptedData = new byte[(int) attachment.length()];
        StreamUtil.readFully(inputStream, encryptedData);
        GroupsV2Operations operations = ApplicationDependencies.getGroupsV2Operations();
        GroupsV2Operations.GroupOperations groupOperations = operations.forGroup(groupSecretParams);
        return groupOperations.decryptAvatar(encryptedData);
    } finally {
        if (attachment.exists())
            if (!attachment.delete()) {
                Log.w(TAG, "Unable to delete temp avatar file");
            }
    }
}
Also used : GroupsV2Operations(org.whispersystems.signalservice.api.groupsv2.GroupsV2Operations) GroupSecretParams(org.signal.zkgroup.groups.GroupSecretParams) SignalServiceMessageReceiver(org.whispersystems.signalservice.api.SignalServiceMessageReceiver) File(java.io.File) FileInputStream(java.io.FileInputStream) Nullable(androidx.annotation.Nullable)

Example 3 with SignalServiceMessageReceiver

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

the class StickerPackDownloadJob method onRun.

@Override
protected void onRun() throws IOException, InvalidMessageException {
    if (isReferencePack && !SignalDatabase.attachments().containsStickerPackId(packId) && !BlessedPacks.contains(packId)) {
        Log.w(TAG, "There are no attachments with the requested packId present for this reference pack. Skipping.");
        return;
    }
    if (isReferencePack && SignalDatabase.stickers().isPackAvailableAsReference(packId)) {
        Log.i(TAG, "Sticker pack already available for reference. Skipping.");
        return;
    }
    SignalServiceMessageReceiver receiver = ApplicationDependencies.getSignalServiceMessageReceiver();
    JobManager jobManager = ApplicationDependencies.getJobManager();
    StickerDatabase stickerDatabase = SignalDatabase.stickers();
    byte[] packIdBytes = Hex.fromStringCondensed(packId);
    byte[] packKeyBytes = Hex.fromStringCondensed(packKey);
    SignalServiceStickerManifest manifest = receiver.retrieveStickerManifest(packIdBytes, packKeyBytes);
    if (manifest.getStickers().isEmpty()) {
        Log.w(TAG, "No stickers in pack!");
        return;
    }
    if (!isReferencePack && stickerDatabase.isPackAvailableAsReference(packId)) {
        stickerDatabase.markPackAsInstalled(packId, notify);
    }
    StickerInfo cover = manifest.getCover().or(manifest.getStickers().get(0));
    JobManager.Chain chain = jobManager.startChain(new StickerDownloadJob(new IncomingSticker(packId, packKey, manifest.getTitle().or(""), manifest.getAuthor().or(""), cover.getId(), "", cover.getContentType(), true, !isReferencePack), notify));
    if (!isReferencePack) {
        List<Job> jobs = new ArrayList<>(manifest.getStickers().size());
        for (StickerInfo stickerInfo : manifest.getStickers()) {
            jobs.add(new StickerDownloadJob(new IncomingSticker(packId, packKey, manifest.getTitle().or(""), manifest.getAuthor().or(""), stickerInfo.getId(), stickerInfo.getEmoji(), stickerInfo.getContentType(), false, true), notify));
        }
        chain.then(jobs);
    }
    chain.enqueue();
}
Also used : SignalServiceStickerManifest(org.whispersystems.signalservice.api.messages.SignalServiceStickerManifest) SignalServiceMessageReceiver(org.whispersystems.signalservice.api.SignalServiceMessageReceiver) StickerDatabase(org.thoughtcrime.securesms.database.StickerDatabase) ArrayList(java.util.ArrayList) IncomingSticker(org.thoughtcrime.securesms.database.model.IncomingSticker) JobManager(org.thoughtcrime.securesms.jobmanager.JobManager) StickerInfo(org.whispersystems.signalservice.api.messages.SignalServiceStickerManifest.StickerInfo) Job(org.thoughtcrime.securesms.jobmanager.Job)

Example 4 with SignalServiceMessageReceiver

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

the class StickerDownloadJob method onRun.

@Override
protected void onRun() throws Exception {
    StickerDatabase db = SignalDatabase.stickers();
    StickerRecord stickerRecord = db.getSticker(sticker.getPackId(), sticker.getStickerId(), sticker.isCover());
    if (stickerRecord != null) {
        try (InputStream stream = PartAuthority.getAttachmentStream(context, stickerRecord.getUri())) {
            if (stream != null) {
                Log.w(TAG, "Sticker already downloaded.");
                return;
            }
        } catch (FileNotFoundException e) {
            Log.w(TAG, "Sticker file no longer exists, downloading again.");
        }
    }
    if (!db.isPackInstalled(sticker.getPackId()) && !sticker.isCover()) {
        Log.w(TAG, "Pack is no longer installed.");
        return;
    }
    SignalServiceMessageReceiver receiver = ApplicationDependencies.getSignalServiceMessageReceiver();
    byte[] packIdBytes = Hex.fromStringCondensed(sticker.getPackId());
    byte[] packKeyBytes = Hex.fromStringCondensed(sticker.getPackKey());
    InputStream stream = receiver.retrieveSticker(packIdBytes, packKeyBytes, sticker.getStickerId());
    db.insertSticker(sticker, stream, notify);
}
Also used : InputStream(java.io.InputStream) SignalServiceMessageReceiver(org.whispersystems.signalservice.api.SignalServiceMessageReceiver) StickerDatabase(org.thoughtcrime.securesms.database.StickerDatabase) FileNotFoundException(java.io.FileNotFoundException) StickerRecord(org.thoughtcrime.securesms.database.model.StickerRecord)

Example 5 with SignalServiceMessageReceiver

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

the class LinkPreviewRepository method fetchStickerPackLinkPreview.

private static RequestController fetchStickerPackLinkPreview(@NonNull Context context, @NonNull String packUrl, @NonNull Callback callback) {
    SignalExecutors.UNBOUNDED.execute(() -> {
        try {
            Pair<String, String> stickerParams = StickerUrl.parseShareLink(packUrl).or(new Pair<>("", ""));
            String packIdString = stickerParams.first();
            String packKeyString = stickerParams.second();
            byte[] packIdBytes = Hex.fromStringCondensed(packIdString);
            byte[] packKeyBytes = Hex.fromStringCondensed(packKeyString);
            SignalServiceMessageReceiver receiver = ApplicationDependencies.getSignalServiceMessageReceiver();
            SignalServiceStickerManifest manifest = receiver.retrieveStickerManifest(packIdBytes, packKeyBytes);
            String title = manifest.getTitle().or(manifest.getAuthor()).or("");
            Optional<StickerInfo> firstSticker = Optional.fromNullable(manifest.getStickers().size() > 0 ? manifest.getStickers().get(0) : null);
            Optional<StickerInfo> cover = manifest.getCover().or(firstSticker);
            if (cover.isPresent()) {
                Bitmap bitmap = GlideApp.with(context).asBitmap().load(new StickerRemoteUri(packIdString, packKeyString, cover.get().getId())).skipMemoryCache(true).diskCacheStrategy(DiskCacheStrategy.NONE).centerInside().submit(512, 512).get();
                Optional<Attachment> thumbnail = bitmapToAttachment(bitmap, Bitmap.CompressFormat.WEBP, MediaUtil.IMAGE_WEBP);
                callback.onSuccess(new LinkPreview(packUrl, title, "", 0, thumbnail));
            } else {
                callback.onError(Error.PREVIEW_NOT_AVAILABLE);
            }
        } catch (IOException | InvalidMessageException | ExecutionException | InterruptedException e) {
            Log.w(TAG, "Failed to fetch sticker pack link preview.");
            callback.onError(Error.PREVIEW_NOT_AVAILABLE);
        }
    });
    return () -> Log.i(TAG, "Cancelled sticker pack link preview fetch -- no effect.");
}
Also used : SignalServiceStickerManifest(org.whispersystems.signalservice.api.messages.SignalServiceStickerManifest) InvalidMessageException(org.whispersystems.libsignal.InvalidMessageException) StickerRemoteUri(org.thoughtcrime.securesms.stickers.StickerRemoteUri) Attachment(org.thoughtcrime.securesms.attachments.Attachment) UriAttachment(org.thoughtcrime.securesms.attachments.UriAttachment) IOException(java.io.IOException) Bitmap(android.graphics.Bitmap) SignalServiceMessageReceiver(org.whispersystems.signalservice.api.SignalServiceMessageReceiver) StickerInfo(org.whispersystems.signalservice.api.messages.SignalServiceStickerManifest.StickerInfo) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

SignalServiceMessageReceiver (org.whispersystems.signalservice.api.SignalServiceMessageReceiver)8 File (java.io.File)4 InputStream (java.io.InputStream)4 IOException (java.io.IOException)3 InvalidMessageException (org.whispersystems.libsignal.InvalidMessageException)3 NonSuccessfulResponseCodeException (org.whispersystems.signalservice.api.push.exceptions.NonSuccessfulResponseCodeException)3 StickerDatabase (org.thoughtcrime.securesms.database.StickerDatabase)2 SignalServiceAttachmentPointer (org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer)2 SignalServiceStickerManifest (org.whispersystems.signalservice.api.messages.SignalServiceStickerManifest)2 StickerInfo (org.whispersystems.signalservice.api.messages.SignalServiceStickerManifest.StickerInfo)2 MissingConfigurationException (org.whispersystems.signalservice.api.push.exceptions.MissingConfigurationException)2 Bitmap (android.graphics.Bitmap)1 Nullable (androidx.annotation.Nullable)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 ArrayList (java.util.ArrayList)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 GroupSecretParams (org.signal.zkgroup.groups.GroupSecretParams)1 ProfileKey (org.signal.zkgroup.profiles.ProfileKey)1