CCSynchronize.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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)readFolderServerUrl:(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 = NSOperationQueuePriorityNormal;
  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 fileID:nil selector:nil];
  70. }
  71. }
  72. // MULTI THREAD
  73. - (void)readFolderSuccess:(CCMetadataNet *)metadataNet permissions:(NSString *)permissions etag:(NSString *)etag metadatas:(NSArray *)metadatas
  74. {
  75. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  76. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  77. NSMutableArray *metadatasForVerifyChange = [NSMutableArray new];
  78. if ([recordAccount.account isEqualToString:metadataNet.account] == NO)
  79. return;
  80. NSArray *recordsInSessions = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND directoryID = %@ AND session != ''", app.activeAccount, metadataNet.directoryID] sorted:nil ascending:NO];
  81. // ----- Test : (DELETE) -----
  82. NSMutableArray *metadatasNotPresents = [[NSMutableArray alloc] init];
  83. NSArray *tableMetadatas = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND directoryID = %@ AND session = ''", app.activeAccount, metadataNet.directoryID] sorted:nil ascending:NO];
  84. for (tableMetadata *record in tableMetadatas) {
  85. // reject cryptated
  86. if (record.cryptated)
  87. continue;
  88. BOOL fileIDFound = NO;
  89. for (tableMetadata *metadata in metadatas) {
  90. if ([record.fileID isEqualToString:metadata.fileID]) {
  91. fileIDFound = YES;
  92. break;
  93. }
  94. }
  95. if (!fileIDFound)
  96. [metadatasNotPresents addObject:record];
  97. }
  98. // delete metadata not present
  99. for (tableMetadata *metadata in metadatasNotPresents) {
  100. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileID] error:nil];
  101. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID] error:nil];
  102. if (metadata.directory && metadataNet.serverUrl) {
  103. NSString *dirForDelete = [CCUtility stringAppendServerUrl:metadataNet.serverUrl addFileName:metadata.fileNameData];
  104. [[NCManageDatabase sharedInstance] deleteDirectoryAndSubDirectoryWithServerUrl:dirForDelete];
  105. }
  106. [[NCManageDatabase sharedInstance] deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", metadata.fileID] clearDateReadDirectoryID:nil];
  107. [[NCManageDatabase sharedInstance] deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", metadata.fileID]];
  108. }
  109. dispatch_async(dispatch_get_main_queue(), ^{
  110. if ([metadatasNotPresents count] > 0)
  111. [app.activeMain reloadDatasource:metadataNet.serverUrl fileID:nil selector:nil];
  112. });
  113. // ----- Test : (MODIFY) -----
  114. for (tableMetadata *metadata in metadatas) {
  115. // reject cryptated
  116. if (metadata.cryptated)
  117. continue;
  118. // dir recursive
  119. if (metadata.directory) {
  120. NSString *serverUrl = [CCUtility stringAppendServerUrl:metadataNet.serverUrl addFileName:metadata.fileNameData];
  121. NSString *etag = metadata.etag;
  122. // Verify if do not exists this Metadata
  123. tableMetadata *result = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", metadata.fileID]];
  124. if (!result)
  125. (void)[[NCManageDatabase sharedInstance] addMetadata:metadata activeUrl:app.activeUrl serverUrl:metadataNet.serverUrl];
  126. // Load if different etag
  127. tableDirectory *tableDirectory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND serverUrl = %@", metadataNet.account, serverUrl]];
  128. if (![tableDirectory.etag isEqualToString:etag]) {
  129. [[NCManageDatabase sharedInstance] setDirectoryWithServerUrl:serverUrl serverUrlTo:nil etag:etag];
  130. [self readFolderServerUrl:serverUrl selector:metadataNet.selector];
  131. }
  132. } else {
  133. if ([metadataNet.selector isEqualToString:selectorReadFolderWithDownload]) {
  134. // It's in session
  135. BOOL recordInSession = NO;
  136. for (tableMetadata *record in recordsInSessions) {
  137. if ([record.fileID isEqualToString:metadata.fileID]) {
  138. recordInSession = YES;
  139. break;
  140. }
  141. }
  142. if (recordInSession)
  143. continue;
  144. // Ohhhh INSERT
  145. [metadatasForVerifyChange addObject:metadata];
  146. }
  147. if ([metadataNet.selector isEqualToString:selectorReadFolder]) {
  148. // Verify if do not exists this Metadata
  149. tableMetadata *result = [[NCManageDatabase sharedInstance] getMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", metadata.fileID]];
  150. if (!result)
  151. (void)[[NCManageDatabase sharedInstance] addMetadata:metadata activeUrl:metadataNet.serverUrl serverUrl:metadataNet.serverUrl];
  152. }
  153. }
  154. }
  155. if ([metadatasForVerifyChange count] > 0)
  156. [self verifyChangeMedatas:metadatasForVerifyChange serverUrl:metadataNet.serverUrl account:metadataNet.account withDownload:YES];
  157. });
  158. }
  159. #pragma --------------------------------------------------------------------------------------------
  160. #pragma mark ===== Read File =====
  161. #pragma --------------------------------------------------------------------------------------------
  162. - (void)readFile:(tableMetadata *)metadata withDownload:(BOOL)withDownload
  163. {
  164. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  165. if (serverUrl == nil) return;
  166. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  167. metadataNet.action = actionReadFile;
  168. metadataNet.fileID = metadata.fileID;
  169. metadataNet.fileName = metadata.fileName;
  170. metadataNet.fileNamePrint = metadata.fileNamePrint;
  171. metadataNet.options = [NSNumber numberWithBool:withDownload] ;
  172. metadataNet.priority = NSOperationQueuePriorityLow;
  173. metadataNet.selector = selectorReadFile;
  174. metadataNet.serverUrl = serverUrl;
  175. [app addNetworkingOperationQueue:app.netQueue delegate:self metadataNet:metadataNet];
  176. }
  177. - (void)readFileFailure:(CCMetadataNet *)metadataNet message:(NSString *)message errorCode:(NSInteger)errorCode
  178. {
  179. // verify active user
  180. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  181. // File not present, remove it
  182. if (errorCode == 404 && [recordAccount.account isEqualToString:metadataNet.account]) {
  183. [[NCManageDatabase sharedInstance] deleteLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", metadataNet.fileID]];
  184. [[NCManageDatabase sharedInstance] deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", metadataNet.account, metadataNet.fileID] clearDateReadDirectoryID:nil];
  185. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadataNet.directoryID];
  186. [app.activeMain reloadDatasource:serverUrl fileID:nil selector:nil];
  187. }
  188. }
  189. - (void)readFileSuccess:(CCMetadataNet *)metadataNet metadata:(tableMetadata *)metadata
  190. {
  191. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
  192. BOOL withDownload = [metadataNet.options boolValue];
  193. [self verifyChangeMedatas:[[NSArray alloc] initWithObjects:metadata, nil] serverUrl:metadataNet.serverUrl account:app.activeAccount withDownload:withDownload];
  194. });
  195. }
  196. #pragma --------------------------------------------------------------------------------------------
  197. #pragma mark ===== Verify Metadatas =====
  198. #pragma --------------------------------------------------------------------------------------------
  199. // MULTI THREAD
  200. - (void)verifyChangeMedatas:(NSArray *)allRecordMetadatas serverUrl:(NSString *)serverUrl account:(NSString *)account withDownload:(BOOL)withDownload
  201. {
  202. NSMutableArray *metadatas = [[NSMutableArray alloc] init];
  203. for (tableMetadata *metadata in allRecordMetadatas) {
  204. BOOL changeRev = NO;
  205. // change account
  206. if ([metadata.account isEqualToString:account] == NO)
  207. return;
  208. // no dir
  209. if (metadata.directory)
  210. continue;
  211. tableLocalFile *localFile = [[NCManageDatabase sharedInstance] getTableLocalFileWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", metadata.fileID]];
  212. if (withDownload) {
  213. if (![localFile.etag isEqualToString:metadata.etag])
  214. changeRev = YES;
  215. } else {
  216. if (localFile && ![localFile.etag isEqualToString:metadata.etag]) // it must be in TableRecord
  217. changeRev = YES;
  218. }
  219. if (changeRev) {
  220. if ([metadata.type isEqualToString: k_metadataType_file]) {
  221. // remove file and ico
  222. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileID] error:nil];
  223. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@.ico", app.directoryUser, metadata.fileID] error:nil];
  224. }
  225. if ([metadata.type isEqualToString: k_metadataType_template]) {
  226. // remove model
  227. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", app.directoryUser, metadata.fileName] error:nil];
  228. }
  229. [metadatas addObject:metadata];
  230. }
  231. }
  232. if ([metadatas count])
  233. [self SynchronizeMetadatas:metadatas withDownload:withDownload];
  234. }
  235. // MULTI THREAD
  236. - (void)SynchronizeMetadatas:(NSArray *)metadatas withDownload:(BOOL)withDownload
  237. {
  238. NSString *oldDirectoryID, *serverUrl, *fileID;
  239. for (tableMetadata *metadata in metadatas) {
  240. NSString *selector, *selectorPost;
  241. BOOL downloadData = NO, downloadPlist = NO;
  242. if ([metadata.type isEqualToString: k_metadataType_file]) {
  243. downloadData = YES;
  244. selector = selectorDownloadSynchronize;
  245. }
  246. if ([metadata.type isEqualToString: k_metadataType_template]) {
  247. downloadPlist = YES;
  248. selector = selectorLoadPlist;
  249. }
  250. // Clear date for dorce refresh view
  251. if (![oldDirectoryID isEqualToString:metadata.directoryID]) {
  252. serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:metadata.directoryID];
  253. oldDirectoryID = metadata.directoryID;
  254. [[NCManageDatabase sharedInstance] clearDateReadWithServerUrl:serverUrl directoryID:nil];
  255. }
  256. fileID = metadata.fileID;
  257. (void)[[NCManageDatabase sharedInstance] addMetadata:metadata activeUrl:serverUrl serverUrl:serverUrl];
  258. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  259. metadataNet.action = actionDownloadFile;
  260. metadataNet.downloadData = downloadData;
  261. metadataNet.downloadPlist = downloadPlist;
  262. metadataNet.fileID = fileID;
  263. metadataNet.selector = selector;
  264. metadataNet.selectorPost = selectorPost;
  265. metadataNet.serverUrl = serverUrl;
  266. metadataNet.session = k_download_session;
  267. metadataNet.taskStatus = k_taskStatusResume;
  268. [app addNetworkingOperationQueue:app.netQueueDownload delegate:app.activeMain metadataNet:metadataNet];
  269. }
  270. dispatch_async(dispatch_get_main_queue(), ^{
  271. [app.activeMain reloadDatasource:serverUrl fileID:nil selector:nil];
  272. });
  273. }
  274. @end