Add federation support for attachment retrieval.

This commit is contained in:
Moxie Marlinspike 2013-10-19 18:16:11 -07:00
parent 246cd10454
commit 51b9affe90
3 changed files with 21 additions and 6 deletions

View File

@ -172,9 +172,14 @@ public class PushServiceSocket {
return new Gson().fromJson(response.second, AttachmentKey.class).getId(); return new Gson().fromJson(response.second, AttachmentKey.class).getId();
} }
public File retrieveAttachment(long attachmentId) throws IOException { public File retrieveAttachment(String relay, long attachmentId) throws IOException {
Pair<String, String> response = makeRequestForResponseHeader(String.format(ATTACHMENT_PATH, String.valueOf(attachmentId)), String path = String.format(ATTACHMENT_PATH, String.valueOf(attachmentId));
"GET", null, "Content-Location");
if (relay != null) {
path = path + "?relay=" + relay;
}
Pair<String, String> response = makeRequestForResponseHeader(path, "GET", null, "Content-Location");
Log.w("PushServiceSocket", "Attachment: " + attachmentId + " is at: " + response.first); Log.w("PushServiceSocket", "Attachment: " + attachmentId + " is at: " + response.first);

View File

@ -57,6 +57,11 @@ public class IncomingMediaMessage {
media.setContentType(Util.toIsoBytes(attachment.getContentType())); media.setContentType(Util.toIsoBytes(attachment.getContentType()));
media.setContentLocation(Util.toIsoBytes(String.valueOf(attachment.getId()))); media.setContentLocation(Util.toIsoBytes(String.valueOf(attachment.getId())));
media.setContentDisposition(Util.toIsoBytes(Base64.encodeBytes(encryptedKey))); media.setContentDisposition(Util.toIsoBytes(Base64.encodeBytes(encryptedKey)));
if (message.getRelay() != null) {
media.setName(Util.toIsoBytes(message.getRelay()));
}
media.setPendingPush(true); media.setPendingPush(true);
body.addPart(media); body.addPart(media);

View File

@ -69,8 +69,13 @@ public class PushDownloader {
MasterCipher masterCipher = new MasterCipher(masterSecret); MasterCipher masterCipher = new MasterCipher(masterSecret);
long contentLocation = Long.parseLong(Util.toIsoString(part.getContentLocation())); long contentLocation = Long.parseLong(Util.toIsoString(part.getContentLocation()));
byte[] key = masterCipher.decryptBytes(Base64.decode(Util.toIsoString(part.getContentDisposition()))); byte[] key = masterCipher.decryptBytes(Base64.decode(Util.toIsoString(part.getContentDisposition())));
String relay = null;
attachmentFile = downloadAttachment(contentLocation); if (part.getName() != null) {
relay = Util.toIsoString(part.getName());
}
attachmentFile = downloadAttachment(relay, contentLocation);
InputStream attachmentInput = new AttachmentCipherInputStream(attachmentFile, key); InputStream attachmentInput = new AttachmentCipherInputStream(attachmentFile, key);
database.updateDownloadedPart(messageId, partId, part, attachmentInput); database.updateDownloadedPart(messageId, partId, part, attachmentInput);
@ -97,9 +102,9 @@ public class PushDownloader {
} }
} }
private File downloadAttachment(long contentLocation) throws IOException { private File downloadAttachment(String relay, long contentLocation) throws IOException {
PushServiceSocket socket = new PushServiceSocket(context, TextSecurePushCredentials.getInstance()); PushServiceSocket socket = new PushServiceSocket(context, TextSecurePushCredentials.getInstance());
return socket.retrieveAttachment(contentLocation); return socket.retrieveAttachment(relay, contentLocation);
} }
} }