|
18 | 18 |
|
19 | 19 | #import "OCCore.h"
|
20 | 20 | #import "OCCore+ItemList.h"
|
| 21 | +#import "OCDataRenderer.h" |
| 22 | +#import "NSProgress+OCExtensions.h" |
| 23 | +#import "GADrive.h" |
| 24 | +#import "GADriveItem.h" |
| 25 | +#import "NSError+OCError.h" |
21 | 26 |
|
22 | 27 | @implementation OCCore (DriveManagement)
|
23 | 28 |
|
@@ -74,4 +79,113 @@ - (nullable NSProgress *)updateDrive:(OCDrive *)drive properties:(NSDictionary<O
|
74 | 79 | }]);
|
75 | 80 | }
|
76 | 81 |
|
| 82 | +- (void)retrieveDrive:(OCDrive *)drive itemForResource:(OCDriveResource)resource completionHandler:(void(^)(NSError * _Nullable error, OCItem * _Nullable item))completionHandler |
| 83 | +{ |
| 84 | + NSError *error = nil; |
| 85 | + GADriveItem *driveItem = nil; |
| 86 | + |
| 87 | + if ([resource isEqual:OCDriveResourceCoverImage]) { |
| 88 | + // Use special "image" drive item from GADrive |
| 89 | + driveItem = [drive.gaDrive specialDriveItemFor:GASpecialFolderNameImage]; |
| 90 | + } else if ([resource isEqual:OCDriveResourceCoverDescription]) { |
| 91 | + // Use special "readme" drive item from GADrive |
| 92 | + driveItem = [drive.gaDrive specialDriveItemFor:GASpecialFolderNameReadme]; |
| 93 | + } else if ([resource isEqual:OCDriveResourceSpaceFolder]) { |
| 94 | + // Use APIs to determine .space folder and find an item for it, creating it if needed |
| 95 | + OCPath spacesFolderPath; |
| 96 | + if ((spacesFolderPath = [self classSettingForOCClassSettingsKey:OCCoreSpaceResourceFolderPath]) != nil) |
| 97 | + { |
| 98 | + OCLocation *spacesFolderLocation = [[OCLocation alloc] initWithDriveID:drive.identifier path:[NSString stringWithFormat:@"/%@/", spacesFolderPath]]; // Add leading and trailing / to spaces folder path |
| 99 | + __block OCCoreItemTracking itemTracking; |
| 100 | + __weak OCCore *weakCore = self; |
| 101 | + dispatch_queue_t queue = _queue; |
| 102 | + itemTracking = [self trackItemAtLocation:spacesFolderLocation trackingHandler:^(NSError * _Nullable error, OCItem * _Nullable item, BOOL isInitial) { |
| 103 | + if (isInitial) |
| 104 | + { |
| 105 | + dispatch_async(queue, ^{ |
| 106 | + // End tracking |
| 107 | + itemTracking = nil; |
| 108 | + |
| 109 | + // Check for errors |
| 110 | + if (error != nil) |
| 111 | + { |
| 112 | + completionHandler(error, nil); |
| 113 | + return; |
| 114 | + } |
| 115 | + |
| 116 | + // Folder found -> return |
| 117 | + if (item != nil) |
| 118 | + { |
| 119 | + completionHandler(nil, item); |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + // Folder not found -> create it |
| 124 | + OCCore *strongCore; |
| 125 | + |
| 126 | + if ((strongCore = weakCore) == nil) |
| 127 | + { |
| 128 | + completionHandler(OCError(OCErrorInternal), nil); |
| 129 | + return; |
| 130 | + } |
| 131 | + |
| 132 | + [strongCore.vault.database retrieveCacheItemsAtLocation:drive.rootLocation itemOnly:YES completionHandler:^(OCDatabase *db, NSError *error, OCSyncAnchor syncAnchor, NSArray<OCItem *> *items) { |
| 133 | + // Determine root item |
| 134 | + if (error != nil) |
| 135 | + { |
| 136 | + completionHandler(error, nil); |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + OCItem *rootItem = items.firstObject; |
| 141 | + if (rootItem != nil) |
| 142 | + { |
| 143 | + // Create folder |
| 144 | + OCCore *strongCore; |
| 145 | + if ((strongCore = weakCore) == nil) |
| 146 | + { |
| 147 | + completionHandler(OCError(OCErrorInternal), nil); |
| 148 | + return; |
| 149 | + } |
| 150 | + |
| 151 | + [strongCore createFolder:spacesFolderPath inside:rootItem options:nil resultHandler:^(NSError * _Nullable error, OCCore * _Nonnull core, OCItem * _Nullable item, id _Nullable parameter) { |
| 152 | + // Return created folder result |
| 153 | + completionHandler(error, item); |
| 154 | + }]; |
| 155 | + } |
| 156 | + }]; |
| 157 | + }); |
| 158 | + } |
| 159 | + }]; |
| 160 | + } |
| 161 | + return; |
| 162 | + } else { |
| 163 | + // Unknown/unsupported resource type -> return error |
| 164 | + completionHandler(OCError(OCErrorInvalidParameter), nil); |
| 165 | + } |
| 166 | + |
| 167 | + if ((driveItem != nil) && (driveItem.identifier != nil)) |
| 168 | + { |
| 169 | + // GADriveItem.id is the FileID of the item, so fetch it from the database |
| 170 | + [self.vault.database retrieveCacheItemForFileID:driveItem.identifier completionHandler:^(OCDatabase *db, NSError *error, OCSyncAnchor syncAnchor, OCItem *item) { |
| 171 | + completionHandler(error, item); |
| 172 | + }]; |
| 173 | + } |
| 174 | + else |
| 175 | + { |
| 176 | + // No drive item of type found for drive |
| 177 | + completionHandler(OCError(OCErrorResourceNotFound), nil); |
| 178 | + } |
| 179 | +} |
| 180 | + |
| 181 | +- (nullable NSProgress *)updateDrive:(OCDrive *)drive resourceFor:(OCDriveResource)resource withItem:(nullable OCItem *)item completionHandler:(void(^)(NSError * _Nullable error, OCDrive * _Nullable drive))completionHandler |
| 182 | +{ |
| 183 | + return ([self.connection updateDrive:drive resourceFor:resource withItem:item completionHandler:^(NSError * _Nullable error, OCDrive * _Nullable drive) { |
| 184 | + if (error == nil) { |
| 185 | + [self fetchUpdatesWithCompletionHandler:nil]; |
| 186 | + } |
| 187 | + completionHandler(error, drive); |
| 188 | + }]); |
| 189 | +} |
| 190 | + |
77 | 191 | @end
|
0 commit comments