1 package org
.asamk
.signal
.manager
.helper
;
3 import org
.asamk
.signal
.manager
.AttachmentInvalidException
;
4 import org
.asamk
.signal
.manager
.AttachmentStore
;
5 import org
.asamk
.signal
.manager
.SignalDependencies
;
6 import org
.asamk
.signal
.manager
.config
.ServiceConfig
;
7 import org
.asamk
.signal
.manager
.util
.AttachmentUtils
;
8 import org
.asamk
.signal
.manager
.util
.IOUtils
;
9 import org
.slf4j
.Logger
;
10 import org
.slf4j
.LoggerFactory
;
11 import org
.whispersystems
.libsignal
.InvalidMessageException
;
12 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachment
;
13 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachmentPointer
;
14 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachmentRemoteId
;
15 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.MissingConfigurationException
;
18 import java
.io
.IOException
;
19 import java
.io
.InputStream
;
20 import java
.io
.OutputStream
;
21 import java
.nio
.file
.Files
;
22 import java
.util
.ArrayList
;
23 import java
.util
.List
;
25 public class AttachmentHelper
{
27 private final static Logger logger
= LoggerFactory
.getLogger(AttachmentHelper
.class);
29 private final SignalDependencies dependencies
;
30 private final AttachmentStore attachmentStore
;
32 public AttachmentHelper(
33 final SignalDependencies dependencies
, final AttachmentStore attachmentStore
35 this.dependencies
= dependencies
;
36 this.attachmentStore
= attachmentStore
;
39 public File
getAttachmentFile(SignalServiceAttachmentRemoteId attachmentId
) {
40 return attachmentStore
.getAttachmentFile(attachmentId
);
43 public List
<SignalServiceAttachment
> uploadAttachments(final List
<String
> attachments
) throws AttachmentInvalidException
, IOException
{
44 var attachmentStreams
= AttachmentUtils
.getSignalServiceAttachments(attachments
);
46 // Upload attachments here, so we only upload once even for multiple recipients
47 var messageSender
= dependencies
.getMessageSender();
48 var attachmentPointers
= new ArrayList
<SignalServiceAttachment
>(attachmentStreams
.size());
49 for (var attachment
: attachmentStreams
) {
50 if (attachment
.isStream()) {
51 attachmentPointers
.add(messageSender
.uploadAttachment(attachment
.asStream()));
52 } else if (attachment
.isPointer()) {
53 attachmentPointers
.add(attachment
.asPointer());
56 return attachmentPointers
;
59 public void downloadAttachment(final SignalServiceAttachment attachment
) {
60 if (!attachment
.isPointer()) {
61 logger
.warn("Invalid state, can't store an attachment stream.");
64 var pointer
= attachment
.asPointer();
65 if (pointer
.getPreview().isPresent()) {
66 final var preview
= pointer
.getPreview().get();
68 attachmentStore
.storeAttachmentPreview(pointer
.getRemoteId(),
69 outputStream
-> outputStream
.write(preview
, 0, preview
.length
));
70 } catch (IOException e
) {
71 logger
.warn("Failed to download attachment preview, ignoring: {}", e
.getMessage());
76 attachmentStore
.storeAttachment(pointer
.getRemoteId(),
77 outputStream
-> this.retrieveAttachment(pointer
, outputStream
));
78 } catch (IOException e
) {
79 logger
.warn("Failed to download attachment ({}), ignoring: {}", pointer
.getRemoteId(), e
.getMessage());
83 void retrieveAttachment(SignalServiceAttachment attachment
, OutputStream outputStream
) throws IOException
{
84 retrieveAttachment(attachment
, input
-> IOUtils
.copyStream(input
, outputStream
));
87 public void retrieveAttachment(
88 SignalServiceAttachment attachment
, AttachmentHandler consumer
89 ) throws IOException
{
90 if (attachment
.isStream()) {
91 var input
= attachment
.asStream().getInputStream();
92 // don't close input stream here, it might be reused later (e.g. with contact sync messages ...)
93 consumer
.handle(input
);
97 var tmpFile
= IOUtils
.createTempFile();
98 try (var input
= retrieveAttachmentAsStream(attachment
.asPointer(), tmpFile
)) {
99 consumer
.handle(input
);
102 Files
.delete(tmpFile
.toPath());
103 } catch (IOException e
) {
104 logger
.warn("Failed to delete received attachment temp file “{}”, ignoring: {}",
111 private InputStream
retrieveAttachmentAsStream(
112 SignalServiceAttachmentPointer pointer
, File tmpFile
113 ) throws IOException
{
115 return dependencies
.getMessageReceiver()
116 .retrieveAttachment(pointer
, tmpFile
, ServiceConfig
.MAX_ATTACHMENT_SIZE
);
117 } catch (MissingConfigurationException
| InvalidMessageException e
) {
118 throw new IOException(e
);
123 public interface AttachmentHandler
{
125 void handle(InputStream inputStream
) throws IOException
;