1 package org
.asamk
.signal
.manager
;
3 import org
.asamk
.signal
.manager
.util
.IOUtils
;
4 import org
.whispersystems
.signalservice
.api
.messages
.SignalServiceAttachmentRemoteId
;
7 import java
.io
.FileOutputStream
;
8 import java
.io
.IOException
;
9 import java
.io
.OutputStream
;
11 public class AttachmentStore
{
13 private final File attachmentsPath
;
15 public AttachmentStore(final File attachmentsPath
) {
16 this.attachmentsPath
= attachmentsPath
;
19 public void storeAttachmentPreview(
20 final SignalServiceAttachmentRemoteId attachmentId
, final AttachmentStorer storer
21 ) throws IOException
{
22 storeAttachment(getAttachmentPreviewFile(attachmentId
), storer
);
25 public void storeAttachment(
26 final SignalServiceAttachmentRemoteId attachmentId
, final AttachmentStorer storer
27 ) throws IOException
{
28 storeAttachment(getAttachmentFile(attachmentId
), storer
);
31 private void storeAttachment(final File attachmentFile
, final AttachmentStorer storer
) throws IOException
{
32 createAttachmentsDir();
33 try (OutputStream output
= new FileOutputStream(attachmentFile
)) {
38 private File
getAttachmentPreviewFile(SignalServiceAttachmentRemoteId attachmentId
) {
39 return new File(attachmentsPath
, attachmentId
.toString() + ".preview");
42 public File
getAttachmentFile(SignalServiceAttachmentRemoteId attachmentId
) {
43 return new File(attachmentsPath
, attachmentId
.toString());
46 private void createAttachmentsDir() throws IOException
{
47 IOUtils
.createPrivateDirectories(attachmentsPath
);
51 public interface AttachmentStorer
{
53 void store(OutputStream outputStream
) throws IOException
;