]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/manager/AttachmentStore.java
Convert gradle scripts to kotlin
[signal-cli] / src / main / java / org / asamk / signal / manager / AttachmentStore.java
1 package org.asamk.signal.manager;
2
3 import org.asamk.signal.manager.util.IOUtils;
4 import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentRemoteId;
5
6 import java.io.File;
7 import java.io.FileOutputStream;
8 import java.io.IOException;
9 import java.io.OutputStream;
10
11 public class AttachmentStore {
12
13 private final File attachmentsPath;
14
15 public AttachmentStore(final File attachmentsPath) {
16 this.attachmentsPath = attachmentsPath;
17 }
18
19 public void storeAttachmentPreview(
20 final SignalServiceAttachmentRemoteId attachmentId, final AttachmentStorer storer
21 ) throws IOException {
22 storeAttachment(getAttachmentPreviewFile(attachmentId), storer);
23 }
24
25 public void storeAttachment(
26 final SignalServiceAttachmentRemoteId attachmentId, final AttachmentStorer storer
27 ) throws IOException {
28 storeAttachment(getAttachmentFile(attachmentId), storer);
29 }
30
31 private void storeAttachment(final File attachmentFile, final AttachmentStorer storer) throws IOException {
32 createAttachmentsDir();
33 try (OutputStream output = new FileOutputStream(attachmentFile)) {
34 storer.store(output);
35 }
36 }
37
38 private File getAttachmentPreviewFile(SignalServiceAttachmentRemoteId attachmentId) {
39 return new File(attachmentsPath, attachmentId.toString() + ".preview");
40 }
41
42 public File getAttachmentFile(SignalServiceAttachmentRemoteId attachmentId) {
43 return new File(attachmentsPath, attachmentId.toString());
44 }
45
46 private void createAttachmentsDir() throws IOException {
47 IOUtils.createPrivateDirectories(attachmentsPath);
48 }
49
50 @FunctionalInterface
51 public interface AttachmentStorer {
52
53 void store(OutputStream outputStream) throws IOException;
54 }
55 }