]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/AvatarStore.java
Implement editing of previous messages
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / AvatarStore.java
1 package org.asamk.signal.manager;
2
3 import org.asamk.signal.manager.groups.GroupId;
4 import org.asamk.signal.manager.storage.recipients.RecipientAddress;
5 import org.asamk.signal.manager.util.IOUtils;
6 import org.asamk.signal.manager.util.Utils;
7 import org.whispersystems.signalservice.api.util.StreamDetails;
8
9 import java.io.File;
10 import java.io.FileOutputStream;
11 import java.io.IOException;
12 import java.io.OutputStream;
13 import java.nio.file.Files;
14
15 public class AvatarStore {
16
17 private final File avatarsPath;
18
19 public AvatarStore(final File avatarsPath) {
20 this.avatarsPath = avatarsPath;
21 }
22
23 public StreamDetails retrieveContactAvatar(RecipientAddress address) throws IOException {
24 return retrieveAvatar(getContactAvatarFile(address));
25 }
26
27 public StreamDetails retrieveProfileAvatar(RecipientAddress address) throws IOException {
28 return retrieveAvatar(getProfileAvatarFile(address));
29 }
30
31 public StreamDetails retrieveGroupAvatar(GroupId groupId) throws IOException {
32 final var groupAvatarFile = getGroupAvatarFile(groupId);
33 return retrieveAvatar(groupAvatarFile);
34 }
35
36 public void storeContactAvatar(RecipientAddress address, AvatarStorer storer) throws IOException {
37 storeAvatar(getContactAvatarFile(address), storer);
38 }
39
40 public void storeProfileAvatar(RecipientAddress address, AvatarStorer storer) throws IOException {
41 storeAvatar(getProfileAvatarFile(address), storer);
42 }
43
44 public void storeGroupAvatar(GroupId groupId, AvatarStorer storer) throws IOException {
45 storeAvatar(getGroupAvatarFile(groupId), storer);
46 }
47
48 public void deleteProfileAvatar(RecipientAddress address) throws IOException {
49 deleteAvatar(getProfileAvatarFile(address));
50 }
51
52 public void deleteGroupAvatar(GroupId groupId) throws IOException {
53 deleteAvatar(getGroupAvatarFile(groupId));
54 }
55
56 private StreamDetails retrieveAvatar(final File avatarFile) throws IOException {
57 if (!avatarFile.exists()) {
58 return null;
59 }
60 return Utils.createStreamDetailsFromFile(avatarFile);
61 }
62
63 private void storeAvatar(final File avatarFile, final AvatarStorer storer) throws IOException {
64 createAvatarsDir();
65 try (OutputStream output = new FileOutputStream(avatarFile)) {
66 storer.store(output);
67 }
68 }
69
70 private void deleteAvatar(final File avatarFile) throws IOException {
71 if (avatarFile.exists()) {
72 Files.delete(avatarFile.toPath());
73 }
74 }
75
76 private File getGroupAvatarFile(GroupId groupId) {
77 return new File(avatarsPath, "group-" + groupId.toBase64().replace("/", "_"));
78 }
79
80 private File getContactAvatarFile(RecipientAddress address) {
81 return new File(avatarsPath, "contact-" + address.getLegacyIdentifier());
82 }
83
84 private File getProfileAvatarFile(RecipientAddress address) {
85 return new File(avatarsPath, "profile-" + address.getLegacyIdentifier());
86 }
87
88 private void createAvatarsDir() throws IOException {
89 IOUtils.createPrivateDirectories(avatarsPath);
90 }
91
92 @FunctionalInterface
93 public interface AvatarStorer {
94
95 void store(OutputStream outputStream) throws IOException;
96 }
97 }