diff --git a/Signal/src/util/OWSBackupAPI.swift b/Signal/src/util/OWSBackupAPI.swift index 6324c2f46..63989fe7a 100644 --- a/Signal/src/util/OWSBackupAPI.swift +++ b/Signal/src/util/OWSBackupAPI.swift @@ -6,22 +6,6 @@ import Foundation import SignalServiceKit import CloudKit -//func FormatAnalyticsLocation(file: String, function: String) -> NSString { -// return "\((file as NSString).lastPathComponent):\(function)" as NSString -//} -// -//func OWSProdError(_ eventName: String, file: String, function: String, line: Int32) { -// let location = FormatAnalyticsLocation(file: file, function: function) -// OWSAnalytics -// .logEvent(eventName, severity: .error, parameters: nil, location: location.utf8String!, line:line) -//} -// -//func OWSProdInfo(_ eventName: String, file: String, function: String, line: Int32) { -// let location = FormatAnalyticsLocation(file: file, function: function) -// OWSAnalytics -// .logEvent(eventName, severity: .info, parameters: nil, location: location.utf8String!, line:line) -//} - @objc public class OWSBackupAPI: NSObject { @objc public class func recordIdForTest() -> String { @@ -30,31 +14,39 @@ import CloudKit @objc public class func recordIdForAttachmentStream(value: TSAttachmentStream) -> String { - return "attachment-stream-\(value.uniqueId)" + guard let uniqueId = value.uniqueId else { + owsFail("Database entity missing uniqueId.") + return "unknown" + } + return "attachment-stream-\(uniqueId)" } @objc - public class func saveTestFileToCloud(fileUrl: NSURL, - completion: @escaping (Error?) -> Swift.Void) { + public class func saveTestFileToCloud(fileUrl: URL, + completion: @escaping (Error?) -> Swift.Void) { saveFileToCloud(fileUrl: fileUrl, - recordId: recordIdForTest(), - recordType: "test", - completion: completion) + recordId: recordIdForTest(), + recordType: "test", + completion: completion) } @objc - public class func saveFileToCloud(fileUrl: NSURL, - recordId: String, - recordType: String, - completion: @escaping (Error?) -> Swift.Void) { + public class func saveFileToCloud(fileUrl: URL, + recordId: String, + recordType: String, + completion: @escaping (Error?) -> Swift.Void) { let recordID = CKRecordID(recordName: recordId) let record = CKRecord(recordType: recordType, recordID: recordID) -// artworkRecord["title"] = "MacKerricher State Park" as NSString -// artworkRecord["artist"] = "Mei Chen" as NSString -// artworkRecord["address"] = "Fort Bragg, CA" as NSString -// artworkRecord[@"title" ] = @"MacKerricher State Park"; -// artworkRecord[@"artist"] = @"Mei Chen"; -// artworkRecord[@"address"] = @"Fort Bragg, CA"; + let asset = CKAsset(fileURL: fileUrl) + record["payload"] = asset + + saveRecordToCloud(record: record, + completion: completion) + } + + @objc + public class func saveRecordToCloud(record: CKRecord, + completion: @escaping (Error?) -> Swift.Void) { let myContainer = CKContainer.default() let privateDatabase = myContainer.privateCloudDatabase @@ -75,23 +67,22 @@ import CloudKit public class func checkCloudKitAccess(completion: @escaping (Bool) -> Swift.Void) { CKContainer.default().accountStatus(completionHandler: { (accountStatus, error) in DispatchQueue.main.async { - switch accountStatus { - case .couldNotDetermine: - Logger.error("\(self.logTag) could not determine CloudKit account status:\(error).") - OWSAlerts.showErrorAlert(withMessage: NSLocalizedString("CLOUDKIT_STATUS_COULD_NOT_DETERMINE", comment: "Error indicating that the app could not determine that user's CloudKit account status")) - completion(false) - case .noAccount: - Logger.error("\(self.logTag) no CloudKit account.") - OWSAlerts.showErrorAlert(withMessage: NSLocalizedString("CLOUDKIT_STATUS_NO_ACCOUNT", comment: "Error indicating that user does not have an iCloud account.")) - completion(false) - case .restricted: - Logger.error("\(self.logTag) restricted CloudKit account.") - OWSAlerts.showErrorAlert(withMessage: NSLocalizedString("CLOUDKIT_STATUS_RESTRICTED", comment: "Error indicating that the app was prevented from accessing the user's CloudKit account.")) - completion(false) - case .available: - Logger.error("\(self.logTag) no CloudKit account.") - completion(true) - } + switch accountStatus { + case .couldNotDetermine: + Logger.error("\(self.logTag) could not determine CloudKit account status:\(String(describing: error)).") + OWSAlerts.showErrorAlert(withMessage: NSLocalizedString("CLOUDKIT_STATUS_COULD_NOT_DETERMINE", comment: "Error indicating that the app could not determine that user's CloudKit account status")) + completion(false) + case .noAccount: + Logger.error("\(self.logTag) no CloudKit account.") + OWSAlerts.showErrorAlert(withMessage: NSLocalizedString("CLOUDKIT_STATUS_NO_ACCOUNT", comment: "Error indicating that user does not have an iCloud account.")) + completion(false) + case .restricted: + Logger.error("\(self.logTag) restricted CloudKit account.") + OWSAlerts.showErrorAlert(withMessage: NSLocalizedString("CLOUDKIT_STATUS_RESTRICTED", comment: "Error indicating that the app was prevented from accessing the user's CloudKit account.")) + completion(false) + case .available: + completion(true) + } } }) }