mirror of
https://code.briarproject.org/briar/briar.git
synced 2026-02-13 19:29:06 +01:00
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:
@@ -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
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user