//
// C o p y r i g h t ( c ) 2 0 1 8 O p e n W h i s p e r S y s t e m s . A l l r i g h t s r e s e r v e d .
//
import Foundation
import SignalServiceKit
import CloudKit
@objc public class OWSBackupAPI : NSObject {
// I f w e c h a n g e t h e r e c o r d t y p e s , w e n e e d t o e n s u r e i n d i c e s
// a r e c o n f i g u r e d p r o p e r l y i n t h e C l o u d K i t d a s h b o a r d .
static let signalBackupRecordType = " signalBackup "
static let manifestRecordName = " manifest "
static let payloadKey = " payload "
@objc
public class func recordIdForTest ( ) -> String {
return " test- \( NSUUID ( ) . uuidString ) "
}
@objc
public class func saveTestFileToCloud ( fileUrl : URL ,
success : @ escaping ( String ) -> Swift . Void ,
failure : @ escaping ( Error ) -> Swift . Void ) {
saveFileToCloud ( fileUrl : fileUrl ,
recordName : NSUUID ( ) . uuidString ,
recordType : signalBackupRecordType ,
success : success ,
failure : failure )
}
// " E p h e m e r a l " f i l e s a r e s p e c i f i c t o t h i s b a c k u p e x p o r t a n d w i l l a l w a y s n e e d t o
// b e s a v e d . F o r e x a m p l e , a c o m p l e t e i m a g e o f t h e d a t a b a s e i s e x p o r t e d e a c h t i m e .
// W e w o u l d n ' t w a n t t o o v e r w r i t e p r e v i o u s i m a g e s u n t i l t h e e n t i r e b a c k u p e x p o r t i s
// c o m p l e t e .
@objc
public class func saveEphemeralDatabaseFileToCloud ( fileUrl : URL ,
success : @ escaping ( String ) -> Swift . Void ,
failure : @ escaping ( Error ) -> Swift . Void ) {
saveFileToCloud ( fileUrl : fileUrl ,
recordName : NSUUID ( ) . uuidString ,
recordType : signalBackupRecordType ,
success : success ,
failure : failure )
}
// " P e r s i s t e n t " f i l e s m a y b e s h a r e d b e t w e e n b a c k u p e x p o r t ; t h e y s h o u l d o n l y b e s a v e d
// o n c e . F o r e x a m p l e , a t t a c h m e n t f i l e s s h o u l d o n l y b e u p l o a d e d o n c e . S u b s e q u e n t
// b a c k u p s c a n r e u s e t h e s a m e r e c o r d .
@objc
public class func savePersistentFileOnceToCloud ( fileId : String ,
fileUrlBlock : @ escaping ( Swift . Void ) -> URL ? ,
success : @ escaping ( String ) -> Swift . Void ,
failure : @ escaping ( Error ) -> Swift . Void ) {
saveFileOnceToCloud ( recordName : " persistentFile- \( fileId ) " ,
recordType : signalBackupRecordType ,
fileUrlBlock : fileUrlBlock ,
success : success ,
failure : failure )
}
// @ o b j c
// p u b l i c c l a s s f u n c u p s e r t A t t a c h m e n t T o C l o u d ( f i l e U r l : U R L ,
// s u c c e s s : @ e s c a p i n g ( S t r i n g ) - > S w i f t . V o i d ,
// f a i l u r e : @ e s c a p i n g ( E r r o r ) - > S w i f t . V o i d ) {
// / / W e w a n t t o u s e a w e l l - k n o w n r e c o r d i d a n d t y p e f o r m a n i f e s t f i l e s .
// u p s e r t F i l e T o C l o u d ( f i l e U r l : f i l e U r l ,
// r e c o r d N a m e : m a n i f e s t R e c o r d N a m e ,
// r e c o r d T y p e : s i g n a l B a c k u p R e c o r d T y p e ,
// s u c c e s s : s u c c e s s ,
// f a i l u r e : f a i l u r e )
// }
@objc
public class func upsertManifestFileToCloud ( fileUrl : URL ,
success : @ escaping ( String ) -> Swift . Void ,
failure : @ escaping ( Error ) -> Swift . Void ) {
// W e w a n t t o u s e a w e l l - k n o w n r e c o r d i d a n d t y p e f o r m a n i f e s t f i l e s .
upsertFileToCloud ( fileUrl : fileUrl ,
recordName : manifestRecordName ,
recordType : signalBackupRecordType ,
success : success ,
failure : failure )
}
@objc
public class func saveFileToCloud ( fileUrl : URL ,
recordName : String ,
recordType : String ,
success : @ escaping ( String ) -> Swift . Void ,
failure : @ escaping ( Error ) -> Swift . Void ) {
let recordID = CKRecordID ( recordName : recordName )
let record = CKRecord ( recordType : recordType , recordID : recordID )
let asset = CKAsset ( fileURL : fileUrl )
record [ payloadKey ] = asset
saveRecordToCloud ( record : record ,
success : success ,
failure : failure )
}
@objc
public class func saveRecordToCloud ( record : CKRecord ,
success : @ escaping ( String ) -> Swift . Void ,
failure : @ escaping ( Error ) -> Swift . Void ) {
let myContainer = CKContainer . default ( )
let privateDatabase = myContainer . privateCloudDatabase
privateDatabase . save ( record ) {
( record , error ) in
if let error = error {
Logger . error ( " \( self . logTag ) error saving record: \( error ) " )
failure ( error )
} else {
guard let recordName = record ? . recordID . recordName else {
Logger . error ( " \( self . logTag ) error retrieving saved record's name. " )
failure ( OWSErrorWithCodeDescription ( . exportBackupError ,
NSLocalizedString ( " BACKUP_EXPORT_ERROR_SAVE_FILE_TO_CLOUD_FAILED " ,
comment : " Error indicating the a backup export failed to save a file to the cloud. " ) ) )
return
}
Logger . info ( " \( self . logTag ) saved record. " )
success ( recordName )
}
}
}
@objc
public class func deleteRecordFromCloud ( recordName : String ,
success : @ escaping ( Swift . Void ) -> Swift . Void ,
failure : @ escaping ( Error ) -> Swift . Void ) {
let recordID = CKRecordID ( recordName : recordName )
let myContainer = CKContainer . default ( )
let privateDatabase = myContainer . privateCloudDatabase
privateDatabase . delete ( withRecordID : recordID ) {
( record , error ) in
if let error = error {
Logger . error ( " \( self . logTag ) error deleting record: \( error ) " )
failure ( error )
} else {
Logger . info ( " \( self . logTag ) deleted record. " )
success ( )
}
}
}
@objc
public class func upsertFileToCloud ( fileUrl : URL ,
recordName : String ,
recordType : String ,
success : @ escaping ( String ) -> Swift . Void ,
failure : @ escaping ( Error ) -> Swift . Void ) {
let recordId = CKRecordID ( recordName : recordName )
let fetchOperation = CKFetchRecordsOperation ( recordIDs : [ recordId ] )
// D o n ' t d o w n l o a d t h e f i l e ; w e ' r e j u s t u s i n g t h e f e t c h t o c h e c k w h e t h e r o r
// n o t t h i s r e c o r d a l r e a d y e x i s t s .
fetchOperation . desiredKeys = [ ]
fetchOperation . perRecordCompletionBlock = { ( record , recordId , error ) in
if let error = error {
if let ckerror = error as ? CKError {
if ckerror . code = = . unknownItem {
// N o r e c o r d f o u n d t o u p d a t e , s a v i n g n e w r e c o r d .
saveFileToCloud ( fileUrl : fileUrl ,
recordName : recordName ,
recordType : recordType ,
success : success ,
failure : failure )
return
}
Logger . error ( " \( self . logTag ) error fetching record: \( error ) \( ckerror . code ) . " )
} else {
Logger . error ( " \( self . logTag ) error fetching record: \( error ) . " )
}
failure ( error )
return
}
guard let record = record else {
Logger . error ( " \( self . logTag ) error missing record. " )
Logger . flush ( )
failure ( OWSErrorWithCodeDescription ( . exportBackupError ,
NSLocalizedString ( " BACKUP_EXPORT_ERROR_SAVE_FILE_TO_CLOUD_FAILED " ,
comment : " Error indicating the a backup export failed to save a file to the cloud. " ) ) )
return
}
Logger . verbose ( " \( self . logTag ) updating record. " )
let asset = CKAsset ( fileURL : fileUrl )
record [ payloadKey ] = asset
saveRecordToCloud ( record : record ,
success : success ,
failure : failure )
}
let myContainer = CKContainer . default ( )
let privateDatabase = myContainer . privateCloudDatabase
privateDatabase . add ( fetchOperation )
}
@objc
public class func saveFileOnceToCloud ( recordName : String ,
recordType : String ,
fileUrlBlock : @ escaping ( Swift . Void ) -> URL ? ,
success : @ escaping ( String ) -> Swift . Void ,
failure : @ escaping ( Error ) -> Swift . Void ) {
let recordId = CKRecordID ( recordName : recordName )
let fetchOperation = CKFetchRecordsOperation ( recordIDs : [ recordId ] )
// D o n ' t d o w n l o a d t h e f i l e ; w e ' r e j u s t u s i n g t h e f e t c h t o c h e c k w h e t h e r o r
// n o t t h i s r e c o r d a l r e a d y e x i s t s .
fetchOperation . desiredKeys = [ ]
fetchOperation . perRecordCompletionBlock = { ( record , recordId , error ) in
if let error = error {
if let ckerror = error as ? CKError {
if ckerror . code = = . unknownItem {
// N o r e c o r d f o u n d t o u p d a t e , s a v i n g n e w r e c o r d .
guard let fileUrl = fileUrlBlock ( ) else {
Logger . error ( " \( self . logTag ) error preparing file for upload. " )
return
}
saveFileToCloud ( fileUrl : fileUrl ,
recordName : recordName ,
recordType : recordType ,
success : success ,
failure : failure )
return
}
Logger . error ( " \( self . logTag ) error fetching record: \( error ) \( ckerror . code ) . " )
} else {
Logger . error ( " \( self . logTag ) error fetching record: \( error ) . " )
}
failure ( error )
return
}
Logger . info ( " \( self . logTag ) record already exists; skipping save. " )
success ( recordName )
}
let myContainer = CKContainer . default ( )
let privateDatabase = myContainer . privateCloudDatabase
privateDatabase . add ( fetchOperation )
}
@objc
public class func fetchAllRecordNames ( success : @ escaping ( [ String ] ) -> Swift . Void ,
failure : @ escaping ( Error ) -> Swift . Void ) {
let query = CKQuery ( recordType : signalBackupRecordType , predicate : NSPredicate ( value : true ) )
// F e t c h t h e f i r s t p a g e o f r e s u l t s f o r t h i s q u e r y .
fetchAllRecordNamesStep ( query : query ,
previousRecordNames : [ String ] ( ) ,
cursor : nil ,
success : success ,
failure : failure )
}
private class func fetchAllRecordNamesStep ( query : CKQuery ,
previousRecordNames : [ String ] ,
cursor : CKQueryCursor ? ,
success : @ escaping ( [ String ] ) -> Swift . Void ,
failure : @ escaping ( Error ) -> Swift . Void ) {
var allRecordNames = previousRecordNames
let queryOperation = CKQueryOperation ( query : query )
// I f t h i s i s n ' t t h e f i r s t p a g e o f r e s u l t s f o r t h i s q u e r y , r e s u m e
// w h e r e w e l e f t o f f .
queryOperation . cursor = cursor
// D o n ' t d o w n l o a d t h e f i l e ; w e ' r e j u s t u s i n g t h e q u e r y t o g e t a l i s t o f r e c o r d n a m e s .
queryOperation . desiredKeys = [ ]
queryOperation . recordFetchedBlock = { ( record ) in
assert ( record . recordID . recordName . count > 0 )
allRecordNames . append ( record . recordID . recordName )
}
queryOperation . queryCompletionBlock = { ( cursor , error ) in
if let error = error {
Logger . error ( " \( self . logTag ) error fetching all record names: \( error ) . " )
failure ( error )
return
}
if let cursor = cursor {
Logger . verbose ( " \( self . logTag ) fetching more record names \( allRecordNames . count ) . " )
// T h e r e a r e m o r e p a g e s o f r e s u l t s , c o n t i n u e f e t c h i n g .
fetchAllRecordNamesStep ( query : query ,
previousRecordNames : allRecordNames ,
cursor : cursor ,
success : success ,
failure : failure )
return
}
Logger . info ( " \( self . logTag ) fetched \( allRecordNames . count ) record names. " )
success ( allRecordNames )
}
let myContainer = CKContainer . default ( )
let privateDatabase = myContainer . privateCloudDatabase
privateDatabase . add ( queryOperation )
}
@objc
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: \( 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 )
}
}
} )
}
}