]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/helper/AttachmentHelper.java
0ffa62ed8a2560b3b1ac5afe1051e330f6b1c2b1
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / helper / AttachmentHelper.java
1 package org.asamk.signal.manager.helper;
2
3 import org.asamk.signal.manager.api.AttachmentInvalidException;
4 import org.asamk.signal.manager.config.ServiceConfig;
5 import org.asamk.signal.manager.internal.SignalDependencies;
6 import org.asamk.signal.manager.storage.AttachmentStore;
7 import org.asamk.signal.manager.util.AttachmentUtils;
8 import org.asamk.signal.manager.util.IOUtils;
9 import org.signal.libsignal.protocol.InvalidMessageException;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12 import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
13 import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer;
14 import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentStream;
15 import org.whispersystems.signalservice.api.push.exceptions.MissingConfigurationException;
16 import org.whispersystems.signalservice.api.util.StreamDetails;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.io.InputStream;
21 import java.io.OutputStream;
22 import java.nio.file.Files;
23 import java.util.ArrayList;
24 import java.util.List;
25
26 public class AttachmentHelper {
27
28 private static final Logger logger = LoggerFactory.getLogger(AttachmentHelper.class);
29
30 private final SignalDependencies dependencies;
31 private final AttachmentStore attachmentStore;
32
33 public AttachmentHelper(final Context context) {
34 this.dependencies = context.getDependencies();
35 this.attachmentStore = context.getAttachmentStore();
36 }
37
38 public File getAttachmentFile(SignalServiceAttachmentPointer pointer) {
39 return attachmentStore.getAttachmentFile(pointer);
40 }
41
42 public StreamDetails retrieveAttachment(final String id) throws IOException {
43 return attachmentStore.retrieveAttachment(id);
44 }
45
46 public List<SignalServiceAttachment> uploadAttachments(final List<String> attachments) throws AttachmentInvalidException, IOException {
47 var attachmentStreams = createAttachmentStreams(attachments);
48
49 // Upload attachments here, so we only upload once even for multiple recipients
50 var attachmentPointers = new ArrayList<SignalServiceAttachment>(attachmentStreams.size());
51 for (var attachmentStream : attachmentStreams) {
52 attachmentPointers.add(uploadAttachment(attachmentStream));
53 }
54 return attachmentPointers;
55 }
56
57 private List<SignalServiceAttachmentStream> createAttachmentStreams(List<String> attachments) throws AttachmentInvalidException, IOException {
58 if (attachments == null) {
59 return null;
60 }
61 final var signalServiceAttachments = new ArrayList<SignalServiceAttachmentStream>(attachments.size());
62 for (var attachment : attachments) {
63 final var uploadSpec = dependencies.getMessageSender().getResumableUploadSpec();
64 signalServiceAttachments.add(AttachmentUtils.createAttachmentStream(attachment, uploadSpec));
65 }
66 return signalServiceAttachments;
67 }
68
69 public SignalServiceAttachmentPointer uploadAttachment(String attachment) throws IOException, AttachmentInvalidException {
70 final var uploadSpec = dependencies.getMessageSender().getResumableUploadSpec();
71 var attachmentStream = AttachmentUtils.createAttachmentStream(attachment, uploadSpec);
72 return uploadAttachment(attachmentStream);
73 }
74
75 public SignalServiceAttachmentPointer uploadAttachment(SignalServiceAttachmentStream attachment) throws IOException {
76 var messageSender = dependencies.getMessageSender();
77 return messageSender.uploadAttachment(attachment);
78 }
79
80 public void downloadAttachment(final SignalServiceAttachment attachment) {
81 if (!attachment.isPointer()) {
82 logger.warn("Invalid state, can't store an attachment stream.");
83 }
84
85 var pointer = attachment.asPointer();
86 if (pointer.getPreview().isPresent()) {
87 final var preview = pointer.getPreview().get();
88 try {
89 attachmentStore.storeAttachmentPreview(pointer,
90 outputStream -> outputStream.write(preview, 0, preview.length));
91 } catch (IOException e) {
92 logger.warn("Failed to download attachment preview, ignoring: {}", e.getMessage());
93 }
94 }
95
96 try {
97 attachmentStore.storeAttachment(pointer, outputStream -> this.retrieveAttachment(pointer, outputStream));
98 } catch (IOException e) {
99 logger.warn("Failed to download attachment ({}), ignoring: {}", pointer.getRemoteId(), e.getMessage());
100 }
101 }
102
103 void retrieveAttachment(SignalServiceAttachment attachment, OutputStream outputStream) throws IOException {
104 retrieveAttachment(attachment, input -> IOUtils.copyStream(input, outputStream));
105 }
106
107 public void retrieveAttachment(SignalServiceAttachment attachment, AttachmentHandler consumer) throws IOException {
108 if (attachment.isStream()) {
109 var input = attachment.asStream().getInputStream();
110 // don't close input stream here, it might be reused later (e.g. with contact sync messages ...)
111 consumer.handle(input);
112 return;
113 }
114
115 final var pointer = attachment.asPointer();
116 logger.debug("Retrieving attachment {} with size {}", pointer.getRemoteId(), pointer.getSize());
117 var tmpFile = IOUtils.createTempFile();
118 try (var input = retrieveAttachmentAsStream(pointer, tmpFile)) {
119 consumer.handle(input);
120 } finally {
121 try {
122 Files.delete(tmpFile.toPath());
123 } catch (IOException e) {
124 logger.warn("Failed to delete received attachment temp file “{}”, ignoring: {}",
125 tmpFile,
126 e.getMessage());
127 }
128 }
129 }
130
131 private InputStream retrieveAttachmentAsStream(
132 SignalServiceAttachmentPointer pointer,
133 File tmpFile
134 ) throws IOException {
135 try {
136 return dependencies.getMessageReceiver()
137 .retrieveAttachment(pointer, tmpFile, ServiceConfig.MAX_ATTACHMENT_SIZE);
138 } catch (MissingConfigurationException | InvalidMessageException e) {
139 throw new IOException(e);
140 }
141 }
142
143 @FunctionalInterface
144 public interface AttachmentHandler {
145
146 void handle(InputStream inputStream) throws IOException;
147 }
148 }