CCSynchronize.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
  88. NSMutableArray *metadatasForVerifyChange = [NSMutableArray new];
  89. NSMutableArray *addMetadatas = [NSMutableArray new];
  90. NSArray *recordsInSessions = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND directoryID = %@ AND session != ''", appDelegate.activeAccount, metadataNet.directoryID] sorted:nil ascending:NO];
  91. // ----- Test : (DELETE) -----
  92. NSMutableArray *metadatasNotPresents = [NSMutableArray new];
  93. NSArray *tableMetadatas = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND directoryID = %@ AND session = ''", appDelegate.activeAccount, metadataNet.directoryID] sorted:nil ascending:NO];
  94. for (tableMetadata *record in tableMetadatas) {
  95. BOOL fileIDFound = NO;
  96. for (tableMetadata *metadata in metadatas) {
  97. if ([record.fileID isEqualToString:metadata.fileID]) {
  98. fileIDFound = YES;
  99. break;
  100. }
  101. }
  102. if (!fileIDFound)
  103. [metadatasNotPresents addObject:record];
  104. }
  105. // delete metadata not present
  106. for (tableMetadata *metadata in metadatasNotPresents) {
  107. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", appDelegate.directoryUser, metadata.fileID] error:nil];
  108. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.ico", appDelegate.directoryUser, metadata.fileID] error:nil];
  109. if (metadata.directory && metadataNet.serverUrl) {
  110. NSString *dirForDelete = [CCUtility stringAppendServerUrl:metadataNet.serverUrl addFileName:metadata.fileName];
  111. [[NCManageDatabase sharedInstance] deleteDirectoryAndSubDirectoryWithServerUrl:dirForDelete];
  112. }
  113. [[NCManageDatabase sharedInstance] deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", metadata.fileID] clearDateReadDirectoryID:nil];
  114. [[NCManageDatabase sharedInstance] deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", metadata.fileID]];
  115. }
  116. dispatch_async(dispatch_get_main_queue(), ^{
  117. if ([metadatasNotPresents count] > 0)
  118. [appDelegate.activeMain reloadDatasource:metadataNet.serverUrl];
  119. });
  120. // ----- Test : (MODIFY) -----
  121. for (tableMetadata *metadata in metadatas) {
  122. // RECURSIVE DIRECTORY MODE
  123. if (metadata.directory) {
  124. NSString *serverUrl = [CCUtility stringAppendServerUrl:metadataNet.serverUrl addFileName:metadata.fileName];
  125. NSString *etag = metadata.etag;
  126. // Verify if do not exists this Metadata
  127. tableMetadata *result = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", metadata.fileID]];
  128. if (!result)
  129. (void)[[NCManageDatabase sharedInstance] addMetadata:metadata];
  130. // Load if different etag
  131. tableDirectory *tableDirectory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND serverUrl = %@", metadataNet.account, serverUrl]];
  132. if (![tableDirectory.etag isEqualToString:etag] || [metadataNet.selector isEqualToString:selectorReadFolderWithDownload]) {
  133. [self readFolder:serverUrl selector:metadataNet.selector];
  134. }
  135. } else {
  136. if ([metadataNet.selector isEqualToString:selectorReadFolderWithDownload]) {
  137. // It's in session
  138. BOOL recordInSession = NO;
  139. for (tableMetadata *record in recordsInSessions) {
  140. if ([record.fileID isEqualToString:metadata.fileID]) {
  141. recordInSession = YES;
  142. break;
  143. }
  144. }
  145. if (recordInSession)
  146. continue;
  147. // Ohhhh INSERT
  148. [metadatasForVerifyChange addObject:metadata];
  149. }
  150. if ([metadataNet.selector isEqualToString:selectorReadFolder]) {
  151. // Verify if do not exists this Metadata
  152. tableMetadata *result = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", metadata.fileID]];
  153. if (!result)
  154. [addMetadatas addObject:metadata];
  155. }
  156. }
  157. }
  158. if ([addMetadatas count] > 0)
  159. (void)[[NCManageDatabase sharedInstance] addMetadatas:addMetadatas serverUrl:metadataNet.serverUrl];
  160. if ([metadatasForVerifyChange count] > 0)
  161. [self verifyChangeMedatas:metadatasForVerifyChange serverUrl:metadataNet.serverUrl account:metadataNet.account withDownload:YES];
  162. });
  163. }
  164. #pragma --------------------------------------------------------------------------------------------
  165. #pragma mark ===== Read File for Folder & Read File=====
  166. #pragma --------------------------------------------------------------------------------------------
  167. - (void)readFileForFolder:(NSString *)fileName serverUrl:(NSString *)serverUrl selector:(NSString *)selector
  168. {
  169. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:appDelegate.activeAccount];
  170. metadataNet.action = actionReadFile;
  171. metadataNet.fileName = fileName;
  172. metadataNet.priority = NSOperationQueuePriorityLow;
  173. metadataNet.selector = selector;
  174. metadataNet.serverUrl = serverUrl;
  175. [appDelegate addNetworkingOperationQueue:appDelegate.netQueue delegate:self metadataNet:metadataNet];
  176. }
  177. - (void)readFile:(tableMetadata *)metadata selector:(NSString *)selector
  178. {
  179. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  180. if (!serverUrl) return;
  181. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:appDelegate.activeAccount];
  182. metadataNet.action = actionReadFile;
  183. metadataNet.fileID = metadata.fileID;
  184. metadataNet.fileName = metadata.fileName;
  185. metadataNet.priority = NSOperationQueuePriorityLow;
  186. metadataNet.selector = selector;
  187. metadataNet.serverUrl = serverUrl;
  188. [appDelegate addNetworkingOperationQueue:appDelegate.netQueue delegate:self metadataNet:metadataNet];
  189. }
  190. - (void)readFileSuccessFailure:(CCMetadataNet *)metadataNet metadata:(tableMetadata *)metadata message:(NSString *)message errorCode:(NSInteger)errorCode
  191. {
  192. // Check Active Account
  193. if (![metadataNet.account isEqualToString:appDelegate.activeAccount])
  194. return;
  195. if (errorCode == 0) {
  196. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
  197. // Selector : selectorReadFile, selectorReadFileWithDownload
  198. if ([metadataNet.selector isEqualToString:selectorReadFile] || [metadataNet.selector isEqualToString:selectorReadFileWithDownload]) {
  199. BOOL withDownload = NO;
  200. if ([metadataNet.selector isEqualToString:selectorReadFileWithDownload])
  201. withDownload = YES;
  202. //Add/Update Metadata
  203. tableMetadata *addMetadata = [[NCManageDatabase sharedInstance] addMetadata:metadata];
  204. if (addMetadata)
  205. [self verifyChangeMedatas:[[NSArray alloc] initWithObjects:addMetadata, nil] serverUrl:metadataNet.serverUrl account:appDelegate.activeAccount withDownload:withDownload];
  206. }
  207. // Selector : selectorReadFileReloadFolder, selectorReadFileFolderWithDownload
  208. if ([metadataNet.selector isEqualToString:selectorReadFileFolder] || [metadataNet.selector isEqualToString:selectorReadFileFolderWithDownload]) {
  209. NSString *serverUrl = [CCUtility stringAppendServerUrl:metadataNet.serverUrl addFileName:metadataNet.fileName];
  210. tableDirectory *tableDirectory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND serverUrl = %@", metadataNet.account, serverUrl]];
  211. // Verify changed etag
  212. if (![tableDirectory.etag isEqualToString:metadata.etag] && tableDirectory) {
  213. if ([metadataNet.selector isEqualToString:selectorReadFileFolder])
  214. [self readFolder:serverUrl selector:selectorReadFolder];
  215. if ([metadataNet.selector isEqualToString:selectorReadFileFolderWithDownload])
  216. [self readFolder:serverUrl selector:selectorReadFolderWithDownload];
  217. }
  218. }
  219. });
  220. } else {
  221. // Selector : selectorReadFile, selectorReadFileWithDownload
  222. if ([metadataNet.selector isEqualToString:selectorReadFile] || [metadataNet.selector isEqualToString:selectorReadFileWithDownload]) {
  223. // File not present, remove it
  224. if (errorCode == 404) {
  225. [[NCManageDatabase sharedInstance] deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", metadataNet.fileID]];
  226. [[NCManageDatabase sharedInstance] deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", metadataNet.account, metadataNet.fileID] clearDateReadDirectoryID:nil];
  227. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadataNet.directoryID];
  228. if (serverUrl)
  229. [appDelegate.activeMain reloadDatasource:serverUrl];
  230. }
  231. }
  232. }
  233. }
  234. #pragma --------------------------------------------------------------------------------------------
  235. #pragma mark ===== Verify Metadatas =====
  236. #pragma --------------------------------------------------------------------------------------------
  237. // MULTI THREAD
  238. - (void)verifyChangeMedatas:(NSArray *)allRecordMetadatas serverUrl:(NSString *)serverUrl account:(NSString *)account withDownload:(BOOL)withDownload
  239. {
  240. NSMutableArray *metadatas = [[NSMutableArray alloc] init];
  241. for (tableMetadata *metadata in allRecordMetadatas) {
  242. BOOL changeRev = NO;
  243. // change account
  244. if ([metadata.account isEqualToString:account] == NO)
  245. return;
  246. // no dir
  247. if (metadata.directory)
  248. continue;
  249. tableLocalFile *localFile = [[NCManageDatabase sharedInstance] getTableLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", metadata.fileID]];
  250. if (withDownload) {
  251. if (![localFile.etag isEqualToString:metadata.etag])
  252. changeRev = YES;
  253. } else {
  254. if (localFile && ![localFile.etag isEqualToString:metadata.etag]) // it must be in TableRecord
  255. changeRev = YES;
  256. }
  257. if (changeRev) {
  258. // remove file and ico
  259. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", appDelegate.directoryUser, metadata.fileID] error:nil];
  260. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.ico", appDelegate.directoryUser, metadata.fileID] error:nil];
  261. [metadatas addObject:metadata];
  262. }
  263. }
  264. if ([metadatas count])
  265. [self SynchronizeMetadatas:metadatas withDownload:withDownload];
  266. }
  267. // MULTI THREAD
  268. - (void)SynchronizeMetadatas:(NSArray *)metadatas withDownload:(BOOL)withDownload
  269. {
  270. NSString *oldDirectoryID, *serverUrl, *fileID;
  271. NSMutableArray *metadataToAdd = [NSMutableArray new];
  272. NSMutableArray *metadataNetToAdd = [NSMutableArray new];
  273. for (tableMetadata *metadata in metadatas) {
  274. NSString *selector, *selectorPost;
  275. CCMetadataNet *metadataNet = [CCMetadataNet new];
  276. selector = selectorDownloadSynchronize;
  277. // Clear date for dorce refresh view
  278. if (![oldDirectoryID isEqualToString:metadata.directoryID]) {
  279. serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  280. oldDirectoryID = metadata.directoryID;
  281. if (!serverUrl)
  282. continue;
  283. [[NCManageDatabase sharedInstance] clearDateReadWithServerUrl:serverUrl directoryID:nil];
  284. }
  285. fileID = metadata.fileID;
  286. [metadataToAdd addObject:metadata];
  287. metadataNet.fileID = fileID;
  288. metadataNet.selector = selector;
  289. metadataNet.selectorPost = selectorPost;
  290. metadataNet.serverUrl = serverUrl;
  291. metadataNet.session = k_download_session;
  292. [metadataNetToAdd addObject:metadataNet];
  293. }
  294. (void)[[NCManageDatabase sharedInstance] addMetadatas:metadataToAdd serverUrl:nil];
  295. [[NCManageDatabase sharedInstance] addQueueDownloadWithMetadatasNet:metadataNetToAdd];
  296. dispatch_async(dispatch_get_main_queue(), ^{
  297. [appDelegate.activeMain reloadDatasource:serverUrl];
  298. });
  299. }
  300. @end