]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/helper/AttachmentHelper.java
449a575ef69ece32f1ec6de2a1e4575531923c4c
[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.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;
16
17 import java.io.File;
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;
24
25 public class AttachmentHelper {
26
27 private final static Logger logger = LoggerFactory.getLogger(AttachmentHelper.class);
28
29 private final SignalDependencies dependencies;
30 private final AttachmentStore attachmentStore;
31
32 public AttachmentHelper(
33 final SignalDependencies dependencies, final AttachmentStore attachmentStore
34 ) {
35 this.dependencies = dependencies;
36 this.attachmentStore = attachmentStore;
37 }
38
39 public File getAttachmentFile(SignalServiceAttachmentRemoteId attachmentId) {
40 return attachmentStore.getAttachmentFile(attachmentId);
41 }
42
43 public List<SignalServiceAttachment> uploadAttachments(final List<String> attachments) throws AttachmentInvalidException, IOException {
44 var attachmentStreams = AttachmentUtils.getSignalServiceAttachments(attachments);
45
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());
54 }
55 }
56 return attachmentPointers;
57 }
58
59 public void downloadAttachment(final SignalServiceAttachment attachment) {
60 if (!attachment.isPointer()) {
61 logger.warn("Invalid state, can't store an attachment stream.");
62 }
63
64 var pointer = attachment.asPointer();
65 if (pointer.getPreview().isPresent()) {
66 final var preview = pointer.getPreview().get();
67 try {
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());
72 }
73 }
74
75 try {
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());
80 }
81 }
82
83 void retrieveAttachment(SignalServiceAttachment attachment, OutputStream outputStream) throws IOException {
84 retrieveAttachment(attachment, input -> IOUtils.copyStream(input, outputStream));
85 }
86
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);
94 return;
95 }
96
97 var tmpFile = IOUtils.createTempFile();
98 try (var input = retrieveAttachmentAsStream(attachment.asPointer(), tmpFile)) {
99 consumer.handle(input);
100 } finally {
101 try {
102 Files.delete(tmpFile.toPath());
103 } catch (IOException e) {
104 logger.warn("Failed to delete received attachment temp file “{}”, ignoring: {}",
105 tmpFile,
106 e.getMessage());
107 }
108 }
109 }
110
111 private InputStream retrieveAttachmentAsStream(
112 SignalServiceAttachmentPointer pointer, File tmpFile
113 ) throws IOException {
114 try {
115 return dependencies.getMessageReceiver()
116 .retrieveAttachment(pointer, tmpFile, ServiceConfig.MAX_ATTACHMENT_SIZE);
117 } catch (MissingConfigurationException | InvalidMessageException e) {
118 throw new IOException(e);
119 }
120 }
121
122 @FunctionalInterface
123 public interface AttachmentHandler {
124
125 void handle(InputStream inputStream) throws IOException;
126 }
127 }