session-ios/Signal/src/util/OWSBackupLazyRestoreJob.swift

93 lines
3.3 KiB
Swift
Raw Normal View History

2018-03-20 22:22:19 +01:00
//
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
//
import Foundation
import PromiseKit
import SignalServiceKit
@objc
public class OWSBackupLazyRestoreJob: NSObject {
let primaryStorage: OWSPrimaryStorage
private var jobTempDirPath: String?
deinit {
if let jobTempDirPath = self.jobTempDirPath {
DispatchQueue.global().async {
OWSFileSystem.deleteFile(jobTempDirPath)
}
}
}
@objc
2018-04-02 15:39:13 +02:00
public class func runAsync() {
OWSBackupLazyRestoreJob().runAsync()
2018-03-20 22:22:19 +01:00
}
public override init() {
self.primaryStorage = OWSPrimaryStorage.shared()
}
2018-04-02 15:39:13 +02:00
private func runAsync() {
2018-04-11 21:17:34 +02:00
SwiftAssertIsOnMainThread(#function)
2018-03-20 22:22:19 +01:00
DispatchQueue.global().async {
self.restoreAttachments()
}
}
private func restoreAttachments() {
let temporaryDirectory = NSTemporaryDirectory()
let jobTempDirPath = (temporaryDirectory as NSString).appendingPathComponent(NSUUID().uuidString)
guard OWSFileSystem.ensureDirectoryExists(jobTempDirPath) else {
2018-03-22 18:48:22 +01:00
Logger.error("\(logTag) could not create temp directory.")
2018-03-20 22:22:19 +01:00
return
}
self.jobTempDirPath = jobTempDirPath
let backupIO = OWSBackupIO(jobTempDirPath: jobTempDirPath)
let attachmentIds = OWSBackup.shared().attachmentIdsForLazyRestore()
2018-03-22 18:48:22 +01:00
guard attachmentIds.count > 0 else {
Logger.info("\(logTag) No attachments need lazy restore.")
return
}
Logger.info("\(logTag) Lazy restoring \(attachmentIds.count) attachments.")
2018-03-20 22:22:19 +01:00
self.tryToRestoreNextAttachment(attachmentIds: attachmentIds, backupIO: backupIO)
}
private func tryToRestoreNextAttachment(attachmentIds: [String], backupIO: OWSBackupIO) {
var attachmentIdsCopy = attachmentIds
guard let attachmentId = attachmentIdsCopy.last else {
// This job is done.
2018-03-22 18:48:22 +01:00
Logger.verbose("\(logTag) job is done.")
2018-03-20 22:22:19 +01:00
return
}
attachmentIdsCopy.removeLast()
guard let attachment = TSAttachmentStream.fetch(uniqueId: attachmentId) else {
2018-03-22 18:48:22 +01:00
Logger.warn("\(logTag) could not load attachment.")
2018-03-20 22:22:19 +01:00
// Not necessarily an error.
// The attachment might have been deleted since the job began.
// Continue trying to restore the other attachments.
tryToRestoreNextAttachment(attachmentIds: attachmentIds, backupIO: backupIO)
return
}
OWSBackup.shared().lazyRestoreAttachment(attachment,
backupIO: backupIO,
completion: { (success) in
if success {
2018-03-22 18:48:22 +01:00
Logger.info("\(self.logTag) restored attachment.")
2018-03-20 22:22:19 +01:00
} else {
2018-03-22 18:48:22 +01:00
Logger.warn("\(self.logTag) could not restore attachment.")
2018-03-20 22:22:19 +01:00
}
// Continue trying to restore the other attachments.
self.tryToRestoreNextAttachment(attachmentIds: attachmentIdsCopy, backupIO: backupIO)
})
}
}