]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/helper/AttachmentHelper.java
Small improvements
[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.AttachmentStore;
4 import org.asamk.signal.manager.SignalDependencies;
5 import org.asamk.signal.manager.api.AttachmentInvalidException;
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.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 final static 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 = AttachmentUtils.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 public SignalServiceAttachmentPointer uploadAttachment(String attachment) throws IOException, AttachmentInvalidException {
58 var attachmentStream = AttachmentUtils.createAttachmentStream(attachment);
59 return uploadAttachment(attachmentStream);
60 }
61
62 public SignalServiceAttachmentPointer uploadAttachment(SignalServiceAttachmentStream attachment) throws IOException {
63 var messageSender = dependencies.getMessageSender();
64 return messageSender.uploadAttachment(attachment);
65 }
66
67 public void downloadAttachment(final SignalServiceAttachment attachment) {
68 if (!attachment.isPointer()) {
69 logger.warn("Invalid state, can't store an attachment stream.");
70 }
71
72 var pointer = attachment.asPointer();
73 if (pointer.getPreview().isPresent()) {
74 final var preview = pointer.getPreview().get();
75 try {
76 attachmentStore.storeAttachmentPreview(pointer,
77 outputStream -> outputStream.write(preview, 0, preview.length));
78 } catch (IOException e) {
79 logger.warn("Failed to download attachment preview, ignoring: {}", e.getMessage());
80 }
81 }
82
83 try {
84 attachmentStore.storeAttachment(pointer, outputStream -> this.retrieveAttachment(pointer, outputStream));
85 } catch (IOException e) {
86 logger.warn("Failed to download attachment ({}), ignoring: {}", pointer.getRemoteId(), e.getMessage());
87 }
88 }
89
90 void retrieveAttachment(SignalServiceAttachment attachment, OutputStream outputStream) throws IOException {
91 retrieveAttachment(attachment, input -> IOUtils.copyStream(input, outputStream));
92 }
93
94 public void retrieveAttachment(
95 SignalServiceAttachment attachment, AttachmentHandler consumer
96 ) throws IOException {
97 if (attachment.isStream()) {
98 var input = attachment.asStream().getInputStream();
99 // don't close input stream here, it might be reused later (e.g. with contact sync messages ...)
100 consumer.handle(input);
101 return;
102 }
103
104 final var pointer = attachment.asPointer();
105 logger.debug("Retrieving attachment {} with size {}", pointer.getRemoteId(), pointer.getSize());
106 var tmpFile = IOUtils.createTempFile();
107 try (var input = retrieveAttachmentAsStream(pointer, tmpFile)) {
108 consumer.handle(input);
109 } finally {
110 try {
111 Files.delete(tmpFile.toPath());
112 } catch (IOException e) {
113 logger.warn("Failed to delete received attachment temp file “{}”, ignoring: {}",
114 tmpFile,
115 e.getMessage());
116 }
117 }
118 }
119
120 private InputStream retrieveAttachmentAsStream(
121 SignalServiceAttachmentPointer pointer, File tmpFile
122 ) throws IOException {
123 try {
124 return dependencies.getMessageReceiver()
125 .retrieveAttachment(pointer, tmpFile, ServiceConfig.MAX_ATTACHMENT_SIZE);
126 } catch (MissingConfigurationException | InvalidMessageException e) {
127 throw new IOException(e);
128 }
129 }
130
131 @FunctionalInterface
132 public interface AttachmentHandler {
133
134 void handle(InputStream inputStream) throws IOException;
135 }
136 }