1 package org
.asamk
.signal
.manager
.util
;
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
;
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
;
17 public class AttachmentUtils
{
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
) {
25 signalServiceAttachments
.add(createAttachment(new File(attachment
)));
26 } catch (IOException e
) {
27 throw new AttachmentInvalidException(attachment
, e
);
31 return signalServiceAttachments
;
34 public static SignalServiceAttachmentStream
createAttachment(File attachmentFile
) throws IOException
{
35 final StreamDetails streamDetails
= Utils
.createStreamDetailsFromFile(attachmentFile
);
36 return createAttachment(streamDetails
, Optional
.of(attachmentFile
.getName()));
39 public static SignalServiceAttachmentStream
createAttachment(
40 StreamDetails streamDetails
, Optional
<String
> name
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(),
65 public static void retrieveAttachment(
66 SignalServiceAttachmentStream stream
, OutputStream output
67 ) throws IOException
{
68 InputStream input
= stream
.getInputStream();
69 IOUtils
.copyStream(input
, output
);