Decrypt profile picture after downloading.

pull/69/head
Mikunj 6 years ago
parent 2e83a2bb85
commit 5f7ceeed6a

@ -1056,10 +1056,12 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error);
} }
NSString *_Nullable avatarUrlPathAtStart = userProfile.avatarUrlPath; NSString *_Nullable avatarUrlPathAtStart = userProfile.avatarUrlPath;
if (userProfile.avatarUrlPath.length < 1) { if (userProfile.profileKey.keyData.length < 1 || userProfile.avatarUrlPath.length < 1) {
return; return;
} }
OWSAES256Key *profileKeyAtStart = userProfile.profileKey;
NSString *fileName = [self generateAvatarFilename]; NSString *fileName = [self generateAvatarFilename];
NSString *filePath = [OWSUserProfile profileAvatarFilepathWithFilename:fileName]; NSString *filePath = [OWSUserProfile profileAvatarFilepathWithFilename:fileName];
@ -1077,7 +1079,6 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error);
NSString *tempDirectory = OWSTemporaryDirectory(); NSString *tempDirectory = OWSTemporaryDirectory();
NSString *tempFilePath = [tempDirectory stringByAppendingPathComponent:fileName]; NSString *tempFilePath = [tempDirectory stringByAppendingPathComponent:fileName];
NSString *profilePictureURL = userProfile.avatarUrlPath; NSString *profilePictureURL = userProfile.avatarUrlPath;
NSError *serializationError; NSError *serializationError;
NSMutableURLRequest *request = NSMutableURLRequest *request =
@ -1091,7 +1092,6 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error);
} }
NSURLSession* session = [NSURLSession sharedSession]; NSURLSession* session = [NSURLSession sharedSession];
NSURLSessionTask* downloadTask = [session downloadTaskWithRequest:request completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) { NSURLSessionTask* downloadTask = [session downloadTaskWithRequest:request completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
@synchronized(self.currentAvatarDownloads) @synchronized(self.currentAvatarDownloads)
@ -1105,32 +1105,68 @@ typedef void (^ProfileManagerFailureBlock)(NSError *error);
} }
NSFileManager *fileManager = [NSFileManager defaultManager]; NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *fileURL = [NSURL fileURLWithPath:filePath]; NSURL *tempFileUrl = [NSURL fileURLWithPath:tempFilePath];
NSError *moveError; NSError *moveError;
if (![fileManager moveItemAtURL:location toURL:fileURL error:&moveError]) { if (![fileManager moveItemAtURL:location toURL:tempFileUrl error:&moveError]) {
OWSLogError(@"MoveItemAtURL for avatar failed: %@", moveError); OWSLogError(@"MoveItemAtURL for avatar failed: %@", moveError);
return; return;
} }
UIImage *image = [UIImage imageWithContentsOfFile:[fileURL path]]; NSData *_Nullable encryptedData = (error ? nil : [NSData dataWithContentsOfFile:tempFilePath]);
if (image) { NSData *_Nullable decryptedData = [self decryptProfileData:encryptedData profileKey:profileKeyAtStart];
dispatch_async(dispatch_get_main_queue(), ^{ UIImage *_Nullable image = nil;
if (decryptedData) {
BOOL success = [decryptedData writeToFile:filePath atomically:YES];
if (success) {
image = [UIImage imageWithContentsOfFile:filePath];
}
}
[self updateProfileAvatarCache:image filename:fileName]; OWSUserProfile *latestUserProfile = [OWSUserProfile getOrBuildUserProfileForRecipientId:userProfile.recipientId dbConnection:self.dbConnection];
OWSUserProfile *latestUserProfile = if (latestUserProfile.profileKey.keyData.length < 1
[OWSUserProfile getOrBuildUserProfileForRecipientId:userProfile.recipientId || ![latestUserProfile.profileKey isEqual:userProfile.profileKey]) {
dbConnection:self.dbConnection]; OWSLogWarn(@"Ignoring avatar download for obsolete user profile.");
} else if (![avatarUrlPathAtStart isEqualToString:latestUserProfile.avatarUrlPath]) {
OWSLogInfo(@"avatar url has changed during download");
if (latestUserProfile.avatarUrlPath.length > 0) {
[self downloadAvatarForUserProfile:latestUserProfile];
}
} else if (error) {
if ([response isKindOfClass:NSHTTPURLResponse.class]
&& ((NSHTTPURLResponse *)response).statusCode == 403) {
OWSLogInfo(@"no avatar for: %@", userProfile.recipientId);
} else {
OWSLogError(@"avatar download for %@ failed with error: %@", userProfile.recipientId, error);
}
} else if (!encryptedData) {
OWSLogError(@"avatar encrypted data for %@ could not be read.", userProfile.recipientId);
} else if (!decryptedData) {
OWSLogError(@"avatar data for %@ could not be decrypted.", userProfile.recipientId);
} else if (!image) {
OWSLogError(@"avatar image for %@ could not be loaded with error: %@", userProfile.recipientId, error);
} else {
[self updateProfileAvatarCache:image filename:fileName];
[latestUserProfile updateWithAvatarFileName:fileName dbConnection:self.dbConnection completion:^{ [latestUserProfile updateWithAvatarFileName:fileName dbConnection:self.dbConnection completion:^{
[[NSNotificationCenter defaultCenter] [[NSNotificationCenter defaultCenter]
postNotificationNameAsync:OWSContactsManagerSignalAccountsDidChangeNotification postNotificationNameAsync:OWSContactsManagerSignalAccountsDidChangeNotification
object:nil]; object:nil];
}]; }];
});
} }
// If we're updating the profile that corresponds to our local number,
// update the local profile as well.
if (userProfile.address.isLocalAddress) {
OWSUserProfile *localUserProfile = self.localUserProfile;
OWSAssertDebug(localUserProfile);
[localUserProfile updateWithAvatarFileName:fileName dbConnection:self.dbConnection completion:nil];
[self updateProfileAvatarCache:image filename:fileName];
}
OWSAssertDebug(backgroundTask);
backgroundTask = nil;
}]; }];
[downloadTask resume]; [downloadTask resume];

Loading…
Cancel
Save