]> nmode's Git Repositories - signal-cli/commitdiff
Refactor attachment upload helpers
authorAsamK <asamk@gmx.de>
Wed, 25 May 2022 20:31:15 +0000 (22:31 +0200)
committerAsamK <asamk@gmx.de>
Wed, 25 May 2022 20:31:15 +0000 (22:31 +0200)
lib/src/main/java/org/asamk/signal/manager/ManagerImpl.java
lib/src/main/java/org/asamk/signal/manager/helper/AttachmentHelper.java
lib/src/main/java/org/asamk/signal/manager/helper/GroupHelper.java
lib/src/main/java/org/asamk/signal/manager/helper/SyncHelper.java
lib/src/main/java/org/asamk/signal/manager/util/AttachmentUtils.java

index 22fcc14137791b4adce4d708ff7204171aa62fc8..40bd7240a9da9485f22a4880adb65ce39c7ea161 100644 (file)
@@ -590,7 +590,7 @@ class ManagerImpl implements Manager {
                     stickerPack.getPackKey(),
                     stickerId,
                     manifestSticker.emoji(),
-                    AttachmentUtils.createAttachment(streamDetails, Optional.empty())));
+                    AttachmentUtils.createAttachmentStream(streamDetails, Optional.empty())));
         }
     }
 
index bad3d788331e010891409b74ce068b6ed05887b1..8793ce936e64459392cb957d815c3c67c8d1d8f0 100644 (file)
@@ -12,6 +12,7 @@ import org.slf4j.LoggerFactory;
 import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
 import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer;
 import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentRemoteId;
+import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentStream;
 import org.whispersystems.signalservice.api.push.exceptions.MissingConfigurationException;
 
 import java.io.File;
@@ -39,21 +40,26 @@ public class AttachmentHelper {
     }
 
     public List<SignalServiceAttachment> uploadAttachments(final List<String> attachments) throws AttachmentInvalidException, IOException {
-        var attachmentStreams = AttachmentUtils.getSignalServiceAttachments(attachments);
+        var attachmentStreams = AttachmentUtils.createAttachmentStreams(attachments);
 
         // Upload attachments here, so we only upload once even for multiple recipients
-        var messageSender = dependencies.getMessageSender();
         var attachmentPointers = new ArrayList<SignalServiceAttachment>(attachmentStreams.size());
-        for (var attachment : attachmentStreams) {
-            if (attachment.isStream()) {
-                attachmentPointers.add(messageSender.uploadAttachment(attachment.asStream()));
-            } else if (attachment.isPointer()) {
-                attachmentPointers.add(attachment.asPointer());
-            }
+        for (var attachmentStream : attachmentStreams) {
+            attachmentPointers.add(uploadAttachment(attachmentStream));
         }
         return attachmentPointers;
     }
 
+    public SignalServiceAttachmentPointer uploadAttachment(String attachment) throws IOException, AttachmentInvalidException {
+        var attachmentStream = AttachmentUtils.createAttachmentStream(new File(attachment));
+        return uploadAttachment(attachmentStream);
+    }
+
+    public SignalServiceAttachmentPointer uploadAttachment(SignalServiceAttachmentStream attachment) throws IOException {
+        var messageSender = dependencies.getMessageSender();
+        return messageSender.uploadAttachment(attachment);
+    }
+
     public void downloadAttachment(final SignalServiceAttachment attachment) {
         if (!attachment.isPointer()) {
             logger.warn("Invalid state, can't store an attachment stream.");
index f4e9232085194acf8efd5e98c1e0d803eea0abc4..3ed642ede0af87bafc43e672f7cf31c074866bbd 100644 (file)
@@ -98,7 +98,7 @@ public class GroupHelper {
             return Optional.empty();
         }
 
-        return Optional.of(AttachmentUtils.createAttachment(streamDetails, Optional.empty()));
+        return Optional.of(AttachmentUtils.createAttachmentStream(streamDetails, Optional.empty()));
     }
 
     public GroupInfoV2 getOrMigrateGroup(
index 035e57706757282416500ac29a7d721165fab22b..4e77d738bcb3890a7627649e39cadbbf09dc7683 100644 (file)
@@ -302,7 +302,7 @@ public class SyncHelper {
             return Optional.empty();
         }
 
-        return Optional.of(AttachmentUtils.createAttachment(streamDetails, Optional.empty()));
+        return Optional.of(AttachmentUtils.createAttachmentStream(streamDetails, Optional.empty()));
     }
 
     private void downloadContactAvatar(SignalServiceAttachment avatar, RecipientAddress address) {
index a19a6fcf7916080be70fdbea3c2f5ea66a21506c..a94d76738f442040abf55b83158ad253e1c4d575 100644 (file)
@@ -1,7 +1,6 @@
 package org.asamk.signal.manager.util;
 
 import org.asamk.signal.manager.api.AttachmentInvalidException;
-import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
 import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentStream;
 import org.whispersystems.signalservice.api.util.StreamDetails;
 import org.whispersystems.signalservice.internal.push.http.ResumableUploadSpec;
@@ -14,30 +13,30 @@ import java.util.Optional;
 
 public class AttachmentUtils {
 
-    public static List<SignalServiceAttachment> getSignalServiceAttachments(List<String> attachments) throws AttachmentInvalidException {
-        List<SignalServiceAttachment> signalServiceAttachments = null;
-        if (attachments != null) {
-            signalServiceAttachments = new ArrayList<>(attachments.size());
-            for (var attachment : attachments) {
-                try {
-                    signalServiceAttachments.add(createAttachment(new File(attachment)));
-                } catch (IOException e) {
-                    throw new AttachmentInvalidException(attachment, e);
-                }
-            }
+    public static List<SignalServiceAttachmentStream> createAttachmentStreams(List<String> attachments) throws AttachmentInvalidException {
+        if (attachments == null) {
+            return null;
+        }
+        final var signalServiceAttachments = new ArrayList<SignalServiceAttachmentStream>(attachments.size());
+        for (var attachment : attachments) {
+            signalServiceAttachments.add(createAttachmentStream(new File(attachment)));
         }
         return signalServiceAttachments;
     }
 
-    public static SignalServiceAttachmentStream createAttachment(File attachmentFile) throws IOException {
-        final var streamDetails = Utils.createStreamDetailsFromFile(attachmentFile);
-        return createAttachment(streamDetails, Optional.of(attachmentFile.getName()));
+    public static SignalServiceAttachmentStream createAttachmentStream(File attachmentFile) throws AttachmentInvalidException {
+        try {
+            final var streamDetails = Utils.createStreamDetailsFromFile(attachmentFile);
+            return createAttachmentStream(streamDetails, Optional.of(attachmentFile.getName()));
+        } catch (IOException e) {
+            throw new AttachmentInvalidException(attachmentFile.toString(), e);
+        }
     }
 
-    public static SignalServiceAttachmentStream createAttachment(
+    public static SignalServiceAttachmentStream createAttachmentStream(
             StreamDetails streamDetails, Optional<String> name
     ) {
-        // TODO mabybe add a parameter to set the voiceNote, borderless, preview, width, height and caption option
+        // TODO maybe add a parameter to set the voiceNote, borderless, preview, width, height and caption option
         final var uploadTimestamp = System.currentTimeMillis();
         Optional<byte[]> preview = Optional.empty();
         Optional<String> caption = Optional.empty();