Upload attachments to cloud; upsert files to cloud.
This commit is contained in:
parent
0971bad4b2
commit
20587ba377
|
@ -12,21 +12,21 @@ import CloudKit
|
|||
return "test-\(NSUUID().uuidString)"
|
||||
}
|
||||
|
||||
@objc
|
||||
public class func recordIdForAttachmentStream(value: TSAttachmentStream) -> String {
|
||||
guard let uniqueId = value.uniqueId else {
|
||||
owsFail("Database entity missing uniqueId.")
|
||||
return "unknown"
|
||||
}
|
||||
return "attachment-stream-\(uniqueId)"
|
||||
}
|
||||
// @objc
|
||||
// public class func recordNameForTest(value: TSAttachmentStream) -> String {
|
||||
// guard let uniqueId = value.uniqueId else {
|
||||
// owsFail("Database entity missing uniqueId.")
|
||||
// return "unknown"
|
||||
// }
|
||||
// return "attachment-stream-\(uniqueId)"
|
||||
// }
|
||||
|
||||
@objc
|
||||
public class func saveTestFileToCloud(fileUrl: URL,
|
||||
success: @escaping (String) -> Swift.Void,
|
||||
failure: @escaping (Error) -> Swift.Void) {
|
||||
saveFileToCloud(fileUrl: fileUrl,
|
||||
recordId: recordIdForTest(),
|
||||
recordName: NSUUID().uuidString,
|
||||
recordType: "test",
|
||||
success: success,
|
||||
failure: failure)
|
||||
|
@ -37,22 +37,39 @@ import CloudKit
|
|||
success: @escaping (String) -> Swift.Void,
|
||||
failure: @escaping (Error) -> Swift.Void) {
|
||||
saveFileToCloud(fileUrl: fileUrl,
|
||||
recordId: NSUUID().uuidString,
|
||||
recordName: NSUUID().uuidString,
|
||||
recordType: "backupFile",
|
||||
success: success,
|
||||
failure: failure)
|
||||
}
|
||||
|
||||
// TODO:
|
||||
static let manifestRecordName = "manifest_"
|
||||
static let manifestRecordType = "manifest"
|
||||
static let payloadKey = "payload"
|
||||
|
||||
@objc
|
||||
public class func upsertManifestFileToCloud(fileUrl: URL,
|
||||
success: @escaping (String) -> Swift.Void,
|
||||
failure: @escaping (Error) -> Swift.Void) {
|
||||
// We want to use a well-known record id and type for manifest files.
|
||||
upsertFileToCloud(fileUrl: fileUrl,
|
||||
recordName: manifestRecordName,
|
||||
recordType: manifestRecordType,
|
||||
success: success,
|
||||
failure: failure)
|
||||
}
|
||||
|
||||
@objc
|
||||
public class func saveFileToCloud(fileUrl: URL,
|
||||
recordId: String,
|
||||
recordName: String,
|
||||
recordType: String,
|
||||
success: @escaping (String) -> Swift.Void,
|
||||
failure: @escaping (Error) -> Swift.Void) {
|
||||
let recordID = CKRecordID(recordName: recordId)
|
||||
let recordID = CKRecordID(recordName: recordName)
|
||||
let record = CKRecord(recordType: recordType, recordID: recordID)
|
||||
let asset = CKAsset(fileURL: fileUrl)
|
||||
record["payload"] = asset
|
||||
record[payloadKey] = asset
|
||||
|
||||
saveRecordToCloud(record: record,
|
||||
success: success,
|
||||
|
@ -86,6 +103,54 @@ import CloudKit
|
|||
}
|
||||
}
|
||||
|
||||
@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 ])
|
||||
fetchOperation.perRecordCompletionBlock = { (record, recordId, error) in
|
||||
if let error = error {
|
||||
if let ckerror = error as? CKError {
|
||||
if ckerror.code == .unknownItem {
|
||||
// No record found to update, saving new record.
|
||||
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 checkCloudKitAccess(completion: @escaping (Bool) -> Swift.Void) {
|
||||
CKContainer.default().accountStatus(completionHandler: { (accountStatus, error) in
|
||||
|
|
|
@ -4,10 +4,6 @@
|
|||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
// extern NSString *const OWSBackup_FileExtension;
|
||||
|
||||
// extern NSString *const NSNotificationNameBackupStateDidChange;
|
||||
|
||||
@class OWSBackupExport;
|
||||
|
||||
@protocol OWSBackupExportDelegate <NSObject>
|
||||
|
@ -16,21 +12,18 @@ NS_ASSUME_NONNULL_BEGIN
|
|||
// and retrieved with the backup PIN.
|
||||
- (nullable NSData *)backupKey;
|
||||
|
||||
// Either backupExportDidSucceed:... or backupExportDidFail:... will
|
||||
// be called exactly once on the main thread UNLESS:
|
||||
//
|
||||
// * The export was never started.
|
||||
// * The export was cancelled.
|
||||
- (void)backupExportDidSucceed:(OWSBackupExport *)backupExport;
|
||||
|
||||
- (void)backupExportDidFail:(OWSBackupExport *)backupExport error:(NSError *)error;
|
||||
|
||||
@end
|
||||
|
||||
//#pragma mark -
|
||||
|
||||
// typedef NS_ENUM(NSUInteger, OWSBackupState) {
|
||||
// OWSBackupState_AtRest = 0,
|
||||
// OWSBackupState_InProgress,
|
||||
// // OWSBackupState_Cancelled,
|
||||
// OWSBackupState_Failed,
|
||||
//};
|
||||
|
||||
@class OWSPrimaryStorage;
|
||||
|
||||
@interface OWSBackupExport : NSObject
|
||||
|
@ -44,33 +37,6 @@ NS_ASSUME_NONNULL_BEGIN
|
|||
|
||||
- (void)cancel;
|
||||
|
||||
//@property (nonatomic, readonly) OWSBackupState backupExportState;
|
||||
//
|
||||
////@property (nonatomic, readonly) CGFloat backupProgress;
|
||||
////
|
||||
////// If non-nil, backup is encrypted.
|
||||
////@property (nonatomic, nullable, readonly) NSString *backupPassword;
|
||||
////
|
||||
////// Only applies to "backup export" task.
|
||||
////@property (nonatomic, nullable, readonly) TSThread *currentThread;
|
||||
////
|
||||
////@property (nonatomic, readonly) NSString *backupZipPath;
|
||||
////
|
||||
//
|
||||
//
|
||||
//+ (instancetype)sharedManager;
|
||||
//
|
||||
//- (BOOL)isBackupEnabled;
|
||||
//- (void)setIsBackupEnabled:(BOOL)value;
|
||||
|
||||
//- (void)exportBackup:(nullable TSThread *)currentThread skipPassword:(BOOL)skipPassword;
|
||||
//
|
||||
//- (void)importBackup:(NSString *)backupZipPath password:(NSString *_Nullable)password;
|
||||
//
|
||||
//- (void)cancel;
|
||||
//
|
||||
//+ (void)applicationDidFinishLaunching;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
||||
|
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue