Respond to CR.

pull/1/head
Matthew Chen 7 years ago
parent 0f4e846ed7
commit d6cb07cc4a

@ -302,7 +302,7 @@ static NSTimeInterval launchStartedAt;
error = [OWSPrimaryStorage migrateToSharedData]; error = [OWSPrimaryStorage migrateToSharedData];
} }
if (!error) { if (!error) {
error = [OWSProfileManager migrateToSharedData]; error = [OWSUserProfile migrateToSharedData];
} }
if (!error) { if (!error) {
error = [TSAttachmentStream migrateToSharedData]; error = [TSAttachmentStream migrateToSharedData];

@ -234,7 +234,7 @@ NS_ASSUME_NONNULL_BEGIN
[DDLog flushLog]; [DDLog flushLog];
[OWSStorage resetAllStorage]; [OWSStorage resetAllStorage];
[[OWSProfileManager sharedManager] resetProfileStorage]; [OWSUserProfile resetProfileStorage];
[Environment.preferences clear]; [Environment.preferences clear];
[self clearAllNotifications]; [self clearAllNotifications];

@ -22,10 +22,6 @@ extern const NSUInteger kOWSProfileManager_MaxAvatarDiameter;
+ (instancetype)sharedManager; + (instancetype)sharedManager;
- (void)resetProfileStorage;
+ (nullable NSError *)migrateToSharedData;
#pragma mark - Local Profile #pragma mark - Local Profile
// These two methods should only be called from the main thread. // These two methods should only be called from the main thread.

@ -224,9 +224,6 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
OWSUserProfile *userProfile = self.localUserProfile; OWSUserProfile *userProfile = self.localUserProfile;
OWSAssert(userProfile); OWSAssert(userProfile);
NSString *_Nullable oldAvatarFilePath =
[self profileAvatarFilepathWithFilename:userProfile.avatarFileName];
[userProfile updateWithProfileName:profileName [userProfile updateWithProfileName:profileName
avatarUrlPath:avatarUrlPath avatarUrlPath:avatarUrlPath
avatarFileName:avatarFileName avatarFileName:avatarFileName
@ -236,8 +233,6 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
[self updateProfileAvatarCache:avatarImage filename:avatarFileName]; [self updateProfileAvatarCache:avatarImage filename:avatarFileName];
} }
[OWSFileSystem deleteFileIfExists:oldAvatarFilePath];
successBlock(); successBlock();
}]; }];
} }
@ -309,8 +304,7 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
OWSAssert(data); OWSAssert(data);
if (data) { if (data) {
NSString *fileName = [[NSUUID UUID].UUIDString stringByAppendingPathExtension:@"jpg"]; NSString *fileName = [[NSUUID UUID].UUIDString stringByAppendingPathExtension:@"jpg"];
NSString *_Nullable filePath = [self profileAvatarFilepathWithFilename:fileName]; NSString *filePath = [OWSUserProfile profileAvatarFilepathWithFilename:fileName];
OWSAssert(filePath);
BOOL success = [data writeToFile:filePath atomically:YES]; BOOL success = [data writeToFile:filePath atomically:YES];
OWSAssert(success); OWSAssert(success);
if (success) { if (success) {
@ -363,13 +357,7 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
// the profile avatar blank, etc. // the profile avatar blank, etc.
void (^clearLocalAvatar)(void) = ^{ void (^clearLocalAvatar)(void) = ^{
OWSUserProfile *userProfile = self.localUserProfile; OWSUserProfile *userProfile = self.localUserProfile;
NSString *_Nullable oldAvatarFilePath = [self profileAvatarFilepathWithFilename:userProfile.avatarFileName]; [userProfile updateWithAvatarUrlPath:nil avatarFileName:nil dbConnection:self.dbConnection completion:nil];
[userProfile updateWithAvatarUrlPath:nil
avatarFileName:nil
dbConnection:self.dbConnection
completion:^{
[OWSFileSystem deleteFileIfExists:oldAvatarFilePath];
}];
}; };
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
@ -561,12 +549,7 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
- (void)regenerateLocalProfile - (void)regenerateLocalProfile
{ {
OWSUserProfile *userProfile = self.localUserProfile; OWSUserProfile *userProfile = self.localUserProfile;
NSString *_Nullable oldAvatarFilePath = [self profileAvatarFilepathWithFilename:userProfile.avatarFileName]; [userProfile clearWithProfileKey:[OWSAES256Key generateRandomKey] dbConnection:self.dbConnection completion:nil];
[userProfile clearWithProfileKey:[OWSAES256Key generateRandomKey]
dbConnection:self.dbConnection
completion:^{
[OWSFileSystem deleteFileIfExists:oldAvatarFilePath];
}];
} }
- (void)addUserToProfileWhitelist:(NSString *)recipientId - (void)addUserToProfileWhitelist:(NSString *)recipientId
@ -744,12 +727,9 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
return; return;
} }
NSString *_Nullable oldAvatarFilePath = [self profileAvatarFilepathWithFilename:userProfile.avatarFileName];
[userProfile clearWithProfileKey:profileKey [userProfile clearWithProfileKey:profileKey
dbConnection:self.dbConnection dbConnection:self.dbConnection
completion:^{ completion:^{
[OWSFileSystem deleteFileIfExists:oldAvatarFilePath];
dispatch_async(dispatch_get_main_queue(), ^(void) { dispatch_async(dispatch_get_main_queue(), ^(void) {
[ProfileFetcherJob runWithRecipientId:recipientId [ProfileFetcherJob runWithRecipientId:recipientId
networkManager:self.networkManager networkManager:self.networkManager
@ -837,8 +817,7 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
OWSAES256Key *profileKeyAtStart = userProfile.profileKey; OWSAES256Key *profileKeyAtStart = userProfile.profileKey;
NSString *fileName = [[NSUUID UUID].UUIDString stringByAppendingPathExtension:@"jpg"]; NSString *fileName = [[NSUUID UUID].UUIDString stringByAppendingPathExtension:@"jpg"];
NSString *_Nullable filePath = [self profileAvatarFilepathWithFilename:fileName]; NSString *filePath = [OWSUserProfile profileAvatarFilepathWithFilename:fileName];
OWSAssert(filePath);
@synchronized(self.currentAvatarDownloads) @synchronized(self.currentAvatarDownloads)
{ {
@ -895,13 +874,7 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
} else { } else {
[self updateProfileAvatarCache:image filename:fileName]; [self updateProfileAvatarCache:image filename:fileName];
NSString *_Nullable oldAvatarFilePath = [latestUserProfile updateWithAvatarFileName:fileName dbConnection:self.dbConnection completion:nil];
[self profileAvatarFilepathWithFilename:latestUserProfile.avatarFileName];
[latestUserProfile updateWithAvatarFileName:fileName
dbConnection:self.dbConnection
completion:^{
[OWSFileSystem deleteFileIfExists:oldAvatarFilePath];
}];
} }
// If we're updating the profile that corresponds to our local number, // If we're updating the profile that corresponds to our local number,
@ -911,13 +884,7 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
OWSUserProfile *localUserProfile = self.localUserProfile; OWSUserProfile *localUserProfile = self.localUserProfile;
OWSAssert(localUserProfile); OWSAssert(localUserProfile);
NSString *_Nullable oldAvatarFilePath = [localUserProfile updateWithAvatarFileName:fileName dbConnection:self.dbConnection completion:nil];
[self profileAvatarFilepathWithFilename:localUserProfile.avatarFileName];
[localUserProfile updateWithAvatarFileName:fileName
dbConnection:self.dbConnection
completion:^{
[OWSFileSystem deleteFileIfExists:oldAvatarFilePath];
}];
[self updateProfileAvatarCache:image filename:fileName]; [self updateProfileAvatarCache:image filename:fileName];
} }
@ -968,15 +935,10 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
NSString *_Nullable profileName = NSString *_Nullable profileName =
[self decryptProfileNameData:profileNameEncrypted profileKey:userProfile.profileKey]; [self decryptProfileNameData:profileNameEncrypted profileKey:userProfile.profileKey];
{ [userProfile updateWithProfileName:profileName
NSString *_Nullable oldAvatarFilePath = [self profileAvatarFilepathWithFilename:userProfile.avatarFileName]; avatarUrlPath:avatarUrlPath
[userProfile updateWithProfileName:profileName dbConnection:self.dbConnection
avatarUrlPath:avatarUrlPath completion:nil];
dbConnection:self.dbConnection
completion:^{
[OWSFileSystem deleteFileIfExists:oldAvatarFilePath];
}];
}
// If we're updating the profile that corresponds to our local number, // If we're updating the profile that corresponds to our local number,
// update the local profile as well. // update the local profile as well.
@ -985,15 +947,10 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
OWSUserProfile *localUserProfile = self.localUserProfile; OWSUserProfile *localUserProfile = self.localUserProfile;
OWSAssert(localUserProfile); OWSAssert(localUserProfile);
NSString *_Nullable oldAvatarFilePath =
[self profileAvatarFilepathWithFilename:localUserProfile.avatarFileName];
[localUserProfile updateWithProfileName:profileName [localUserProfile updateWithProfileName:profileName
avatarUrlPath:avatarUrlPath avatarUrlPath:avatarUrlPath
dbConnection:self.dbConnection dbConnection:self.dbConnection
completion:^{ completion:nil];
[OWSFileSystem deleteFileIfExists:oldAvatarFilePath];
}];
} }
// Whenever we change avatarUrlPath, OWSUserProfile clears avatarFileName. // Whenever we change avatarUrlPath, OWSUserProfile clears avatarFileName.
@ -1114,22 +1071,11 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
#pragma mark - Avatar Disk Cache #pragma mark - Avatar Disk Cache
- (nullable NSString *)profileAvatarFilepathWithFilename:(nullable NSString *)filename
{
if (!filename) {
return nil;
}
OWSAssert(filename.length > 0);
return [self.profileAvatarsDirPath stringByAppendingPathComponent:filename];
}
- (nullable NSData *)loadProfileDataWithFilename:(NSString *)filename - (nullable NSData *)loadProfileDataWithFilename:(NSString *)filename
{ {
OWSAssert(filename.length > 0); OWSAssert(filename.length > 0);
NSString *_Nullable filePath = [self profileAvatarFilepathWithFilename:filename]; NSString *filePath = [OWSUserProfile profileAvatarFilepathWithFilename:filename];
OWSAssert(filePath);
return [NSData dataWithContentsOfFile:filePath]; return [NSData dataWithContentsOfFile:filePath];
} }
@ -1172,49 +1118,6 @@ const NSUInteger kOWSProfileManager_MaxAvatarDiameter = 640;
} }
} }
+ (NSString *)legacyProfileAvatarsDirPath
{
return [[OWSFileSystem appDocumentDirectoryPath] stringByAppendingPathComponent:@"ProfileAvatars"];
}
+ (NSString *)sharedDataProfileAvatarsDirPath
{
return [[OWSFileSystem appSharedDataDirectoryPath] stringByAppendingPathComponent:@"ProfileAvatars"];
}
+ (nullable NSError *)migrateToSharedData
{
DDLogInfo(@"%@ %s", self.logTag, __PRETTY_FUNCTION__);
return [OWSFileSystem moveAppFilePath:self.legacyProfileAvatarsDirPath
sharedDataFilePath:self.sharedDataProfileAvatarsDirPath];
}
- (NSString *)profileAvatarsDirPath
{
static NSString *profileAvatarsDirPath = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
profileAvatarsDirPath = OWSProfileManager.sharedDataProfileAvatarsDirPath;
[OWSFileSystem ensureDirectoryExists:profileAvatarsDirPath];
});
return profileAvatarsDirPath;
}
// TODO: We may want to clean up this directory in the "orphan cleanup" logic.
- (void)resetProfileStorage
{
OWSAssertIsOnMainThread();
NSError *error;
[[NSFileManager defaultManager] removeItemAtPath:[self profileAvatarsDirPath] error:&error];
if (error) {
DDLogError(@"Failed to delete database: %@", error.description);
}
}
#pragma mark - User Interface #pragma mark - User Interface
- (void)presentAddThreadToProfileWhitelist:(TSThread *)thread - (void)presentAddThreadToProfileWhitelist:(TSThread *)thread

@ -65,6 +65,13 @@ extern NSString *const kLocalProfileUniqueId;
dbConnection:(YapDatabaseConnection *)dbConnection dbConnection:(YapDatabaseConnection *)dbConnection
completion:(nullable OWSUserProfileCompletion)completion; completion:(nullable OWSUserProfileCompletion)completion;
#pragma mark - Profile Avatars Directory
+ (NSString *)profileAvatarFilepathWithFilename:(NSString *)filename;
+ (nullable NSError *)migrateToSharedData;
+ (NSString *)profileAvatarsDirPath;
+ (void)resetProfileStorage;
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

@ -8,6 +8,7 @@
#import <SignalServiceKit/Cryptography.h> #import <SignalServiceKit/Cryptography.h>
#import <SignalServiceKit/NSData+OWS.h> #import <SignalServiceKit/NSData+OWS.h>
#import <SignalServiceKit/NSNotificationCenter+OWS.h> #import <SignalServiceKit/NSNotificationCenter+OWS.h>
#import <SignalServiceKit/OWSFileSystem.h>
#import <SignalServiceKit/TSAccountManager.h> #import <SignalServiceKit/TSAccountManager.h>
#import <YapDatabase/YapDatabaseConnection.h> #import <YapDatabase/YapDatabaseConnection.h>
#import <YapDatabase/YapDatabaseTransaction.h> #import <YapDatabase/YapDatabaseTransaction.h>
@ -37,6 +38,7 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
@implementation OWSUserProfile @implementation OWSUserProfile
@synthesize avatarUrlPath = _avatarUrlPath; @synthesize avatarUrlPath = _avatarUrlPath;
@synthesize avatarFileName = _avatarFileName;
@synthesize profileName = _profileName; @synthesize profileName = _profileName;
+ (NSString *)collection + (NSString *)collection
@ -113,11 +115,38 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
if (didChange) { if (didChange) {
// If the avatarURL changed, the avatarFileName can't be valid. // If the avatarURL changed, the avatarFileName can't be valid.
// Clear it. // Clear it.
self.avatarFileName = nil; self.avatarFileName = nil;
} }
} }
} }
- (nullable NSString *)avatarFileName
{
@synchronized(self) {
return _avatarFileName;
}
}
- (void)setAvatarFileName:(nullable NSString *)avatarFileName
{
@synchronized(self) {
BOOL didChange = ![NSObject isNullableObject:_avatarFileName equalTo:avatarFileName];
if (!didChange) {
return;
}
if (_avatarFileName) {
NSString *oldAvatarFilePath = [OWSUserProfile profileAvatarFilepathWithFilename:_avatarFileName];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[OWSFileSystem deleteFileIfExists:oldAvatarFilePath];
});
}
_avatarFileName = avatarFileName;
}
}
#pragma mark - Update With... Methods #pragma mark - Update With... Methods
// Similar in spirit to [TSYapDatabaseObject applyChangeToSelfAndLatestCopy], // Similar in spirit to [TSYapDatabaseObject applyChangeToSelfAndLatestCopy],
@ -356,6 +385,58 @@ NSString *const kLocalProfileUniqueId = @"kLocalProfileUniqueId";
} }
} }
#pragma mark - Profile Avatars Directory
+ (NSString *)profileAvatarFilepathWithFilename:(NSString *)filename
{
OWSAssert(filename.length > 0);
return [self.profileAvatarsDirPath stringByAppendingPathComponent:filename];
}
+ (NSString *)legacyProfileAvatarsDirPath
{
return [[OWSFileSystem appDocumentDirectoryPath] stringByAppendingPathComponent:@"ProfileAvatars"];
}
+ (NSString *)sharedDataProfileAvatarsDirPath
{
return [[OWSFileSystem appSharedDataDirectoryPath] stringByAppendingPathComponent:@"ProfileAvatars"];
}
+ (nullable NSError *)migrateToSharedData
{
DDLogInfo(@"%@ %s", self.logTag, __PRETTY_FUNCTION__);
return [OWSFileSystem moveAppFilePath:self.legacyProfileAvatarsDirPath
sharedDataFilePath:self.sharedDataProfileAvatarsDirPath];
}
+ (NSString *)profileAvatarsDirPath
{
static NSString *profileAvatarsDirPath = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
profileAvatarsDirPath = self.sharedDataProfileAvatarsDirPath;
[OWSFileSystem ensureDirectoryExists:profileAvatarsDirPath];
});
return profileAvatarsDirPath;
}
// TODO: We may want to clean up this directory in the "orphan cleanup" logic.
+ (void)resetProfileStorage
{
OWSAssertIsOnMainThread();
NSError *error;
[[NSFileManager defaultManager] removeItemAtPath:[self profileAvatarsDirPath] error:&error];
if (error) {
DDLogError(@"Failed to delete database: %@", error.description);
}
}
@end @end
NS_ASSUME_NONNULL_END NS_ASSUME_NONNULL_END

@ -32,7 +32,7 @@ NS_ASSUME_NONNULL_BEGIN
+ (BOOL)deleteFile:(NSString *)filePath; + (BOOL)deleteFile:(NSString *)filePath;
+ (BOOL)deleteFileIfExists:(nullable NSString *)filePath; + (BOOL)deleteFileIfExists:(NSString *)filePath;
+ (NSArray<NSString *> *_Nullable)allFilesInDirectoryRecursive:(NSString *)dirPath error:(NSError **)error; + (NSArray<NSString *> *_Nullable)allFilesInDirectoryRecursive:(NSString *)dirPath error:(NSError **)error;

@ -253,11 +253,8 @@ NS_ASSUME_NONNULL_BEGIN
return YES; return YES;
} }
+ (BOOL)deleteFileIfExists:(nullable NSString *)filePath + (BOOL)deleteFileIfExists:(NSString *)filePath
{ {
if (!filePath) {
return YES;
}
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) { if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
return YES; return YES;
} }

Loading…
Cancel
Save