diff --git a/briar-android/src/androidTestOfficial/java/org/briarproject/briar/android/attachment/AttachmentRetrieverIntegrationTest.java b/briar-android/src/androidTestOfficial/java/org/briarproject/briar/android/attachment/AttachmentRetrieverIntegrationTest.java index eea2427f3..c3661fefb 100644 --- a/briar-android/src/androidTestOfficial/java/org/briarproject/briar/android/attachment/AttachmentRetrieverIntegrationTest.java +++ b/briar-android/src/androidTestOfficial/java/org/briarproject/briar/android/attachment/AttachmentRetrieverIntegrationTest.java @@ -54,7 +54,7 @@ public class AttachmentRetrieverIntegrationTest { public void testSmallJpegImage() throws Exception { AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg"); InputStream is = getUrlInputStream(smallKitten); - Attachment a = new Attachment(is); + Attachment a = new Attachment(h, is); AttachmentItem item = retriever.getAttachmentItem(h, a, true); assertEquals(msgId, item.getMessageId()); assertEquals(160, item.getWidth()); @@ -70,7 +70,7 @@ public class AttachmentRetrieverIntegrationTest { public void testBigJpegImage() throws Exception { AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg"); InputStream is = getUrlInputStream(originalKitten); - Attachment a = new Attachment(is); + Attachment a = new Attachment(h, is); AttachmentItem item = retriever.getAttachmentItem(h, a, true); assertEquals(msgId, item.getMessageId()); assertEquals(1728, item.getWidth()); @@ -86,7 +86,7 @@ public class AttachmentRetrieverIntegrationTest { public void testSmallPngImage() throws Exception { AttachmentHeader h = new AttachmentHeader(msgId, "image/png"); InputStream is = getUrlInputStream(pngKitten); - Attachment a = new Attachment(is); + Attachment a = new Attachment(h, is); AttachmentItem item = retriever.getAttachmentItem(h, a, true); assertEquals(msgId, item.getMessageId()); assertEquals(737, item.getWidth()); @@ -102,7 +102,7 @@ public class AttachmentRetrieverIntegrationTest { public void testUberGif() throws Exception { AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg"); InputStream is = getUrlInputStream(uberGif); - Attachment a = new Attachment(is); + Attachment a = new Attachment(h, is); AttachmentItem item = retriever.getAttachmentItem(h, a, true); assertEquals(1, item.getWidth()); assertEquals(1, item.getHeight()); @@ -117,7 +117,7 @@ public class AttachmentRetrieverIntegrationTest { public void testLottaPixels() throws Exception { AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg"); InputStream is = getUrlInputStream(lottaPixel); - Attachment a = new Attachment(is); + Attachment a = new Attachment(h, is); AttachmentItem item = retriever.getAttachmentItem(h, a, true); assertEquals(64250, item.getWidth()); assertEquals(64250, item.getHeight()); @@ -132,7 +132,7 @@ public class AttachmentRetrieverIntegrationTest { public void testImageIoCrash() throws Exception { AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg"); InputStream is = getUrlInputStream(imageIoCrash); - Attachment a = new Attachment(is); + Attachment a = new Attachment(h, is); AttachmentItem item = retriever.getAttachmentItem(h, a, true); assertEquals(1184, item.getWidth()); assertEquals(448, item.getHeight()); @@ -147,7 +147,7 @@ public class AttachmentRetrieverIntegrationTest { public void testGimpCrash() throws Exception { AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg"); InputStream is = getUrlInputStream(gimpCrash); - Attachment a = new Attachment(is); + Attachment a = new Attachment(h, is); AttachmentItem item = retriever.getAttachmentItem(h, a, true); assertEquals(1, item.getWidth()); assertEquals(1, item.getHeight()); @@ -162,7 +162,7 @@ public class AttachmentRetrieverIntegrationTest { public void testOptiPngAfl() throws Exception { AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg"); InputStream is = getUrlInputStream(optiPngAfl); - Attachment a = new Attachment(is); + Attachment a = new Attachment(h, is); AttachmentItem item = retriever.getAttachmentItem(h, a, true); assertEquals(32, item.getWidth()); assertEquals(32, item.getHeight()); @@ -177,7 +177,7 @@ public class AttachmentRetrieverIntegrationTest { public void testLibrawError() throws Exception { AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg"); InputStream is = getUrlInputStream(librawError); - Attachment a = new Attachment(is); + Attachment a = new Attachment(h, is); AttachmentItem item = retriever.getAttachmentItem(h, a, true); assertTrue(item.hasError()); } @@ -186,7 +186,7 @@ public class AttachmentRetrieverIntegrationTest { public void testSmallAnimatedGifMaxDimensions() throws Exception { AttachmentHeader h = new AttachmentHeader(msgId, "image/gif"); InputStream is = getAssetInputStream("animated.gif"); - Attachment a = new Attachment(is); + Attachment a = new Attachment(h, is); AttachmentItem item = retriever.getAttachmentItem(h, a, true); assertEquals(65535, item.getWidth()); assertEquals(65535, item.getHeight()); @@ -201,7 +201,7 @@ public class AttachmentRetrieverIntegrationTest { public void testSmallAnimatedGifHugeDimensions() throws Exception { AttachmentHeader h = new AttachmentHeader(msgId, "image/gif"); InputStream is = getAssetInputStream("animated2.gif"); - Attachment a = new Attachment(is); + Attachment a = new Attachment(h, is); AttachmentItem item = retriever.getAttachmentItem(h, a, true); assertEquals(10000, item.getWidth()); assertEquals(10000, item.getHeight()); @@ -216,7 +216,7 @@ public class AttachmentRetrieverIntegrationTest { public void testSmallGifLargeDimensions() throws Exception { AttachmentHeader h = new AttachmentHeader(msgId, "image/gif"); InputStream is = getAssetInputStream("error_large.gif"); - Attachment a = new Attachment(is); + Attachment a = new Attachment(h, is); AttachmentItem item = retriever.getAttachmentItem(h, a, true); assertEquals(16384, item.getWidth()); assertEquals(16384, item.getHeight()); @@ -231,7 +231,7 @@ public class AttachmentRetrieverIntegrationTest { public void testHighError() throws Exception { AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg"); InputStream is = getAssetInputStream("error_high.jpg"); - Attachment a = new Attachment(is); + Attachment a = new Attachment(h, is); AttachmentItem item = retriever.getAttachmentItem(h, a, true); assertEquals(1, item.getWidth()); assertEquals(10000, item.getHeight()); @@ -246,7 +246,7 @@ public class AttachmentRetrieverIntegrationTest { public void testWideError() throws Exception { AttachmentHeader h = new AttachmentHeader(msgId, "image/jpeg"); InputStream is = getAssetInputStream("error_wide.jpg"); - Attachment a = new Attachment(is); + Attachment a = new Attachment(h, is); AttachmentItem item = retriever.getAttachmentItem(h, a, true); assertEquals(1920, item.getWidth()); assertEquals(1, item.getHeight()); diff --git a/briar-android/src/main/java/org/briarproject/briar/android/attachment/AttachmentItem.java b/briar-android/src/main/java/org/briarproject/briar/android/attachment/AttachmentItem.java index 8caafac7c..43410db0f 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/attachment/AttachmentItem.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/attachment/AttachmentItem.java @@ -68,7 +68,7 @@ public class AttachmentItem implements Parcelable { header = new AttachmentHeader(messageId, mimeType); } - AttachmentHeader getHeader() { + public AttachmentHeader getHeader() { return header; } diff --git a/briar-android/src/main/java/org/briarproject/briar/android/attachment/AttachmentRetriever.java b/briar-android/src/main/java/org/briarproject/briar/android/attachment/AttachmentRetriever.java index d3fb1188b..fee64d297 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/attachment/AttachmentRetriever.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/attachment/AttachmentRetriever.java @@ -109,16 +109,15 @@ public class AttachmentRetriever { List> attachments = new ArrayList<>(headers.size()); for (AttachmentHeader h : headers) { - Attachment a = messagingManager.getAttachment(h.getMessageId()); + Attachment a = messagingManager.getAttachment(h); attachments.add(new Pair<>(h, a)); } logDuration(LOG, "Loading attachments", start); return attachments; } - @DatabaseExecutor Attachment getMessageAttachment(AttachmentHeader h) throws DbException { - return messagingManager.getAttachment(h.getMessageId()); + return messagingManager.getAttachment(h); } /** diff --git a/briar-android/src/main/java/org/briarproject/briar/android/conversation/ImageViewModel.java b/briar-android/src/main/java/org/briarproject/briar/android/conversation/ImageViewModel.java index 33f29d3e3..7218efa5f 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/conversation/ImageViewModel.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/conversation/ImageViewModel.java @@ -12,7 +12,6 @@ import org.briarproject.bramble.api.db.DatabaseExecutor; import org.briarproject.bramble.api.db.DbException; import org.briarproject.bramble.api.lifecycle.IoExecutor; import org.briarproject.bramble.api.nullsafety.NotNullByDefault; -import org.briarproject.bramble.api.sync.MessageId; import org.briarproject.briar.android.attachment.AttachmentItem; import org.briarproject.briar.android.viewmodel.LiveEvent; import org.briarproject.briar.android.viewmodel.MutableLiveEvent; @@ -135,10 +134,10 @@ public class ImageViewModel extends AndroidViewModel { private void saveImage(AttachmentItem attachment, OutputStreamProvider osp, @Nullable Runnable afterCopy) { - MessageId messageId = attachment.getMessageId(); dbExecutor.execute(() -> { try { - Attachment a = messagingManager.getAttachment(messageId); + Attachment a = + messagingManager.getAttachment(attachment.getHeader()); copyImageFromDb(a, osp, afterCopy); } catch (DbException e) { logException(LOG, WARNING, e); diff --git a/briar-android/src/main/java/org/briarproject/briar/android/conversation/glide/BriarDataFetcher.java b/briar-android/src/main/java/org/briarproject/briar/android/conversation/glide/BriarDataFetcher.java index 1394f8035..852b8c536 100644 --- a/briar-android/src/main/java/org/briarproject/briar/android/conversation/glide/BriarDataFetcher.java +++ b/briar-android/src/main/java/org/briarproject/briar/android/conversation/glide/BriarDataFetcher.java @@ -9,8 +9,8 @@ import com.bumptech.glide.load.data.DataFetcher; import org.briarproject.bramble.api.db.DatabaseExecutor; import org.briarproject.bramble.api.db.DbException; import org.briarproject.bramble.api.nullsafety.NotNullByDefault; -import org.briarproject.bramble.api.sync.MessageId; import org.briarproject.briar.android.attachment.AttachmentItem; +import org.briarproject.briar.api.messaging.Attachment; import org.briarproject.briar.api.messaging.MessagingManager; import java.io.InputStream; @@ -50,11 +50,12 @@ class BriarDataFetcher implements DataFetcher { @Override public void loadData(Priority priority, DataCallback callback) { - MessageId id = attachment.getMessageId(); dbExecutor.execute(() -> { if (cancel) return; try { - inputStream = messagingManager.getAttachment(id).getStream(); + Attachment a = + messagingManager.getAttachment(attachment.getHeader()); + inputStream = a.getStream(); callback.onDataReady(inputStream); } catch (DbException e) { callback.onLoadFailed(e); diff --git a/briar-android/src/test/java/org/briarproject/briar/android/attachment/AttachmentRetrieverTest.java b/briar-android/src/test/java/org/briarproject/briar/android/attachment/AttachmentRetrieverTest.java index f0f38bdb1..a2e7d2108 100644 --- a/briar-android/src/test/java/org/briarproject/briar/android/attachment/AttachmentRetrieverTest.java +++ b/briar-android/src/test/java/org/briarproject/briar/android/attachment/AttachmentRetrieverTest.java @@ -15,6 +15,8 @@ import java.io.InputStream; import static org.briarproject.bramble.test.TestUtils.getRandomBytes; import static org.briarproject.bramble.test.TestUtils.getRandomId; +import static org.briarproject.bramble.util.StringUtils.getRandomString; +import static org.briarproject.briar.api.messaging.MessagingConstants.MAX_CONTENT_TYPE_BYTES; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -25,7 +27,10 @@ public class AttachmentRetrieverTest extends BrambleMockTestCase { 100, 50, 200, 75, 300 ); private final MessageId msgId = new MessageId(getRandomId()); - private final Attachment attachment = new Attachment( + private final String mimeType = getRandomString(MAX_CONTENT_TYPE_BYTES); + private final AttachmentHeader header = + new AttachmentHeader(msgId, mimeType); + private final Attachment attachment = new Attachment(header, new BufferedInputStream( new ByteArrayInputStream(getRandomBytes(42)))); diff --git a/briar-api/src/main/java/org/briarproject/briar/api/messaging/Attachment.java b/briar-api/src/main/java/org/briarproject/briar/api/messaging/Attachment.java index 526beced1..61bb0b0eb 100644 --- a/briar-api/src/main/java/org/briarproject/briar/api/messaging/Attachment.java +++ b/briar-api/src/main/java/org/briarproject/briar/api/messaging/Attachment.java @@ -1,15 +1,27 @@ package org.briarproject.briar.api.messaging; +import org.briarproject.bramble.api.nullsafety.NotNullByDefault; + import java.io.InputStream; +import javax.annotation.concurrent.Immutable; + +@Immutable +@NotNullByDefault public class Attachment { + private final AttachmentHeader header; private final InputStream stream; - public Attachment(InputStream stream) { + public Attachment(AttachmentHeader header, InputStream stream) { + this.header = header; this.stream = stream; } + public AttachmentHeader getHeader() { + return header; + } + public InputStream getStream() { return stream; } diff --git a/briar-api/src/main/java/org/briarproject/briar/api/messaging/MessagingManager.java b/briar-api/src/main/java/org/briarproject/briar/api/messaging/MessagingManager.java index 6352047fc..a4b7fb7e5 100644 --- a/briar-api/src/main/java/org/briarproject/briar/api/messaging/MessagingManager.java +++ b/briar-api/src/main/java/org/briarproject/briar/api/messaging/MessagingManager.java @@ -40,7 +40,7 @@ public interface MessagingManager extends ConversationClient { /** * Stores a local attachment message. * - * @throws FileTooBigException + * @throws FileTooBigException If the attachment is too big */ AttachmentHeader addLocalAttachment(GroupId groupId, long timestamp, String contentType, InputStream is) throws DbException, IOException; @@ -68,9 +68,9 @@ public interface MessagingManager extends ConversationClient { String getMessageText(MessageId m) throws DbException; /** - * Returns the attachment with the given ID. + * Returns the attachment with the given message ID and content type. */ - Attachment getAttachment(MessageId m) throws DbException; + Attachment getAttachment(AttachmentHeader h) throws DbException; /** * Returns true if the contact with the given {@link ContactId} does support diff --git a/briar-core/src/main/java/org/briarproject/briar/messaging/MessagingManagerImpl.java b/briar-core/src/main/java/org/briarproject/briar/messaging/MessagingManagerImpl.java index 2fbbab6ac..e076235e8 100644 --- a/briar-core/src/main/java/org/briarproject/briar/messaging/MessagingManagerImpl.java +++ b/briar-core/src/main/java/org/briarproject/briar/messaging/MessagingManagerImpl.java @@ -374,13 +374,14 @@ class MessagingManagerImpl implements MessagingManager, IncomingMessageHook, } @Override - public Attachment getAttachment(MessageId m) throws DbException { + public Attachment getAttachment(AttachmentHeader h) throws DbException { // TODO: Support large messages + MessageId m = h.getMessageId(); byte[] body = clientHelper.getMessage(m).getBody(); try { BdfDictionary meta = clientHelper.getMessageMetadataAsDictionary(m); int offset = meta.getLong(MSG_KEY_DESCRIPTOR_LENGTH).intValue(); - return new Attachment(new ByteArrayInputStream(body, offset, + return new Attachment(h, new ByteArrayInputStream(body, offset, body.length - offset)); } catch (FormatException e) { throw new DbException(e);