CCSynchronize.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. //
  2. // CCSynchronize.m
  3. // Crypto Cloud Technology Nextcloud
  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. // local
  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. }
  41. return sharedSynchronize;
  42. }
  43. }
  44. #pragma --------------------------------------------------------------------------------------------
  45. #pragma mark ===== Read Folder =====
  46. #pragma --------------------------------------------------------------------------------------------
  47. // serverUrl : start
  48. // directoryID : start
  49. // selector : selectorReadFolder, selectorReadFolderWithDownload
  50. //
  51. - (void)synchronizedFolder:(NSString *)serverUrl selector:(NSString *)selector
  52. {
  53. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  54. metadataNet.action = actionReadFolder;
  55. metadataNet.directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:serverUrl];
  56. metadataNet.priority = NSOperationQueuePriorityLow;
  57. metadataNet.selector = selector;
  58. metadataNet.serverUrl = serverUrl;
  59. [app addNetworkingOperationQueue:app.netQueue delegate:self metadataNet:metadataNet];
  60. NSLog(@"[LOG] %@ directory : %@", selector, serverUrl);
  61. }
  62. - (void)readFolderFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  63. {
  64. // verify active user
  65. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  66. // Folder not present, remove it
  67. if (errorCode == 404 && [recordAccount.account isEqualToString:metadataNet.account]) {
  68. [[NCManageDatabase sharedInstance] deleteDirectoryAndSubDirectoryWithServerUrl:metadataNet.serverUrl];
  69. [app.activeMain reloadDatasource:metadataNet.serverUrl];
  70. }
  71. }
  72. // MULTI THREAD
  73. - (void)readFolderSuccess:(CCMetadataNet *)metadataNet metadataFolder:(tableMetadata *)metadataFolder metadatas:(NSArray *)metadatas
  74. {
  75. // Add/update self Folder
  76. (void)[[NCManageDatabase sharedInstance] addMetadata:metadataFolder];
  77. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  78. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  79. NSMutableArray *metadatasForVerifyChange = [NSMutableArray new];
  80. NSMutableArray *addMetadatas = [NSMutableArray new];
  81. if ([recordAccount.account isEqualToString:metadataNet.account] == NO)
  82. return;
  83. NSArray *recordsInSessions = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND directoryID = %@ AND session != ''", app.activeAccount, metadataNet.directoryID] sorted:nil ascending:NO];
  84. // ----- Test : (DELETE) -----
  85. NSMutableArray *metadatasNotPresents = [[NSMutableArray alloc] init];
  86. NSArray *tableMetadatas = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND directoryID = %@ AND session = ''", app.activeAccount, metadataNet.directoryID] sorted:nil ascending:NO];
  87. for (tableMetadata *record in tableMetadatas) {
  88. // reject cryptated
  89. if (record.cryptated)
  90. continue;
  91. BOOL fileIDFound = NO;
  92. for (tableMetadata *metadata in metadatas) {
  93. if ([record.fileID isEqualToString:metadata.fileID]) {
  94. fileIDFound = YES;
  95. break;
  96. }
  97. }
  98. if (!fileIDFound)
  99. [metadatasNotPresents addObject:record];
  100. }
  101. // delete metadata not present
  102. for (tableMetadata *metadata in metadatasNotPresents) {
  103. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileID] error:nil];
  104. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID] error:nil];
  105. if (metadata.directory && metadataNet.serverUrl) {
  106. NSString *dirForDelete = [CCUtility stringAppendServerUrl:metadataNet.serverUrl addFileName:metadata.fileNameData];
  107. [[NCManageDatabase sharedInstance] deleteDirectoryAndSubDirectoryWithServerUrl:dirForDelete];
  108. }
  109. [[NCManageDatabase sharedInstance] deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", metadata.fileID] clearDateReadDirectoryID:nil];
  110. [[NCManageDatabase sharedInstance] deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", metadata.fileID]];
  111. }
  112. dispatch_async(dispatch_get_main_queue(), ^{
  113. if ([metadatasNotPresents count] > 0)
  114. [app.activeMain reloadDatasource:metadataNet.serverUrl];
  115. });
  116. // ----- Test : (MODIFY) -----
  117. for (tableMetadata *metadata in metadatas) {
  118. // reject cryptated
  119. if (metadata.cryptated)
  120. continue;
  121. // dir recursive
  122. if (metadata.directory) {
  123. NSString *serverUrl = [CCUtility stringAppendServerUrl:metadataNet.serverUrl addFileName:metadata.fileNameData];
  124. NSString *etag = metadata.etag;
  125. // Verify if do not exists this Metadata
  126. tableMetadata *result = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", metadata.fileID]];
  127. if (!result)
  128. (void)[[NCManageDatabase sharedInstance] addMetadata:metadata];
  129. // Load if different etag
  130. tableDirectory *tableDirectory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND serverUrl = %@", metadataNet.account, serverUrl]];
  131. if (![tableDirectory.etag isEqualToString:etag]) {
  132. [[NCManageDatabase sharedInstance] setDirectoryWithServerUrl:serverUrl serverUrlTo:nil etag:etag];
  133. [self synchronizedFolder: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 =====
  166. #pragma --------------------------------------------------------------------------------------------
  167. - (void)synchronizedFile:(tableMetadata *)metadata selector:(NSString *)selector
  168. {
  169. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  170. if (serverUrl == nil) return;
  171. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  172. metadataNet.action = actionReadFile;
  173. metadataNet.fileID = metadata.fileID;
  174. metadataNet.fileName = metadata.fileName;
  175. metadataNet.fileNamePrint = metadata.fileNamePrint;
  176. metadataNet.priority = NSOperationQueuePriorityLow;
  177. metadataNet.selector = selector;
  178. metadataNet.serverUrl = serverUrl;
  179. [app addNetworkingOperationQueue:app.netQueue delegate:self metadataNet:metadataNet];
  180. }
  181. - (void)readFileFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  182. {
  183. // verify active user
  184. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  185. // File not present, remove it
  186. if (errorCode == 404 && [recordAccount.account isEqualToString:metadataNet.account]) {
  187. [[NCManageDatabase sharedInstance] deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", metadataNet.fileID]];
  188. [[NCManageDatabase sharedInstance] deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", metadataNet.account, metadataNet.fileID] clearDateReadDirectoryID:nil];
  189. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadataNet.directoryID];
  190. [app.activeMain reloadDatasource:serverUrl];
  191. }
  192. }
  193. - (void)readFileSuccess:(CCMetadataNet *)metadataNet metadata:(tableMetadata *)metadata
  194. {
  195. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
  196. BOOL withDownload = NO;
  197. if ([metadataNet.selector isEqualToString:selectorReadFileWithDownload])
  198. withDownload = YES;
  199. //Add/Update Metadata
  200. tableMetadata *addMetadata = [[NCManageDatabase sharedInstance] addMetadata:metadata];
  201. if (addMetadata)
  202. [self verifyChangeMedatas:[[NSArray alloc] initWithObjects:addMetadata, nil] serverUrl:metadataNet.serverUrl account:app.activeAccount withDownload:withDownload];
  203. });
  204. }
  205. #pragma --------------------------------------------------------------------------------------------
  206. #pragma mark ===== Verify Metadatas =====
  207. #pragma --------------------------------------------------------------------------------------------
  208. // MULTI THREAD
  209. - (void)verifyChangeMedatas:(NSArray *)allRecordMetadatas serverUrl:(NSString *)serverUrl account:(NSString *)account withDownload:(BOOL)withDownload
  210. {
  211. NSMutableArray *metadatas = [[NSMutableArray alloc] init];
  212. for (tableMetadata *metadata in allRecordMetadatas) {
  213. BOOL changeRev = NO;
  214. // change account
  215. if ([metadata.account isEqualToString:account] == NO)
  216. return;
  217. // no dir
  218. if (metadata.directory)
  219. continue;
  220. tableLocalFile *localFile = [[NCManageDatabase sharedInstance] getTableLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", metadata.fileID]];
  221. if (withDownload) {
  222. if (![localFile.etag isEqualToString:metadata.etag])
  223. changeRev = YES;
  224. } else {
  225. if (localFile && ![localFile.etag isEqualToString:metadata.etag]) // it must be in TableRecord
  226. changeRev = YES;
  227. }
  228. if (changeRev) {
  229. if ([metadata.type isEqualToString: k_metadataType_file]) {
  230. // remove file and ico
  231. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileID] error:nil];
  232. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID] error:nil];
  233. }
  234. if ([metadata.type isEqualToString: k_metadataType_template]) {
  235. // remove model
  236. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileName] error:nil];
  237. }
  238. [metadatas addObject:metadata];
  239. }
  240. }
  241. if ([metadatas count])
  242. [self SynchronizeMetadatas:metadatas withDownload:withDownload];
  243. }
  244. // MULTI THREAD
  245. - (void)SynchronizeMetadatas:(NSArray *)metadatas withDownload:(BOOL)withDownload
  246. {
  247. NSString *oldDirectoryID, *serverUrl, *fileID;
  248. for (tableMetadata *metadata in metadatas) {
  249. NSString *selector, *selectorPost;
  250. BOOL downloadData = NO, downloadPlist = NO;
  251. if ([metadata.type isEqualToString: k_metadataType_file]) {
  252. downloadData = YES;
  253. selector = selectorDownloadSynchronize;
  254. }
  255. if ([metadata.type isEqualToString: k_metadataType_template]) {
  256. downloadPlist = YES;
  257. selector = selectorLoadPlist;
  258. }
  259. // Clear date for dorce refresh view
  260. if (![oldDirectoryID isEqualToString:metadata.directoryID]) {
  261. serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  262. oldDirectoryID = metadata.directoryID;
  263. [[NCManageDatabase sharedInstance] clearDateReadWithServerUrl:serverUrl directoryID:nil];
  264. }
  265. fileID = metadata.fileID;
  266. (void)[[NCManageDatabase sharedInstance] addMetadata:metadata];
  267. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  268. metadataNet.action = actionDownloadFile;
  269. metadataNet.downloadData = downloadData;
  270. metadataNet.downloadPlist = downloadPlist;
  271. metadataNet.fileID = fileID;
  272. metadataNet.priority = NSOperationQueuePriorityLow;
  273. metadataNet.selector = selector;
  274. metadataNet.selectorPost = selectorPost;
  275. metadataNet.serverUrl = serverUrl;
  276. metadataNet.session = k_download_session;
  277. metadataNet.taskStatus = k_taskStatusResume;
  278. [app addNetworkingOperationQueue:app.netQueueDownload delegate:app.activeMain metadataNet:metadataNet];
  279. }
  280. dispatch_async(dispatch_get_main_queue(), ^{
  281. [app.activeMain reloadDatasource:serverUrl];
  282. });
  283. }
  284. @end