]> nmode's Git Repositories - signal-cli/commitdiff
Retrieve group v2 avatars
authorAsamK <asamk@gmx.de>
Thu, 24 Dec 2020 16:53:23 +0000 (17:53 +0100)
committerAsamK <asamk@gmx.de>
Thu, 24 Dec 2020 16:53:23 +0000 (17:53 +0100)
Fixes #392

src/main/java/org/asamk/signal/manager/Manager.java

index b5d425d8d4b8881e63a55091139f3badfcb5cbaa..4c10a5298e47e8acdd894ad2a895f090de2b9dc7 100644 (file)
@@ -1754,6 +1754,11 @@ public class Manager implements Closeable {
             }
             if (group != null) {
                 storeProfileKeysFromMembers(group);
+                try {
+                    retrieveGroupAvatar(groupId, groupSecretParams, group.getAvatar());
+                } catch (IOException e) {
+                    System.err.println("Failed to download group avatar, ignoring ...");
+                }
             }
             groupInfoV2.setGroup(group);
             account.getGroupStore().updateGroup(groupInfoV2);
@@ -2246,6 +2251,35 @@ public class Manager implements Closeable {
         }
     }
 
+    private File retrieveGroupAvatar(
+            GroupId groupId, GroupSecretParams groupSecretParams, String cdnKey
+    ) throws IOException {
+        IOUtils.createPrivateDirectories(pathConfig.getAvatarsPath());
+        SignalServiceMessageReceiver receiver = getOrCreateMessageReceiver();
+        File outputFile = getGroupAvatarFile(groupId);
+        GroupsV2Operations.GroupOperations groupOperations = groupsV2Operations.forGroup(groupSecretParams);
+
+        File tmpFile = IOUtils.createTempFile();
+        tmpFile.deleteOnExit();
+        try (InputStream input = receiver.retrieveGroupsV2ProfileAvatar(cdnKey,
+                tmpFile,
+                ServiceConfig.AVATAR_DOWNLOAD_FAILSAFE_MAX_SIZE)) {
+            byte[] encryptedData = IOUtils.readFully(input);
+
+            byte[] decryptedData = groupOperations.decryptAvatar(encryptedData);
+            try (OutputStream output = new FileOutputStream(outputFile)) {
+                output.write(decryptedData);
+            }
+        } finally {
+            try {
+                Files.delete(tmpFile.toPath());
+            } catch (IOException e) {
+                System.err.println("Failed to delete received avatar temp file “" + tmpFile + "”: " + e.getMessage());
+            }
+        }
+        return outputFile;
+    }
+
     private File getProfileAvatarFile(SignalServiceAddress address) {
         return new File(pathConfig.getAvatarsPath(), "profile-" + address.getLegacyIdentifier());
     }