]> nmode's Git Repositories - signal-cli/blobdiff - lib/src/main/java/org/asamk/signal/manager/helper/AttachmentHelper.java
Update libsignal-service-java
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / helper / AttachmentHelper.java
index 951b7c5c74a3dfe63b9f91ec1943e432b880b85e..08526c726ac33747f2952677a8edf0a2a799a5e0 100644 (file)
@@ -9,6 +9,7 @@ import org.asamk.signal.manager.util.IOUtils;
 import org.signal.libsignal.protocol.InvalidMessageException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import org.whispersystems.signalservice.api.crypto.AttachmentCipherInputStream;
 import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
 import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer;
 import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentStream;
@@ -44,14 +45,20 @@ public class AttachmentHelper {
     }
 
     public List<SignalServiceAttachment> uploadAttachments(final List<String> attachments) throws AttachmentInvalidException, IOException {
-        var attachmentStreams = createAttachmentStreams(attachments);
+        final var attachmentStreams = createAttachmentStreams(attachments);
 
-        // Upload attachments here, so we only upload once even for multiple recipients
-        var attachmentPointers = new ArrayList<SignalServiceAttachment>(attachmentStreams.size());
-        for (var attachmentStream : attachmentStreams) {
-            attachmentPointers.add(uploadAttachment(attachmentStream));
+        try {
+            // Upload attachments here, so we only upload once even for multiple recipients
+            final var attachmentPointers = new ArrayList<SignalServiceAttachment>(attachmentStreams.size());
+            for (final var attachmentStream : attachmentStreams) {
+                attachmentPointers.add(uploadAttachment(attachmentStream));
+            }
+            return attachmentPointers;
+        } finally {
+            for (final var attachmentStream : attachmentStreams) {
+                attachmentStream.close();
+            }
         }
-        return attachmentPointers;
     }
 
     private List<SignalServiceAttachmentStream> createAttachmentStreams(List<String> attachments) throws AttachmentInvalidException, IOException {
@@ -60,14 +67,14 @@ public class AttachmentHelper {
         }
         final var signalServiceAttachments = new ArrayList<SignalServiceAttachmentStream>(attachments.size());
         for (var attachment : attachments) {
-            final var uploadSpec = dependencies.getMessageSender().getResumableUploadSpec().toProto();
+            final var uploadSpec = dependencies.getMessageSender().getResumableUploadSpec();
             signalServiceAttachments.add(AttachmentUtils.createAttachmentStream(attachment, uploadSpec));
         }
         return signalServiceAttachments;
     }
 
     public SignalServiceAttachmentPointer uploadAttachment(String attachment) throws IOException, AttachmentInvalidException {
-        final var uploadSpec = dependencies.getMessageSender().getResumableUploadSpec().toProto();
+        final var uploadSpec = dependencies.getMessageSender().getResumableUploadSpec();
         var attachmentStream = AttachmentUtils.createAttachmentStream(attachment, uploadSpec);
         return uploadAttachment(attachmentStream);
     }
@@ -104,9 +111,7 @@ public class AttachmentHelper {
         retrieveAttachment(attachment, input -> IOUtils.copyStream(input, outputStream));
     }
 
-    public void retrieveAttachment(
-            SignalServiceAttachment attachment, AttachmentHandler consumer
-    ) throws IOException {
+    public void retrieveAttachment(SignalServiceAttachment attachment, AttachmentHandler consumer) throws IOException {
         if (attachment.isStream()) {
             var input = attachment.asStream().getInputStream();
             // don't close input stream here, it might be reused later (e.g. with contact sync messages ...)
@@ -131,11 +136,18 @@ public class AttachmentHelper {
     }
 
     private InputStream retrieveAttachmentAsStream(
-            SignalServiceAttachmentPointer pointer, File tmpFile
+            SignalServiceAttachmentPointer pointer,
+            File tmpFile
     ) throws IOException {
+        if (pointer.getDigest().isEmpty()) {
+            throw new IOException("Attachment pointer has no digest.");
+        }
         try {
             return dependencies.getMessageReceiver()
-                    .retrieveAttachment(pointer, tmpFile, ServiceConfig.MAX_ATTACHMENT_SIZE);
+                    .retrieveAttachment(pointer,
+                            tmpFile,
+                            ServiceConfig.MAX_ATTACHMENT_SIZE,
+                            AttachmentCipherInputStream.IntegrityCheck.forEncryptedDigest(pointer.getDigest().get()));
         } catch (MissingConfigurationException | InvalidMessageException e) {
             throw new IOException(e);
         }