Retrieve group v2 avatars

Fixes #392
This commit is contained in:
AsamK 2020-12-24 17:53:23 +01:00
parent 9942d967a4
commit e11e020886

View file

@ -1754,6 +1754,11 @@ public class Manager implements Closeable {
} }
if (group != null) { if (group != null) {
storeProfileKeysFromMembers(group); storeProfileKeysFromMembers(group);
try {
retrieveGroupAvatar(groupId, groupSecretParams, group.getAvatar());
} catch (IOException e) {
System.err.println("Failed to download group avatar, ignoring ...");
}
} }
groupInfoV2.setGroup(group); groupInfoV2.setGroup(group);
account.getGroupStore().updateGroup(groupInfoV2); 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) { private File getProfileAvatarFile(SignalServiceAddress address) {
return new File(pathConfig.getAvatarsPath(), "profile-" + address.getLegacyIdentifier()); return new File(pathConfig.getAvatarsPath(), "profile-" + address.getLegacyIdentifier());
} }