Change attachment retrieval interface

pull/1/head
Moxie Marlinspike 12 years ago
parent 9287d413ac
commit fb378a6e00

@ -134,10 +134,10 @@ public class PushServiceSocket {
return new Gson().fromJson(response.second, AttachmentKey.class).getId(); return new Gson().fromJson(response.second, AttachmentKey.class).getId();
} }
public List<File> retrieveAttachments(List<PushAttachmentPointer> attachmentIds) public List<Pair<File,String>> retrieveAttachments(List<PushAttachmentPointer> attachmentIds)
throws IOException throws IOException
{ {
List<File> attachments = new LinkedList<File>(); List<Pair<File,String>> attachments = new LinkedList<Pair<File,String>>();
for (PushAttachmentPointer attachmentId : attachmentIds) { for (PushAttachmentPointer attachmentId : attachmentIds) {
Pair<String, String> response = makeRequestForResponseHeader(String.format(ATTACHMENT_PATH, attachmentId.getKey()), Pair<String, String> response = makeRequestForResponseHeader(String.format(ATTACHMENT_PATH, attachmentId.getKey()),
@ -146,9 +146,10 @@ public class PushServiceSocket {
Log.w("PushServiceSocket", "Attachment: " + attachmentId.getKey() + " is at: " + response.first); Log.w("PushServiceSocket", "Attachment: " + attachmentId.getKey() + " is at: " + response.first);
File attachment = File.createTempFile("attachment", ".tmp", context.getFilesDir()); File attachment = File.createTempFile("attachment", ".tmp", context.getFilesDir());
downloadExternalFile(response.first, attachment); attachment.deleteOnExit();
attachments.add(attachment); downloadExternalFile(response.first, attachment);
attachments.add(new Pair<File, String>(attachment, attachmentId.getContentType()));
} }
return attachments; return attachments;

@ -5,8 +5,11 @@ import android.content.Context;
import android.widget.EditText; import android.widget.EditText;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom; import java.security.SecureRandom;
import java.util.Collection; import java.util.Collection;
@ -44,6 +47,10 @@ public class Util {
} }
} }
public static String readFully(File file) throws IOException {
return readFully(new FileInputStream(file));
}
public static String readFully(InputStream in) throws IOException { public static String readFully(InputStream in) throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream(); ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte[] buffer = new byte[4096]; byte[] buffer = new byte[4096];
@ -58,6 +65,18 @@ public class Util {
return new String(bout.toByteArray()); return new String(bout.toByteArray());
} }
public static void copy(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[4096];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
out.close();
}
public static String join(Collection<String> list, String delimiter) { public static String join(Collection<String> list, String delimiter) {
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
int i=0; int i=0;

@ -1,15 +1,14 @@
package org.thoughtcrime.securesms.mms; package org.thoughtcrime.securesms.mms;
import android.util.Log; import android.util.Log;
import android.util.Pair;
import org.thoughtcrime.securesms.util.Util; import org.thoughtcrime.securesms.util.Util;
import org.whispersystems.textsecure.push.IncomingPushMessage; import org.whispersystems.textsecure.push.IncomingPushMessage;
import org.whispersystems.textsecure.push.PushAttachmentPointer;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.Iterator;
import java.util.List; import java.util.List;
import ws.com.google.android.mms.pdu.CharacterSets; import ws.com.google.android.mms.pdu.CharacterSets;
@ -29,7 +28,8 @@ public class IncomingMediaMessage {
this.body = retreived.getBody(); this.body = retreived.getBody();
} }
public IncomingMediaMessage(String localNumber, IncomingPushMessage message, List<File> attachments) public IncomingMediaMessage(String localNumber, IncomingPushMessage message,
List<Pair<File, String>> attachments)
throws IOException throws IOException
{ {
this.headers = new PduHeaders(); this.headers = new PduHeaders();
@ -53,20 +53,18 @@ public class IncomingMediaMessage {
body.addPart(text); body.addPart(text);
} }
Iterator<PushAttachmentPointer> descriptors = message.getAttachments().iterator();
if (attachments != null) { if (attachments != null) {
for (File attachment : attachments) { for (Pair<File, String> attachment : attachments) {
PduPart media = new PduPart(); PduPart media = new PduPart();
FileInputStream fin = new FileInputStream(attachment); FileInputStream fin = new FileInputStream(attachment.first);
byte[] data = Util.readFully(fin); byte[] data = Util.readFully(fin);
PushAttachmentPointer descriptor = descriptors.next();
Log.w("IncomingMediaMessage", "Adding part: " + descriptor.getContentType() + " with length: " + data.length); Log.w("IncomingMediaMessage", "Adding part: " + attachment.second + " with length: " + data.length);
media.setContentType(descriptor.getContentType().getBytes(CharacterSets.MIMENAME_ISO_8859_1)); media.setContentType(attachment.second.getBytes(CharacterSets.MIMENAME_ISO_8859_1));
media.setData(data); media.setData(data);
body.addPart(media); body.addPart(media);
attachment.first.delete();
} }
} }
} }

@ -81,8 +81,8 @@ public class MmsReceiver {
PushServiceSocket socket = new PushServiceSocket(context, localNumber, password); PushServiceSocket socket = new PushServiceSocket(context, localNumber, password);
try { try {
List<File> attachments = socket.retrieveAttachments(pushMessage.getAttachments()); List<Pair<File, String>> attachments = socket.retrieveAttachments(pushMessage.getAttachments());
IncomingMediaMessage message = new IncomingMediaMessage(localNumber, pushMessage, attachments); IncomingMediaMessage message = new IncomingMediaMessage(localNumber, pushMessage, attachments);
DatabaseFactory.getMmsDatabase(context).insertMessageInbox(masterSecret, message, "", -1); DatabaseFactory.getMmsDatabase(context).insertMessageInbox(masterSecret, message, "", -1);
} catch (IOException e) { } catch (IOException e) {

Loading…
Cancel
Save