1 package org
.asamk
.signal
.manager
;
3 import org
.asamk
.signal
.manager
.groups
.GroupId
;
4 import org
.asamk
.signal
.manager
.util
.IOUtils
;
5 import org
.asamk
.signal
.manager
.util
.Utils
;
6 import org
.whispersystems
.signalservice
.api
.push
.SignalServiceAddress
;
7 import org
.whispersystems
.signalservice
.api
.util
.StreamDetails
;
10 import java
.io
.FileOutputStream
;
11 import java
.io
.IOException
;
12 import java
.io
.OutputStream
;
13 import java
.nio
.file
.Files
;
15 public class AvatarStore
{
17 private final File avatarsPath
;
19 public AvatarStore(final File avatarsPath
) {
20 this.avatarsPath
= avatarsPath
;
23 public StreamDetails
retrieveContactAvatar(SignalServiceAddress address
) throws IOException
{
24 return retrieveAvatar(getContactAvatarFile(address
));
27 public StreamDetails
retrieveProfileAvatar(SignalServiceAddress address
) throws IOException
{
28 return retrieveAvatar(getProfileAvatarFile(address
));
31 public StreamDetails
retrieveGroupAvatar(GroupId groupId
) throws IOException
{
32 final File groupAvatarFile
= getGroupAvatarFile(groupId
);
33 return retrieveAvatar(groupAvatarFile
);
36 public void storeContactAvatar(SignalServiceAddress address
, AvatarStorer storer
) throws IOException
{
37 storeAvatar(getContactAvatarFile(address
), storer
);
40 public void storeProfileAvatar(SignalServiceAddress address
, AvatarStorer storer
) throws IOException
{
41 storeAvatar(getProfileAvatarFile(address
), storer
);
44 public void storeGroupAvatar(GroupId groupId
, AvatarStorer storer
) throws IOException
{
45 storeAvatar(getGroupAvatarFile(groupId
), storer
);
48 public void deleteProfileAvatar(SignalServiceAddress address
) throws IOException
{
49 deleteAvatar(getProfileAvatarFile(address
));
52 private StreamDetails
retrieveAvatar(final File avatarFile
) throws IOException
{
53 if (!avatarFile
.exists()) {
56 return Utils
.createStreamDetailsFromFile(avatarFile
);
59 private void storeAvatar(final File avatarFile
, final AvatarStorer storer
) throws IOException
{
61 try (OutputStream output
= new FileOutputStream(avatarFile
)) {
66 private void deleteAvatar(final File avatarFile
) throws IOException
{
67 if (avatarFile
.exists()) {
68 Files
.delete(avatarFile
.toPath());
72 private File
getGroupAvatarFile(GroupId groupId
) {
73 return new File(avatarsPath
, "group-" + groupId
.toBase64().replace("/", "_"));
76 private File
getContactAvatarFile(SignalServiceAddress address
) {
77 return new File(avatarsPath
, "contact-" + address
.getLegacyIdentifier());
80 private File
getProfileAvatarFile(SignalServiceAddress address
) {
81 return new File(avatarsPath
, "profile-" + address
.getLegacyIdentifier());
84 private void createAvatarsDir() throws IOException
{
85 IOUtils
.createPrivateDirectories(avatarsPath
);
89 public interface AvatarStorer
{
91 void store(OutputStream outputStream
) throws IOException
;