CCSynchronize.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. //
  2. // CCSynchronize.m
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 19/10/16.
  6. // Copyright (c) 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #import "CCSynchronize.h"
  24. #import "AppDelegate.h"
  25. #import "CCMain.h"
  26. #import "NCBridgeSwift.h"
  27. @interface CCSynchronize ()
  28. {
  29. AppDelegate *appDelegate;
  30. }
  31. @end
  32. @implementation CCSynchronize
  33. + (CCSynchronize *)sharedSynchronize {
  34. static CCSynchronize *sharedSynchronize;
  35. @synchronized(self)
  36. {
  37. if (!sharedSynchronize) {
  38. sharedSynchronize = [CCSynchronize new];
  39. sharedSynchronize.foldersInSynchronized = [NSMutableOrderedSet new];
  40. sharedSynchronize->appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  41. }
  42. return sharedSynchronize;
  43. }
  44. }
  45. #pragma --------------------------------------------------------------------------------------------
  46. #pragma mark ===== Read Folder =====
  47. #pragma --------------------------------------------------------------------------------------------
  48. // serverUrl : start
  49. // directoryID : start
  50. // selector : selectorReadFolder, selectorReadFolderWithDownload
  51. //
  52. - (void)readFolder:(NSString *)serverUrl selector:(NSString *)selector
  53. {
  54. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:appDelegate.activeAccount];
  55. metadataNet.action = actionReadFolder;
  56. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:serverUrl];
  57. if (!directoryID) return;
  58. metadataNet.depth = @"1";
  59. metadataNet.directoryID = directoryID;
  60. metadataNet.priority = NSOperationQueuePriorityLow;
  61. metadataNet.selector = selector;
  62. metadataNet.serverUrl = serverUrl;
  63. [appDelegate addNetworkingOperationQueue:appDelegate.netQueue delegate:self metadataNet:metadataNet];
  64. NSLog(@"[LOG] %@ directory : %@", selector, serverUrl);
  65. }
  66. // MULTI THREAD
  67. - (void)readFolderSuccessFailure:(CCMetadataNet *)metadataNet metadataFolder:(tableMetadata *)metadataFolder metadatas:(NSArray *)metadatas message:(NSString *)message errorCode:(NSInteger)errorCode
  68. {
  69. // Check Active Account
  70. if (![metadataNet.account isEqualToString:appDelegate.activeAccount])
  71. return;
  72. // ERROR
  73. if (errorCode != 0) {
  74. // Folder not present, remove it
  75. if (errorCode == 404) {
  76. [[NCManageDatabase sharedInstance] deleteDirectoryAndSubDirectoryWithServerUrl:metadataNet.serverUrl];
  77. [appDelegate.activeMain reloadDatasource:metadataNet.serverUrl];
  78. }
  79. return;
  80. }
  81. // Add/update self Folder
  82. if (!metadataFolder || !metadatas || [metadatas count] == 0)
  83. return;
  84. // Add metadata and update etag Directory
  85. (void)[[NCManageDatabase sharedInstance] addMetadata:metadataFolder];
  86. [[NCManageDatabase sharedInstance] setDirectoryWithServerUrl:metadataNet.serverUrl serverUrlTo:nil etag:metadataFolder.etag fileID:metadataFolder.fileID encrypted:metadataFolder.e2eEncrypted];
  87. // reload folder ../ *
  88. NSString *serverUrlParent = [[NCManageDatabase sharedInstance] getServerUrl:metadataFolder.directoryID];
  89. [appDelegate.activeMain reloadDatasource:serverUrlParent];
  90. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
  91. NSMutableArray *metadatasForVerifyChange = [NSMutableArray new];
  92. NSMutableArray *addMetadatas = [NSMutableArray new];
  93. NSArray *recordsInSessions = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"directoryID == %@ AND session != ''", metadataNet.directoryID] sorted:nil ascending:NO];
  94. // ----- Test : (DELETE) -----
  95. NSMutableArray *metadatasNotPresents = [NSMutableArray new];
  96. NSArray *tableMetadatas = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"directoryID == %@ AND session == ''", metadataNet.directoryID] sorted:nil ascending:NO];
  97. for (tableMetadata *record in tableMetadatas) {
  98. BOOL fileIDFound = NO;
  99. for (tableMetadata *metadata in metadatas) {
  100. if ([record.fileID isEqualToString:metadata.fileID]) {
  101. fileIDFound = YES;
  102. break;
  103. }
  104. }
  105. if (!fileIDFound)
  106. [metadatasNotPresents addObject:record];
  107. }
  108. // delete metadata not present
  109. for (tableMetadata *metadata in metadatasNotPresents) {
  110. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryProviderStorageFileID:metadata.fileID] error:nil];
  111. if (metadata.directory && metadataNet.serverUrl) {
  112. NSString *dirForDelete = [CCUtility stringAppendServerUrl:metadataNet.serverUrl addFileName:metadata.fileName];
  113. [[NCManageDatabase sharedInstance] deleteDirectoryAndSubDirectoryWithServerUrl:dirForDelete];
  114. }
  115. [[NCManageDatabase sharedInstance] deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", metadata.fileID] clearDateReadDirectoryID:nil];
  116. [[NCManageDatabase sharedInstance] deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", metadata.fileID]];
  117. [[NCManageDatabase sharedInstance] deletePhotosWithFileID:metadata.fileID];
  118. [appDelegate.activePhotos.fileIDHide addObject:metadata.fileID];
  119. }
  120. dispatch_async(dispatch_get_main_queue(), ^{
  121. if ([metadatasNotPresents count] > 0)
  122. [appDelegate.activeMain reloadDatasource:metadataNet.serverUrl];
  123. });
  124. // ----- Test : (MODIFY) -----
  125. for (tableMetadata *metadata in metadatas) {
  126. // RECURSIVE DIRECTORY MODE
  127. if (metadata.directory) {
  128. NSString *serverUrl = [CCUtility stringAppendServerUrl:metadataNet.serverUrl addFileName:metadata.fileName];
  129. NSString *etag = metadata.etag;
  130. // Verify if do not exists this Metadata
  131. tableMetadata *result = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", metadata.fileID]];
  132. if (!result)
  133. (void)[[NCManageDatabase sharedInstance] addMetadata:metadata];
  134. // Load if different etag
  135. tableDirectory *tableDirectory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", metadataNet.account, serverUrl]];
  136. if (![tableDirectory.etag isEqualToString:etag] || [metadataNet.selector isEqualToString:selectorReadFolderWithDownload]) {
  137. [self readFolder:serverUrl selector:metadataNet.selector];
  138. }
  139. } else {
  140. if ([metadataNet.selector isEqualToString:selectorReadFolderWithDownload]) {
  141. // It's in session
  142. BOOL recordInSession = NO;
  143. for (tableMetadata *record in recordsInSessions) {
  144. if ([record.fileID isEqualToString:metadata.fileID]) {
  145. recordInSession = YES;
  146. break;
  147. }
  148. }
  149. if (recordInSession)
  150. continue;
  151. // Ohhhh INSERT
  152. [metadatasForVerifyChange addObject:metadata];
  153. }
  154. if ([metadataNet.selector isEqualToString:selectorReadFolder]) {
  155. // Verify if do not exists this Metadata
  156. tableMetadata *result = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", metadata.fileID]];
  157. if (!result)
  158. [addMetadatas addObject:metadata];
  159. }
  160. }
  161. }
  162. if ([addMetadatas count] > 0)
  163. (void)[[NCManageDatabase sharedInstance] addMetadatas:addMetadatas serverUrl:metadataNet.serverUrl];
  164. if ([metadatasForVerifyChange count] > 0)
  165. [self verifyChangeMedatas:metadatasForVerifyChange serverUrl:metadataNet.serverUrl account:metadataNet.account withDownload:YES];
  166. });
  167. }
  168. #pragma --------------------------------------------------------------------------------------------
  169. #pragma mark ===== Read File for Folder & Read File=====
  170. #pragma --------------------------------------------------------------------------------------------
  171. - (void)readFileForFolder:(NSString *)fileName serverUrl:(NSString *)serverUrl selector:(NSString *)selector
  172. {
  173. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:appDelegate.activeAccount];
  174. metadataNet.action = actionReadFile;
  175. metadataNet.fileName = fileName;
  176. metadataNet.priority = NSOperationQueuePriorityLow;
  177. metadataNet.selector = selector;
  178. metadataNet.serverUrl = serverUrl;
  179. [appDelegate addNetworkingOperationQueue:appDelegate.netQueue delegate:self metadataNet:metadataNet];
  180. }
  181. - (void)readFile:(tableMetadata *)metadata selector:(NSString *)selector
  182. {
  183. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  184. if (!serverUrl) return;
  185. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:appDelegate.activeAccount];
  186. metadataNet.action = actionReadFile;
  187. metadataNet.fileID = metadata.fileID;
  188. metadataNet.fileName = metadata.fileName;
  189. metadataNet.priority = NSOperationQueuePriorityLow;
  190. metadataNet.selector = selector;
  191. metadataNet.serverUrl = serverUrl;
  192. [appDelegate addNetworkingOperationQueue:appDelegate.netQueue delegate:self metadataNet:metadataNet];
  193. }
  194. - (void)readFileSuccessFailure:(CCMetadataNet *)metadataNet metadata:(tableMetadata *)metadata message:(NSString *)message errorCode:(NSInteger)errorCode
  195. {
  196. // Check Active Account
  197. if (![metadataNet.account isEqualToString:appDelegate.activeAccount])
  198. return;
  199. if (errorCode == 0) {
  200. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
  201. // Selector : selectorReadFile, selectorReadFileWithDownload
  202. if ([metadataNet.selector isEqualToString:selectorReadFile] || [metadataNet.selector isEqualToString:selectorReadFileWithDownload]) {
  203. BOOL withDownload = NO;
  204. if ([metadataNet.selector isEqualToString:selectorReadFileWithDownload])
  205. withDownload = YES;
  206. //Add/Update Metadata
  207. tableMetadata *addMetadata = [[NCManageDatabase sharedInstance] addMetadata:metadata];
  208. if (addMetadata)
  209. [self verifyChangeMedatas:[[NSArray alloc] initWithObjects:addMetadata, nil] serverUrl:metadataNet.serverUrl account:appDelegate.activeAccount withDownload:withDownload];
  210. }
  211. // Selector : selectorReadFileReloadFolder, selectorReadFileFolderWithDownload
  212. if ([metadataNet.selector isEqualToString:selectorReadFileFolder] || [metadataNet.selector isEqualToString:selectorReadFileFolderWithDownload]) {
  213. NSString *serverUrl = [CCUtility stringAppendServerUrl:metadataNet.serverUrl addFileName:metadataNet.fileName];
  214. tableDirectory *tableDirectory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", metadataNet.account, serverUrl]];
  215. tableMetadata *tableMetadata = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", metadata.fileID]];
  216. // Verify changed etag OR was not favorite
  217. if (!([tableDirectory.etag isEqualToString:metadata.etag]) || (tableMetadata == nil || tableMetadata.favorite == NO)) {
  218. if ([metadataNet.selector isEqualToString:selectorReadFileFolder])
  219. [self readFolder:serverUrl selector:selectorReadFolder];
  220. if ([metadataNet.selector isEqualToString:selectorReadFileFolderWithDownload])
  221. [self readFolder:serverUrl selector:selectorReadFolderWithDownload];
  222. }
  223. }
  224. });
  225. } else {
  226. // Selector : selectorReadFile, selectorReadFileWithDownload
  227. if ([metadataNet.selector isEqualToString:selectorReadFile] || [metadataNet.selector isEqualToString:selectorReadFileWithDownload]) {
  228. // File not present, remove it
  229. if (errorCode == 404) {
  230. [[NCManageDatabase sharedInstance] deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", metadataNet.fileID] clearDateReadDirectoryID:nil];
  231. [[NCManageDatabase sharedInstance] deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", metadataNet.fileID]];
  232. [[NCManageDatabase sharedInstance] deletePhotosWithFileID:metadataNet.fileID];
  233. [appDelegate.activePhotos.fileIDHide addObject:metadataNet.fileID];
  234. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadataNet.directoryID];
  235. if (serverUrl)
  236. [appDelegate.activeMain reloadDatasource:serverUrl];
  237. }
  238. }
  239. }
  240. }
  241. #pragma --------------------------------------------------------------------------------------------
  242. #pragma mark ===== Verify Metadatas =====
  243. #pragma --------------------------------------------------------------------------------------------
  244. // MULTI THREAD
  245. - (void)verifyChangeMedatas:(NSArray *)allRecordMetadatas serverUrl:(NSString *)serverUrl account:(NSString *)account withDownload:(BOOL)withDownload
  246. {
  247. NSMutableArray *metadatas = [[NSMutableArray alloc] init];
  248. for (tableMetadata *metadata in allRecordMetadatas) {
  249. BOOL changeRev = NO;
  250. // change account
  251. if ([metadata.account isEqualToString:account] == NO)
  252. return;
  253. // no dir
  254. if (metadata.directory)
  255. continue;
  256. tableLocalFile *localFile = [[NCManageDatabase sharedInstance] getTableLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID == %@", metadata.fileID]];
  257. if (withDownload) {
  258. if (![localFile.etag isEqualToString:metadata.etag])
  259. changeRev = YES;
  260. } else {
  261. if (localFile && ![localFile.etag isEqualToString:metadata.etag]) // it must be in TableRecord
  262. changeRev = YES;
  263. }
  264. if (changeRev) {
  265. // remove & re-create
  266. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryProviderStorageFileID:metadata.fileID] error:nil];
  267. [CCUtility getDirectoryProviderStorageFileID:metadata.fileID fileName:metadata.fileNameView];
  268. [metadatas addObject:metadata];
  269. }
  270. }
  271. if ([metadatas count])
  272. [self SynchronizeMetadatas:metadatas withDownload:withDownload];
  273. }
  274. // MULTI THREAD
  275. - (void)SynchronizeMetadatas:(NSArray *)metadatas withDownload:(BOOL)withDownload
  276. {
  277. NSString *oldDirectoryID, *serverUrl;
  278. NSMutableArray *metadataToAdd = [NSMutableArray new];
  279. for (tableMetadata *metadata in metadatas) {
  280. // Clear date for dorce refresh view
  281. if (![oldDirectoryID isEqualToString:metadata.directoryID]) {
  282. serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  283. oldDirectoryID = metadata.directoryID;
  284. if (!serverUrl)
  285. continue;
  286. [[NCManageDatabase sharedInstance] clearDateReadWithServerUrl:serverUrl directoryID:nil];
  287. }
  288. metadata.session = k_download_session;
  289. metadata.sessionError = @"";
  290. metadata.sessionSelector = selectorDownloadSynchronize;
  291. metadata.status = k_metadataStatusWaitDownload;
  292. [metadataToAdd addObject:metadata];
  293. }
  294. (void)[[NCManageDatabase sharedInstance] addMetadatas:metadataToAdd serverUrl:nil];
  295. [appDelegate performSelectorOnMainThread:@selector(loadAutoDownloadUpload) withObject:nil waitUntilDone:YES];
  296. dispatch_async(dispatch_get_main_queue(), ^{
  297. [appDelegate.activeMain reloadDatasource:serverUrl];
  298. });
  299. }
  300. @end