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
.internal
.push
.http
.ResumableUploadSpec
;
10 import java
.io
.FileInputStream
;
11 import java
.io
.FileNotFoundException
;
12 import java
.io
.FileOutputStream
;
13 import java
.io
.IOException
;
14 import java
.io
.InputStream
;
15 import java
.io
.OutputStream
;
16 import java
.util
.ArrayList
;
17 import java
.util
.List
;
19 public class AttachmentUtils
{
21 public static List
<SignalServiceAttachment
> getSignalServiceAttachments(List
<String
> attachments
) throws AttachmentInvalidException
{
22 List
<SignalServiceAttachment
> signalServiceAttachments
= null;
23 if (attachments
!= null) {
24 signalServiceAttachments
= new ArrayList
<>(attachments
.size());
25 for (String attachment
: attachments
) {
27 signalServiceAttachments
.add(createAttachment(new File(attachment
)));
28 } catch (IOException e
) {
29 throw new AttachmentInvalidException(attachment
, e
);
33 return signalServiceAttachments
;
36 public static SignalServiceAttachmentStream
createAttachment(File attachmentFile
) throws IOException
{
37 InputStream attachmentStream
= new FileInputStream(attachmentFile
);
38 final long attachmentSize
= attachmentFile
.length();
39 final String mime
= Utils
.getFileMimeType(attachmentFile
, "application/octet-stream");
40 // TODO mabybe add a parameter to set the voiceNote, borderless, preview, width, height and caption option
41 final long uploadTimestamp
= System
.currentTimeMillis();
42 Optional
<byte[]> preview
= Optional
.absent();
43 Optional
<String
> caption
= Optional
.absent();
44 Optional
<String
> blurHash
= Optional
.absent();
45 final Optional
<ResumableUploadSpec
> resumableUploadSpec
= Optional
.absent();
46 return new SignalServiceAttachmentStream(attachmentStream
,
49 Optional
.of(attachmentFile
.getName()),
63 public static File
retrieveAttachment(SignalServiceAttachmentStream stream
, File outputFile
) throws IOException
{
64 InputStream input
= stream
.getInputStream();
66 try (OutputStream output
= new FileOutputStream(outputFile
)) {
67 byte[] buffer
= new byte[4096];
70 while ((read
= input
.read(buffer
)) != -1) {
71 output
.write(buffer
, 0, read
);
73 } catch (FileNotFoundException e
) {