Use attachment stream builders.

// FREEBIE
This commit is contained in:
Moxie Marlinspike 2015-07-01 15:39:18 -07:00
parent c2e5f4e80a
commit 59772504e3
3 changed files with 38 additions and 17 deletions

View file

@ -20,6 +20,7 @@ import org.whispersystems.jobqueue.requirements.NetworkRequirement;
import org.whispersystems.libaxolotl.util.guava.Optional;
import org.whispersystems.textsecure.api.TextSecureMessageSender;
import org.whispersystems.textsecure.api.crypto.UntrustedIdentityException;
import org.whispersystems.textsecure.api.messages.TextSecureAttachment;
import org.whispersystems.textsecure.api.messages.TextSecureAttachmentStream;
import org.whispersystems.textsecure.api.messages.multidevice.DeviceContact;
import org.whispersystems.textsecure.api.messages.multidevice.DeviceContactsOutputStream;
@ -101,10 +102,11 @@ public class MultiDeviceContactUpdateJob extends MasterSecretJob implements Inje
throws IOException, UntrustedIdentityException, NetworkException
{
FileInputStream contactsFileStream = new FileInputStream(contactsFile);
TextSecureAttachmentStream attachmentStream = new TextSecureAttachmentStream(contactsFileStream,
"application/octet-stream",
contactsFile.length(),
null);
TextSecureAttachmentStream attachmentStream = TextSecureAttachment.newStreamBuilder()
.withStream(contactsFileStream)
.withContentType("application/octet-stream")
.withLength(contactsFile.length())
.build();
try {
messageSender.sendMessage(TextSecureSyncMessage.forContacts(attachmentStream));
@ -118,7 +120,12 @@ public class MultiDeviceContactUpdateJob extends MasterSecretJob implements Inje
try {
Uri displayPhotoUri = Uri.withAppendedPath(uri, ContactsContract.Contacts.Photo.DISPLAY_PHOTO);
AssetFileDescriptor fd = context.getContentResolver().openAssetFileDescriptor(displayPhotoUri, "r");
return Optional.of(new TextSecureAttachmentStream(fd.createInputStream(), "image/*", fd.getLength(), null));
return Optional.of(TextSecureAttachment.newStreamBuilder()
.withStream(fd.createInputStream())
.withContentType("image/*")
.withLength(fd.getLength())
.build());
} catch (IOException e) {
Log.w(TAG, e);
}
@ -141,7 +148,11 @@ public class MultiDeviceContactUpdateJob extends MasterSecretJob implements Inje
byte[] data = cursor.getBlob(0);
if (data != null) {
return Optional.of(new TextSecureAttachmentStream(new ByteArrayInputStream(data), "image/*", data.length, null));
return Optional.of(TextSecureAttachment.newStreamBuilder()
.withStream(new ByteArrayInputStream(data))
.withContentType("image/*")
.withLength(data.length)
.build());
}
}

View file

@ -14,6 +14,7 @@ import org.whispersystems.jobqueue.requirements.NetworkRequirement;
import org.whispersystems.libaxolotl.util.guava.Optional;
import org.whispersystems.textsecure.api.TextSecureMessageSender;
import org.whispersystems.textsecure.api.crypto.UntrustedIdentityException;
import org.whispersystems.textsecure.api.messages.TextSecureAttachment;
import org.whispersystems.textsecure.api.messages.TextSecureAttachmentStream;
import org.whispersystems.textsecure.api.messages.multidevice.DeviceGroup;
import org.whispersystems.textsecure.api.messages.multidevice.DeviceGroupsOutputStream;
@ -93,10 +94,11 @@ public class MultiDeviceGroupUpdateJob extends MasterSecretJob implements Inject
throws IOException, UntrustedIdentityException
{
FileInputStream contactsFileStream = new FileInputStream(contactsFile);
TextSecureAttachmentStream attachmentStream = new TextSecureAttachmentStream(contactsFileStream,
"application/octet-stream",
contactsFile.length(),
null);
TextSecureAttachmentStream attachmentStream = TextSecureAttachment.newStreamBuilder()
.withStream(contactsFileStream)
.withContentType("application/octet-stream")
.withLength(contactsFile.length())
.build();
messageSender.sendMessage(TextSecureSyncMessage.forGroups(attachmentStream));
}
@ -105,8 +107,11 @@ public class MultiDeviceGroupUpdateJob extends MasterSecretJob implements Inject
private Optional<TextSecureAttachmentStream> getAvatar(@Nullable byte[] avatar) {
if (avatar == null) return Optional.absent();
return Optional.of(new TextSecureAttachmentStream(new ByteArrayInputStream(avatar),
"image/*", avatar.length, null));
return Optional.of(TextSecureAttachment.newStreamBuilder()
.withStream(new ByteArrayInputStream(avatar))
.withContentType("image/*")
.withLength(avatar.length)
.build());
}
private File createTempFile(String prefix) throws IOException {

View file

@ -67,11 +67,16 @@ public abstract class PushSendJob extends SendJob {
{
try {
InputStream is = PartAuthority.getPartStream(context, masterSecret, part.getDataUri());
attachments.add(new TextSecureAttachmentStream(is, contentType, part.getDataSize(), new ProgressListener() {
@Override public void onAttachmentProgress(long total, long progress) {
EventBus.getDefault().postSticky(new PartProgressEvent(part.getPartId(), total, progress));
}
}));
attachments.add(TextSecureAttachment.newStreamBuilder()
.withStream(is)
.withContentType(contentType)
.withLength(part.getDataSize())
.withListener(new ProgressListener() {
@Override public void onAttachmentProgress(long total, long progress) {
EventBus.getDefault().postSticky(new PartProgressEvent(part.getPartId(), total, progress));
}
})
.build());
} catch (IOException ioe) {
Log.w(TAG, "Couldn't open attachment", ioe);
}