Centralize attachment loading in AttachmentReader

This is needed so Glide can load attachments from the DB by using the same AttachmentHeader class.
This commit is contained in:
Torsten Grote
2020-11-24 10:30:53 -03:00
parent cf8f5c989f
commit d0d2e0ed82
5 changed files with 60 additions and 45 deletions

View File

@@ -4,12 +4,13 @@ import com.bumptech.glide.Priority;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.data.DataFetcher;
import org.briarproject.bramble.api.client.ClientHelper;
import org.briarproject.bramble.api.db.DatabaseExecutor;
import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.briar.android.attachment.AttachmentItem;
import org.briarproject.briar.api.media.Attachment;
import org.briarproject.briar.api.messaging.MessagingManager;
import org.briarproject.briar.media.AttachmentReader;
import java.io.InputStream;
import java.util.concurrent.Executor;
@@ -30,7 +31,7 @@ class BriarDataFetcher implements DataFetcher<InputStream> {
private final static Logger LOG =
getLogger(BriarDataFetcher.class.getName());
private final MessagingManager messagingManager;
private final ClientHelper clientHelper;
@DatabaseExecutor
private final Executor dbExecutor;
private final AttachmentItem attachment;
@@ -40,9 +41,9 @@ class BriarDataFetcher implements DataFetcher<InputStream> {
private volatile boolean cancel = false;
@Inject
BriarDataFetcher(MessagingManager messagingManager,
BriarDataFetcher(ClientHelper clientHelper,
@DatabaseExecutor Executor dbExecutor, AttachmentItem attachment) {
this.messagingManager = messagingManager;
this.clientHelper = clientHelper;
this.dbExecutor = dbExecutor;
this.attachment = attachment;
}
@@ -53,8 +54,8 @@ class BriarDataFetcher implements DataFetcher<InputStream> {
dbExecutor.execute(() -> {
if (cancel) return;
try {
Attachment a =
messagingManager.getAttachment(attachment.getHeader());
Attachment a = AttachmentReader
.getAttachment(clientHelper, attachment.getHeader());
inputStream = a.getStream();
callback.onDataReady(inputStream);
} catch (DbException e) {

View File

@@ -1,9 +1,9 @@
package org.briarproject.briar.android.conversation.glide;
import org.briarproject.bramble.api.client.ClientHelper;
import org.briarproject.bramble.api.db.DatabaseExecutor;
import org.briarproject.bramble.api.nullsafety.NotNullByDefault;
import org.briarproject.briar.android.attachment.AttachmentItem;
import org.briarproject.briar.api.messaging.MessagingManager;
import java.util.concurrent.Executor;
@@ -12,19 +12,19 @@ import javax.inject.Inject;
@NotNullByDefault
public class BriarDataFetcherFactory {
private final MessagingManager messagingManager;
private final ClientHelper clientHelper;
@DatabaseExecutor
private final Executor dbExecutor;
@Inject
public BriarDataFetcherFactory(MessagingManager messagingManager,
public BriarDataFetcherFactory(ClientHelper clientHelper,
@DatabaseExecutor Executor dbExecutor) {
this.messagingManager = messagingManager;
this.clientHelper = clientHelper;
this.dbExecutor = dbExecutor;
}
BriarDataFetcher createBriarDataFetcher(AttachmentItem model) {
return new BriarDataFetcher(messagingManager, dbExecutor, model);
return new BriarDataFetcher(clientHelper, dbExecutor, model);
}
}

View File

@@ -33,9 +33,8 @@ import org.briarproject.briar.api.avatar.event.AvatarUpdatedEvent;
import org.briarproject.briar.api.media.Attachment;
import org.briarproject.briar.api.media.AttachmentHeader;
import org.briarproject.briar.api.media.FileTooBigException;
import org.briarproject.briar.api.media.InvalidAttachmentException;
import org.briarproject.briar.media.AttachmentReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -254,19 +253,7 @@ class AvatarManagerImpl implements AvatarManager, OpenDatabaseHook, ContactHook,
@Override
public Attachment getAvatar(AttachmentHeader h) throws DbException {
MessageId m = h.getMessageId();
byte[] body = clientHelper.getMessage(m).getBody();
try {
BdfDictionary meta = clientHelper.getMessageMetadataAsDictionary(m);
String contentType = meta.getString(MSG_KEY_CONTENT_TYPE);
if (!contentType.equals(h.getContentType()))
throw new InvalidAttachmentException();
int offset = meta.getLong(MSG_KEY_DESCRIPTOR_LENGTH).intValue();
return new Attachment(h, new ByteArrayInputStream(body, offset,
body.length - offset));
} catch (FormatException e) {
throw new DbException(e);
}
return AttachmentReader.getAttachment(clientHelper, h);
}
@Nullable

View File

@@ -0,0 +1,44 @@
package org.briarproject.briar.media;
import org.briarproject.bramble.api.FormatException;
import org.briarproject.bramble.api.client.ClientHelper;
import org.briarproject.bramble.api.data.BdfDictionary;
import org.briarproject.bramble.api.db.DbException;
import org.briarproject.bramble.api.sync.MessageId;
import org.briarproject.briar.api.media.Attachment;
import org.briarproject.briar.api.media.AttachmentHeader;
import org.briarproject.briar.api.media.InvalidAttachmentException;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import static org.briarproject.briar.media.MediaConstants.MSG_KEY_CONTENT_TYPE;
import static org.briarproject.briar.media.MediaConstants.MSG_KEY_DESCRIPTOR_LENGTH;
public class AttachmentReader {
public static Attachment getAttachment(ClientHelper clientHelper,
AttachmentHeader h) throws DbException {
// TODO: Support large messages
MessageId m = h.getMessageId();
byte[] body = clientHelper.getMessage(m).getBody();
try {
BdfDictionary meta = clientHelper.getMessageMetadataAsDictionary(m);
String contentType = meta.getString(MSG_KEY_CONTENT_TYPE);
if (!contentType.equals(h.getContentType()))
throw new InvalidAttachmentException();
int offset;
try {
offset = meta.getLong(MSG_KEY_DESCRIPTOR_LENGTH).intValue();
} catch (FormatException e) {
throw new InvalidAttachmentException();
}
InputStream stream = new ByteArrayInputStream(body, offset,
body.length - offset);
return new Attachment(h, stream);
} catch (FormatException e) {
throw new DbException(e);
}
}
}

View File

@@ -35,14 +35,13 @@ import org.briarproject.briar.api.conversation.DeletionResult;
import org.briarproject.briar.api.media.Attachment;
import org.briarproject.briar.api.media.AttachmentHeader;
import org.briarproject.briar.api.media.FileTooBigException;
import org.briarproject.briar.api.media.InvalidAttachmentException;
import org.briarproject.briar.api.messaging.MessagingManager;
import org.briarproject.briar.api.messaging.PrivateMessage;
import org.briarproject.briar.api.messaging.PrivateMessageHeader;
import org.briarproject.briar.api.messaging.event.AttachmentReceivedEvent;
import org.briarproject.briar.api.messaging.event.PrivateMessageReceivedEvent;
import org.briarproject.briar.media.AttachmentReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -402,23 +401,7 @@ class MessagingManagerImpl implements MessagingManager, IncomingMessageHook,
@Override
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);
Long messageType = meta.getOptionalLong(MSG_KEY_MSG_TYPE);
if (messageType == null || messageType != ATTACHMENT)
throw new InvalidAttachmentException();
String contentType = meta.getString(MSG_KEY_CONTENT_TYPE);
if (!contentType.equals(h.getContentType()))
throw new InvalidAttachmentException();
int offset = meta.getLong(MSG_KEY_DESCRIPTOR_LENGTH).intValue();
return new Attachment(h, new ByteArrayInputStream(body, offset,
body.length - offset));
} catch (FormatException e) {
throw new DbException(e);
}
return AttachmentReader.getAttachment(clientHelper, h);
}
@Override