Java Code Examples for org.whispersystems.signalservice.api.SignalServiceMessageReceiver#retrieveAttachment()

The following examples show how to use org.whispersystems.signalservice.api.SignalServiceMessageReceiver#retrieveAttachment() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
private void retrieveAttachment(long messageId,
                                final AttachmentId attachmentId,
                                final Attachment attachment)
    throws IOException
{

  AttachmentDatabase database       = DatabaseFactory.getAttachmentDatabase(context);
  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 (InvalidPartException | NonSuccessfulResponseCodeException | InvalidMessageException | MmsException | MissingConfigurationException e) {
    Log.w(TAG, "Experienced exception while trying to download an attachment.", e);
    markFailed(messageId, attachmentId);
  }
}
 
Example 2
@Override
public void onRun() throws IOException {
  GroupDatabase         database   = DatabaseFactory.getGroupDatabase(context);
  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, 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);
      DatabaseFactory.getGroupDatabase(context).onAvatarUpdated(groupId, true);

      inputStream.close();
    }
  } catch (NonSuccessfulResponseCodeException | InvalidMessageException | MissingConfigurationException e) {
    Log.w(TAG, e);
  } finally {
    if (attachment != null)
      attachment.delete();
  }
}
 
Example 3
Source Project: signald   File: Manager.java    License: GNU General Public License v3.0 4 votes vote down vote up
private InputStream retrieveAttachmentAsStream(SignalServiceAttachmentPointer pointer, File tmpFile) throws IOException, InvalidMessageException {
    final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(serviceConfiguration, accountData.username, accountData.password, accountData.deviceId, accountData.signalingKey, USER_AGENT, null, sleepTimer);
    return messageReceiver.retrieveAttachment(pointer, tmpFile, MAX_ATTACHMENT_SIZE);
}
 
Example 4
Source Project: signal-cli   File: Manager.java    License: GNU General Public License v3.0 4 votes vote down vote up
private InputStream retrieveAttachmentAsStream(SignalServiceAttachmentPointer pointer, File tmpFile) throws IOException, InvalidMessageException, MissingConfigurationException {
    final SignalServiceMessageReceiver messageReceiver = getMessageReceiver();
    return messageReceiver.retrieveAttachment(pointer, tmpFile, ServiceConfig.MAX_ATTACHMENT_SIZE);
}