Lazy restore attachments.

pull/1/head
Matthew Chen 7 years ago
parent 1dced463cb
commit cb8ee3536a

@ -490,7 +490,7 @@ NS_ASSUME_NONNULL_BEGIN
{ {
NSMutableArray<NSString *> *recordNames = [NSMutableArray new]; NSMutableArray<NSString *> *recordNames = [NSMutableArray new];
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { [self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
id ext = [transaction ext:TSMessageDatabaseViewExtensionName]; id ext = [transaction ext:TSLazyRestoreAttachmentsDatabaseViewExtensionName];
if (!ext) { if (!ext) {
OWSProdLogAndFail(@"%@ Could not load database view.", self.logTag); OWSProdLogAndFail(@"%@ Could not load database view.", self.logTag);
return; return;
@ -524,7 +524,7 @@ NS_ASSUME_NONNULL_BEGIN
{ {
NSMutableArray<NSString *> *attachmentIds = [NSMutableArray new]; NSMutableArray<NSString *> *attachmentIds = [NSMutableArray new];
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) { [self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
id ext = [transaction ext:TSMessageDatabaseViewExtensionName]; id ext = [transaction ext:TSLazyRestoreAttachmentsDatabaseViewExtensionName];
if (!ext) { if (!ext) {
OWSProdLogAndFail(@"%@ Could not load database view.", self.logTag); OWSProdLogAndFail(@"%@ Could not load database view.", self.logTag);
return; return;
@ -570,10 +570,7 @@ NS_ASSUME_NONNULL_BEGIN
// will leverage successful file downloads from previous attempts. // will leverage successful file downloads from previous attempts.
// //
// TODO: This will also require imports using a predictable jobTempDirPath. // TODO: This will also require imports using a predictable jobTempDirPath.
NSString *_Nullable tempFilePath = [backupIO createTempFile]; NSString *tempFilePath = [backupIO generateTempFilePath];
if (!tempFilePath) {
return completion(NO);
}
[OWSBackupAPI downloadFileFromCloudWithRecordName:lazyRestoreFragment.recordName [OWSBackupAPI downloadFileFromCloudWithRecordName:lazyRestoreFragment.recordName
toFileUrl:[NSURL fileURLWithPath:tempFilePath] toFileUrl:[NSURL fileURLWithPath:tempFilePath]
@ -611,10 +608,7 @@ NS_ASSUME_NONNULL_BEGIN
return completion(NO); return completion(NO);
} }
NSString *_Nullable decryptedFilePath = [backupIO createTempFile]; NSString *decryptedFilePath = [backupIO generateTempFilePath];
if (!decryptedFilePath) {
return completion(NO);
}
@autoreleasepool { @autoreleasepool {
if (![backupIO decryptFileAsFile:encryptedFilePath dstFilePath:decryptedFilePath encryptionKey:encryptionKey]) { if (![backupIO decryptFileAsFile:encryptedFilePath dstFilePath:decryptedFilePath encryptionKey:encryptionKey]) {
@ -628,6 +622,13 @@ NS_ASSUME_NONNULL_BEGIN
DDLogError(@"%@ Attachment has invalid file path.", self.logTag); DDLogError(@"%@ Attachment has invalid file path.", self.logTag);
return completion(NO); return completion(NO);
} }
NSString *attachmentDirPath = [attachmentFilePath stringByDeletingLastPathComponent];
if (![OWSFileSystem ensureDirectoryExists:attachmentDirPath]) {
DDLogError(@"%@ Couldn't create directory for attachment file.", self.logTag);
return completion(NO);
}
NSError *error; NSError *error;
BOOL success = BOOL success =
[NSFileManager.defaultManager moveItemAtPath:decryptedFilePath toPath:attachmentFilePath error:&error]; [NSFileManager.defaultManager moveItemAtPath:decryptedFilePath toPath:attachmentFilePath error:&error];

@ -20,6 +20,8 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)initWithJobTempDirPath:(NSString *)jobTempDirPath; - (instancetype)initWithJobTempDirPath:(NSString *)jobTempDirPath;
- (NSString *)generateTempFilePath;
- (nullable NSString *)createTempFile; - (nullable NSString *)createTempFile;
#pragma mark - Encrypt #pragma mark - Encrypt

@ -44,9 +44,14 @@ static const compression_algorithm SignalCompressionAlgorithm = COMPRESSION_LZMA
return self; return self;
} }
- (NSString *)generateTempFilePath
{
return [self.jobTempDirPath stringByAppendingPathComponent:[NSUUID UUID].UUIDString];
}
- (nullable NSString *)createTempFile - (nullable NSString *)createTempFile
{ {
NSString *filePath = [self.jobTempDirPath stringByAppendingPathComponent:[NSUUID UUID].UUIDString]; NSString *filePath = [self generateTempFilePath];
if (![OWSFileSystem ensureFileExists:filePath]) { if (![OWSFileSystem ensureFileExists:filePath]) {
OWSProdLogAndFail(@"%@ could not create temp file.", self.logTag); OWSProdLogAndFail(@"%@ could not create temp file.", self.logTag);
return nil; return nil;

@ -259,7 +259,8 @@ NSString *const kOWSBackup_ImportDatabaseKeySpec = @"kOWSBackup_ImportDatabaseKe
DDLogVerbose(@"%@ %s: %zd", self.logTag, __PRETTY_FUNCTION__, self.attachmentsItems.count); DDLogVerbose(@"%@ %s: %zd", self.logTag, __PRETTY_FUNCTION__, self.attachmentsItems.count);
__block NSUInteger count = 0; __block NSUInteger count = 0;
[self.primaryStorage.newDatabaseConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) { YapDatabaseConnection *dbConnection = self.primaryStorage.newDatabaseConnection;
[dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
for (OWSBackupFragment *item in self.attachmentsItems) { for (OWSBackupFragment *item in self.attachmentsItems) {
if (self.isComplete) { if (self.isComplete) {
return; return;

Loading…
Cancel
Save