New attachment API

This commit is contained in:
Moxie Marlinspike 2013-12-01 17:21:41 -08:00
parent 72be77b47e
commit f002072f38
2 changed files with 19 additions and 31 deletions

View File

@ -2,7 +2,6 @@ package org.whispersystems.textsecure.push;
import android.content.Context;
import android.util.Log;
import android.util.Pair;
import com.google.thoughtcrimegson.Gson;
import com.google.thoughtcrimegson.JsonParseException;
@ -167,19 +166,18 @@ public class PushServiceSocket {
}
public long sendAttachment(PushAttachmentData attachment) throws IOException {
Pair<String, String> response = makeRequestForResponseHeader(String.format(ATTACHMENT_PATH, ""),
"GET", null, "Content-Location");
String response = makeRequest(String.format(ATTACHMENT_PATH, ""), "GET", null);
AttachmentDescriptor attachmentKey = new Gson().fromJson(response, AttachmentDescriptor.class);
String contentLocation = response.first;
Log.w("PushServiceSocket", "Got attachment content location: " + contentLocation);
if (contentLocation == null) {
if (attachmentKey == null || attachmentKey.getLocation() == null) {
throw new IOException("Server failed to allocate an attachment key!");
}
uploadExternalFile("PUT", contentLocation, attachment.getData());
Log.w("PushServiceSocket", "Got attachment content location: " + attachmentKey.getLocation());
return new Gson().fromJson(response.second, AttachmentKey.class).getId();
uploadExternalFile("PUT", attachmentKey.getLocation(), attachment.getData());
return attachmentKey.getId();
}
public File retrieveAttachment(String relay, long attachmentId) throws IOException {
@ -189,14 +187,15 @@ public class PushServiceSocket {
path = path + "?relay=" + relay;
}
Pair<String, String> response = makeRequestForResponseHeader(path, "GET", null, "Content-Location");
String response = makeRequest(path, "GET", null);
AttachmentDescriptor descriptor = new Gson().fromJson(response, AttachmentDescriptor.class);
Log.w("PushServiceSocket", "Attachment: " + attachmentId + " is at: " + response.first);
Log.w("PushServiceSocket", "Attachment: " + attachmentId + " is at: " + descriptor.getLocation());
File attachment = File.createTempFile("attachment", ".tmp", context.getFilesDir());
attachment.deleteOnExit();
downloadExternalFile(response.first, attachment);
downloadExternalFile(descriptor.getLocation(), attachment);
return attachment;
}
@ -276,18 +275,6 @@ public class PushServiceSocket {
}
}
private Pair<String, String> makeRequestForResponseHeader(String urlFragment, String method,
String body, String responseHeader)
throws IOException
{
HttpURLConnection connection = makeBaseRequest(urlFragment, method, body);
String response = Util.readFully(connection.getInputStream());
String headerValue = connection.getHeaderField(responseHeader);
connection.disconnect();
return new Pair<String, String>(headerValue, response);
}
private String makeRequest(String urlFragment, String method, String body)
throws IOException
{
@ -321,7 +308,7 @@ public class PushServiceSocket {
throw new RateLimitException("Rate limit exceeded: " + connection.getResponseCode());
}
if (connection.getResponseCode() == 403) {
if (connection.getResponseCode() == 401 || connection.getResponseCode() == 403) {
throw new AuthorizationFailedException("Authorization failed!");
}
@ -409,16 +396,17 @@ public class PushServiceSocket {
}
}
private static class AttachmentKey {
private static class AttachmentDescriptor {
private long id;
public AttachmentKey(long id) {
this.id = id;
}
private String location;
public long getId() {
return id;
}
public String getLocation() {
return location;
}
}
public interface TrustStore {

View File

@ -3,6 +3,6 @@ package org.thoughtcrime.securesms;
public class Release {
public static final String PUSH_URL = "https://textsecure-service.whispersystems.org";
// public static final String PUSH_SERVICE_URL = "http://192.168.1.135:8080";
// public static final String PUSH_URL = "http://192.168.1.135:8080";
}