]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/util/AttachmentUtils.java
5504640fa8ca3dbb592531031269505f7a3125bb
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / util / AttachmentUtils.java
1 package org.asamk.signal.manager.util;
2
3 import org.asamk.signal.manager.api.AttachmentInvalidException;
4 import org.signal.protos.resumableuploads.ResumableUpload;
5 import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentStream;
6 import org.whispersystems.signalservice.api.push.exceptions.ResumeLocationInvalidException;
7 import org.whispersystems.signalservice.api.util.StreamDetails;
8 import org.whispersystems.signalservice.internal.push.http.ResumableUploadSpec;
9
10 import java.io.IOException;
11 import java.util.Optional;
12 import java.util.UUID;
13
14 public class AttachmentUtils {
15
16 public static SignalServiceAttachmentStream createAttachmentStream(
17 String attachment, ResumableUpload resumableUpload
18 ) throws AttachmentInvalidException {
19 try {
20 final var streamDetails = Utils.createStreamDetails(attachment);
21
22 return createAttachmentStream(streamDetails.first(), streamDetails.second(), resumableUpload);
23 } catch (IOException e) {
24 throw new AttachmentInvalidException(attachment, e);
25 }
26 }
27
28 public static SignalServiceAttachmentStream createAttachmentStream(
29 StreamDetails streamDetails, Optional<String> name, ResumableUpload resumableUpload
30 ) throws ResumeLocationInvalidException {
31 // TODO maybe add a parameter to set the voiceNote, borderless, preview, width, height and caption option
32 final var uploadTimestamp = System.currentTimeMillis();
33 final var resumableUploadSpec = ResumableUploadSpec.from(resumableUpload);
34 return SignalServiceAttachmentStream.newStreamBuilder()
35 .withStream(streamDetails.getStream())
36 .withContentType(streamDetails.getContentType())
37 .withLength(streamDetails.getLength())
38 .withFileName(name.orElse(null))
39 .withUploadTimestamp(uploadTimestamp)
40 .withResumableUploadSpec(resumableUploadSpec)
41 .withUuid(UUID.randomUUID())
42 .build();
43 }
44 }