]> nmode's Git Repositories - signal-cli/blobdiff - lib/src/main/java/org/asamk/signal/manager/util/AttachmentUtils.java
Use UploadSpec for attachment uploads
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / util / AttachmentUtils.java
index d06d87459c8738e2de035d80876be68f98035e03..5504640fa8ca3dbb592531031269505f7a3125bb 100644 (file)
@@ -1,65 +1,44 @@
 package org.asamk.signal.manager.util;
 
 import org.asamk.signal.manager.api.AttachmentInvalidException;
+import org.signal.protos.resumableuploads.ResumableUpload;
 import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentStream;
+import org.whispersystems.signalservice.api.push.exceptions.ResumeLocationInvalidException;
 import org.whispersystems.signalservice.api.util.StreamDetails;
 import org.whispersystems.signalservice.internal.push.http.ResumableUploadSpec;
 
 import java.io.IOException;
-import java.util.ArrayList;
-import java.util.List;
 import java.util.Optional;
 import java.util.UUID;
 
 public class AttachmentUtils {
 
-    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(attachment));
-        }
-        return signalServiceAttachments;
-    }
-
-    public static SignalServiceAttachmentStream createAttachmentStream(String attachment) throws AttachmentInvalidException {
+    public static SignalServiceAttachmentStream createAttachmentStream(
+            String attachment, ResumableUpload resumableUpload
+    ) throws AttachmentInvalidException {
         try {
             final var streamDetails = Utils.createStreamDetails(attachment);
 
-            return createAttachmentStream(streamDetails.first(), streamDetails.second());
+            return createAttachmentStream(streamDetails.first(), streamDetails.second(), resumableUpload);
         } catch (IOException e) {
             throw new AttachmentInvalidException(attachment, e);
         }
     }
 
     public static SignalServiceAttachmentStream createAttachmentStream(
-            StreamDetails streamDetails, Optional<String> name
-    ) {
+            StreamDetails streamDetails, Optional<String> name, ResumableUpload resumableUpload
+    ) throws ResumeLocationInvalidException {
         // 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();
-        Optional<String> blurHash = Optional.empty();
-        final Optional<ResumableUploadSpec> resumableUploadSpec = Optional.empty();
-        return new SignalServiceAttachmentStream(streamDetails.getStream(),
-                streamDetails.getContentType(),
-                streamDetails.getLength(),
-                name,
-                false,
-                false,
-                false,
-                false,
-                preview,
-                0,
-                0,
-                uploadTimestamp,
-                caption,
-                blurHash,
-                null,
-                null,
-                resumableUploadSpec,
-                UUID.randomUUID());
+        final var resumableUploadSpec = ResumableUploadSpec.from(resumableUpload);
+        return SignalServiceAttachmentStream.newStreamBuilder()
+                .withStream(streamDetails.getStream())
+                .withContentType(streamDetails.getContentType())
+                .withLength(streamDetails.getLength())
+                .withFileName(name.orElse(null))
+                .withUploadTimestamp(uploadTimestamp)
+                .withResumableUploadSpec(resumableUploadSpec)
+                .withUuid(UUID.randomUUID())
+                .build();
     }
 }