CCSynchronize.m 15 KB

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