]> nmode's Git Repositories - signal-cli/blobdiff - lib/src/main/java/org/asamk/signal/manager/util/AttachmentUtils.java
Remove unnecessary proto conversion
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / util / AttachmentUtils.java
index ec043cfd628706bc41e4c78687c785a19ebdf2b5..ac15a30a9cf9342598b94c047dc0957e7facaf91 100644 (file)
@@ -1,62 +1,42 @@
 package org.asamk.signal.manager.util;
 
-import org.asamk.signal.manager.AttachmentInvalidException;
-import org.whispersystems.libsignal.util.guava.Optional;
-import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
+import org.asamk.signal.manager.api.AttachmentInvalidException;
 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.File;
 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<SignalServiceAttachment> getSignalServiceAttachments(List<String> attachments) throws AttachmentInvalidException {
-        List<SignalServiceAttachment> signalServiceAttachments = null;
-        if (attachments != null) {
-            signalServiceAttachments = new ArrayList<>(attachments.size());
-            for (String attachment : attachments) {
-                try {
-                    signalServiceAttachments.add(createAttachment(new File(attachment)));
-                } catch (IOException e) {
-                    throw new AttachmentInvalidException(attachment, e);
-                }
-            }
-        }
-        return signalServiceAttachments;
-    }
+    public static SignalServiceAttachmentStream createAttachmentStream(
+            String attachment, ResumableUploadSpec resumableUploadSpec
+    ) throws AttachmentInvalidException {
+        try {
+            final var streamDetails = Utils.createStreamDetails(attachment);
 
-    public static SignalServiceAttachmentStream createAttachment(File attachmentFile) throws IOException {
-        final StreamDetails streamDetails = Utils.createStreamDetailsFromFile(attachmentFile);
-        return createAttachment(streamDetails, Optional.of(attachmentFile.getName()));
+            return createAttachmentStream(streamDetails.first(), streamDetails.second(), resumableUploadSpec);
+        } catch (IOException e) {
+            throw new AttachmentInvalidException(attachment, e);
+        }
     }
 
-    public static SignalServiceAttachmentStream createAttachment(
-            StreamDetails streamDetails, Optional<String> name
-    ) {
-        // TODO mabybe add a parameter to set the voiceNote, borderless, preview, width, height and caption option
-        final long uploadTimestamp = System.currentTimeMillis();
-        Optional<byte[]> preview = Optional.absent();
-        Optional<String> caption = Optional.absent();
-        Optional<String> blurHash = Optional.absent();
-        final Optional<ResumableUploadSpec> resumableUploadSpec = Optional.absent();
-        return new SignalServiceAttachmentStream(streamDetails.getStream(),
-                streamDetails.getContentType(),
-                streamDetails.getLength(),
-                name,
-                false,
-                false,
-                preview,
-                0,
-                0,
-                uploadTimestamp,
-                caption,
-                blurHash,
-                null,
-                null,
-                resumableUploadSpec);
+    public static SignalServiceAttachmentStream createAttachmentStream(
+            StreamDetails streamDetails, Optional<String> name, ResumableUploadSpec resumableUploadSpec
+    ) throws ResumeLocationInvalidException {
+        // TODO maybe add a parameter to set the voiceNote, borderless, preview, width, height and caption option
+        final var uploadTimestamp = System.currentTimeMillis();
+        return SignalServiceAttachmentStream.newStreamBuilder()
+                .withStream(streamDetails.getStream())
+                .withContentType(streamDetails.getContentType())
+                .withLength(streamDetails.getLength())
+                .withFileName(name.orElse(null))
+                .withUploadTimestamp(uploadTimestamp)
+                .withResumableUploadSpec(resumableUploadSpec)
+                .withUuid(UUID.randomUUID())
+                .build();
     }
 }