X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/9bb935b11f8e632f48140bc6cf53c6867cf10cbd..96d316b1dd1fec2959ae2ba1f27b68ceb355d7f4:/src/main/java/org/asamk/signal/manager/AttachmentStore.java diff --git a/src/main/java/org/asamk/signal/manager/AttachmentStore.java b/src/main/java/org/asamk/signal/manager/AttachmentStore.java new file mode 100644 index 00000000..f983a90b --- /dev/null +++ b/src/main/java/org/asamk/signal/manager/AttachmentStore.java @@ -0,0 +1,55 @@ +package org.asamk.signal.manager; + +import org.asamk.signal.manager.util.IOUtils; +import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentRemoteId; + +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; + +public class AttachmentStore { + + private final File attachmentsPath; + + public AttachmentStore(final File attachmentsPath) { + this.attachmentsPath = attachmentsPath; + } + + public void storeAttachmentPreview( + final SignalServiceAttachmentRemoteId attachmentId, final AttachmentStorer storer + ) throws IOException { + storeAttachment(getAttachmentPreviewFile(attachmentId), storer); + } + + public void storeAttachment( + final SignalServiceAttachmentRemoteId attachmentId, final AttachmentStorer storer + ) throws IOException { + storeAttachment(getAttachmentFile(attachmentId), storer); + } + + private void storeAttachment(final File attachmentFile, final AttachmentStorer storer) throws IOException { + createAttachmentsDir(); + try (OutputStream output = new FileOutputStream(attachmentFile)) { + storer.store(output); + } + } + + private File getAttachmentPreviewFile(SignalServiceAttachmentRemoteId attachmentId) { + return new File(attachmentsPath, attachmentId.toString() + ".preview"); + } + + public File getAttachmentFile(SignalServiceAttachmentRemoteId attachmentId) { + return new File(attachmentsPath, attachmentId.toString()); + } + + private void createAttachmentsDir() throws IOException { + IOUtils.createPrivateDirectories(attachmentsPath); + } + + @FunctionalInterface + public interface AttachmentStorer { + + void store(OutputStream outputStream) throws IOException; + } +}