]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/manager/util/AttachmentUtils.java
4be85dddf81f940dc4c022121263945841410cb0
[signal-cli] / src / main / java / org / asamk / signal / manager / util / AttachmentUtils.java
1 package org.asamk.signal.manager.util;
2
3 import org.asamk.signal.manager.AttachmentInvalidException;
4 import org.whispersystems.libsignal.util.guava.Optional;
5 import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
6 import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentStream;
7 import org.whispersystems.signalservice.api.util.StreamDetails;
8 import org.whispersystems.signalservice.internal.push.http.ResumableUploadSpec;
9
10 import java.io.File;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.io.OutputStream;
14 import java.util.ArrayList;
15 import java.util.List;
16
17 public class AttachmentUtils {
18
19 public static List<SignalServiceAttachment> getSignalServiceAttachments(List<String> attachments) throws AttachmentInvalidException {
20 List<SignalServiceAttachment> signalServiceAttachments = null;
21 if (attachments != null) {
22 signalServiceAttachments = new ArrayList<>(attachments.size());
23 for (String attachment : attachments) {
24 try {
25 signalServiceAttachments.add(createAttachment(new File(attachment)));
26 } catch (IOException e) {
27 throw new AttachmentInvalidException(attachment, e);
28 }
29 }
30 }
31 return signalServiceAttachments;
32 }
33
34 public static SignalServiceAttachmentStream createAttachment(File attachmentFile) throws IOException {
35 final StreamDetails streamDetails = Utils.createStreamDetailsFromFile(attachmentFile);
36 return createAttachment(streamDetails, Optional.of(attachmentFile.getName()));
37 }
38
39 public static SignalServiceAttachmentStream createAttachment(
40 StreamDetails streamDetails, Optional<String> name
41 ) {
42 // TODO mabybe add a parameter to set the voiceNote, borderless, preview, width, height and caption option
43 final long uploadTimestamp = System.currentTimeMillis();
44 Optional<byte[]> preview = Optional.absent();
45 Optional<String> caption = Optional.absent();
46 Optional<String> blurHash = Optional.absent();
47 final Optional<ResumableUploadSpec> resumableUploadSpec = Optional.absent();
48 return new SignalServiceAttachmentStream(streamDetails.getStream(),
49 streamDetails.getContentType(),
50 streamDetails.getLength(),
51 name,
52 false,
53 false,
54 preview,
55 0,
56 0,
57 uploadTimestamp,
58 caption,
59 blurHash,
60 null,
61 null,
62 resumableUploadSpec);
63 }
64
65 public static void retrieveAttachment(
66 SignalServiceAttachmentStream stream, OutputStream output
67 ) throws IOException {
68 InputStream input = stream.getInputStream();
69 IOUtils.copyStream(input, output);
70 }
71 }