Add command to get an attachment (#1080)

* Add command to get an attachment

* Refactor retrieving of attachments to use StreamDetails

* Refactor AttachmentCommand to GetAttachmentCommand

* Minor improvements to GetAttachmentCommand

* Use JSON serializer to serialize binary data

Serializing the stream is better for memory handling than
loading the whole thing into the file.

* Clean up unneeded class

* Added command to doc

Co-authored-by: cedb <cedb@keylimebox.org>
This commit is contained in:
ced-b 2022-11-01 17:47:43 -04:00 committed by GitHub
parent bf76c04664
commit 2e4d346bc8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 138 additions and 0 deletions

View file

@ -2,8 +2,10 @@ package org.asamk.signal.manager;
import org.asamk.signal.manager.util.IOUtils;
import org.asamk.signal.manager.util.MimeUtils;
import org.asamk.signal.manager.util.Utils;
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer;
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentRemoteId;
import org.whispersystems.signalservice.api.util.StreamDetails;
import java.io.File;
import java.io.FileOutputStream;
@ -39,6 +41,14 @@ public class AttachmentStore {
Optional.ofNullable(pointer.getContentType()));
}
public StreamDetails retrieveAttachment(final String id) throws IOException {
final var attachmentFile = new File(attachmentsPath, id);
if (!attachmentFile.exists()) {
return null;
}
return Utils.createStreamDetailsFromFile(attachmentFile);
}
private void storeAttachment(final File attachmentFile, final AttachmentStorer storer) throws IOException {
createAttachmentsDir();
try (OutputStream output = new FileOutputStream(attachmentFile)) {

View file

@ -39,6 +39,7 @@ import org.whispersystems.signalservice.api.util.PhoneNumberFormatter;
import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.time.Duration;
import java.util.Collection;
@ -271,6 +272,8 @@ public interface Manager extends Closeable {
void addClosedListener(Runnable listener);
InputStream retrieveAttachment(final String id) throws IOException;
@Override
void close() throws IOException;

View file

@ -84,6 +84,7 @@ import org.whispersystems.signalservice.internal.util.Util;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
@ -1167,6 +1168,11 @@ class ManagerImpl implements Manager {
}
}
@Override
public InputStream retrieveAttachment(final String id) throws IOException {
return context.getAttachmentHelper().retrieveAttachment(id).getStream();
}
@Override
public void close() {
Thread thread;

View file

@ -13,6 +13,7 @@ import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer;
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentStream;
import org.whispersystems.signalservice.api.push.exceptions.MissingConfigurationException;
import org.whispersystems.signalservice.api.util.StreamDetails;
import java.io.File;
import java.io.IOException;
@ -38,6 +39,11 @@ public class AttachmentHelper {
return attachmentStore.getAttachmentFile(pointer);
}
public StreamDetails retrieveAttachment(final String id) throws IOException {
return attachmentStore.retrieveAttachment(id);
}
public List<SignalServiceAttachment> uploadAttachments(final List<String> attachments) throws AttachmentInvalidException, IOException {
var attachmentStreams = AttachmentUtils.createAttachmentStreams(attachments);