NCNetworkingSync.m 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. //
  2. // NCNetworkingSync.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 29/10/17.
  6. // Copyright © 2017 TWS. All rights reserved.
  7. //
  8. #import "NCNetworkingSync.h"
  9. #import "CCUtility.h"
  10. #import "CCCertificate.h"
  11. #import "NCBridgeSwift.h"
  12. @implementation NCNetworkingSync
  13. + (NCNetworkingSync *)sharedManager {
  14. static NCNetworkingSync *sharedManager;
  15. @synchronized(self)
  16. {
  17. if (!sharedManager) {
  18. sharedManager = [NCNetworkingSync new];
  19. }
  20. return sharedManager;
  21. }
  22. }
  23. #pragma --------------------------------------------------------------------------------------------
  24. #pragma mark ============================
  25. #pragma --------------------------------------------------------------------------------------------
  26. - (NSError *)uploadFile:(NSString *)localFilePathName remoteFilePathName:(NSString *)remoteFilePathName user:(NSString *)user userID:(NSString *)userID password:(NSString *)password
  27. {
  28. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  29. __block NSError *returnError = nil;
  30. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  31. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  32. [communication setUserAgent:[CCUtility getUserAgent]];
  33. [communication uploadFileSession:localFilePathName toDestiny:remoteFilePathName onCommunication:communication progress:^(NSProgress *progress) {
  34. // Progress
  35. } successRequest:^(NSURLResponse *response, NSString *redirectedServer) {
  36. dispatch_semaphore_signal(semaphore);
  37. } failureRequest:^(NSURLResponse *response, NSString *redirectedServer, NSError *error) {
  38. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  39. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:httpResponse.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Upload file error %lu", (unsigned long)httpResponse.statusCode] forKey:NSLocalizedDescriptionKey]];
  40. dispatch_semaphore_signal(semaphore);
  41. } failureBeforeRequest:^(NSError *error) {
  42. returnError = error;
  43. dispatch_semaphore_signal(semaphore);
  44. }];
  45. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  46. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  47. return returnError;
  48. }
  49. - (NSError *)checkServer:(NSString *)serverUrl user:(NSString *)user userID:(NSString *)userID password:(NSString *)password
  50. {
  51. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  52. __block NSError *returnError = nil;
  53. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  54. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  55. [communication setUserAgent:[CCUtility getUserAgent]];
  56. [communication checkServer:serverUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  57. dispatch_semaphore_signal(semaphore);
  58. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  59. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Check server error %lu", (unsigned long)response.statusCode] forKey:NSLocalizedDescriptionKey]];
  60. dispatch_semaphore_signal(semaphore);
  61. }];
  62. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  63. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  64. return returnError;
  65. }
  66. - (NSError *)readFile:(NSString *)filePathName user:(NSString *)user userID:(NSString *)userID password:(NSString *)password items:(NSArray **)items
  67. {
  68. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  69. __block NSError *returnError = nil;
  70. __block NSArray *returnItems = nil;
  71. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  72. [communication setCredentialsWithUser: user andUserID: userID andPassword: password];
  73. [communication setUserAgent:[CCUtility getUserAgent]];
  74. [communication readFile:filePathName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  75. returnItems = items;
  76. dispatch_semaphore_signal(semaphore);
  77. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  78. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Read file error %lu", (long) response.statusCode] forKey:NSLocalizedDescriptionKey]];
  79. dispatch_semaphore_signal(semaphore);
  80. }];
  81. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  82. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  83. *items = returnItems;
  84. return returnError;
  85. }
  86. - (NSError *)readFolder:(NSString *)serverUrl user:(NSString *)user userID:(NSString *)userID password:(NSString *)password items:(NSArray **)items
  87. {
  88. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  89. __block NSError *returnError = nil;
  90. __block NSArray *returnItems = nil;
  91. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  92. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  93. [communication setUserAgent:[CCUtility getUserAgent]];
  94. [communication readFolder:serverUrl depth:0 withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  95. returnItems = items;
  96. dispatch_semaphore_signal(semaphore);
  97. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  98. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Read folder error %lu", (unsigned long)response.statusCode] forKey:NSLocalizedDescriptionKey]];
  99. dispatch_semaphore_signal(semaphore);
  100. }];
  101. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  102. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  103. *items = returnItems;
  104. return returnError;
  105. }
  106. - (NSError *)createFolderAutomaticUpload:(NSString *)folderPathName user:(NSString *)user userID:(NSString *)userID password:(NSString *)password
  107. {
  108. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  109. __block NSError *returnError = nil;
  110. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  111. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  112. [communication setUserAgent:[CCUtility getUserAgent]];
  113. [communication readFile:folderPathName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  114. dispatch_semaphore_signal(semaphore);
  115. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  116. [communication createFolder:folderPathName onCommunication:communication withForbiddenCharactersSupported:YES successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  117. [[NCManageDatabase sharedInstance] clearDateReadWithServerUrl:[CCUtility deletingLastPathComponentFromServerUrl:folderPathName] directoryID:nil];
  118. dispatch_semaphore_signal(semaphore);
  119. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  120. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Read folder error %lu", (unsigned long)response.statusCode] forKey:NSLocalizedDescriptionKey]];
  121. dispatch_semaphore_signal(semaphore);
  122. } errorBeforeRequest:^(NSError *error) {
  123. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Read folder error %lu", (unsigned long)response.statusCode] forKey:NSLocalizedDescriptionKey]];
  124. dispatch_semaphore_signal(semaphore);
  125. }];
  126. }];
  127. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  128. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  129. return returnError;
  130. }
  131. #pragma --------------------------------------------------------------------------------------------
  132. #pragma mark ===== End-to-End Encryption =====
  133. #pragma --------------------------------------------------------------------------------------------
  134. // E2E
  135. - (NSError *)markEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID serverUrl:(NSString *)serverUrl token:(NSString **)token
  136. {
  137. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  138. __block NSError *returnError = nil;
  139. __block NSString *returnToken = *token;
  140. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  141. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  142. [communication setUserAgent:[CCUtility getUserAgent]];
  143. // Read Folder
  144. [communication readFolder:serverUrl depth:@"1" withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *tokenReadFolder) {
  145. if (items.count > 1) {
  146. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:999 userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"The directory is not empty", nil) forKey:NSLocalizedDescriptionKey]];
  147. dispatch_semaphore_signal(semaphore);
  148. return;
  149. }
  150. // LOCK
  151. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:returnToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  152. returnToken = token;
  153. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithFileID:fileID token:returnToken];
  154. // REMOVE METADATA
  155. [communication deleteEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  156. NSLog(@"Found metadata and delete");
  157. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  158. NSLog(@"%@", [NSString stringWithFormat:@"Remove metadata error %lu", (unsigned long)response.statusCode]);
  159. }];
  160. // MARK
  161. [communication markEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  162. // UNLOCK
  163. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:returnToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  164. returnToken = nil;
  165. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithFileID:fileID token:@""];
  166. dispatch_semaphore_signal(semaphore);
  167. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  168. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Unlock folder error %lu", (unsigned long)response.statusCode] forKey:NSLocalizedDescriptionKey]];
  169. dispatch_semaphore_signal(semaphore);
  170. }];
  171. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  172. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Mark folder as encrypted error %lu", (unsigned long)response.statusCode] forKey:NSLocalizedDescriptionKey]];
  173. dispatch_semaphore_signal(semaphore);
  174. }];
  175. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  176. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Lock folder error %lu", (unsigned long)response.statusCode] forKey:NSLocalizedDescriptionKey]];
  177. dispatch_semaphore_signal(semaphore);
  178. }];
  179. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  180. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Read folder error %lu", (unsigned long)response.statusCode] forKey:NSLocalizedDescriptionKey]];
  181. dispatch_semaphore_signal(semaphore);
  182. }];
  183. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  184. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  185. *token = returnToken;
  186. return returnError;
  187. }
  188. - (NSError *)deletemarkEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID serverUrl:(NSString *)serverUrl token:(NSString **)token
  189. {
  190. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  191. __block NSError *returnError = nil;
  192. __block NSString *returnToken = *token;
  193. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  194. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  195. [communication setUserAgent:[CCUtility getUserAgent]];
  196. // Read Folder
  197. [communication readFolder:serverUrl depth:@"1" withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *tokenReadFolder) {
  198. if (items.count > 1) {
  199. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:999 userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"The directory is not empty", nil) forKey:NSLocalizedDescriptionKey]];
  200. dispatch_semaphore_signal(semaphore);
  201. return;
  202. }
  203. // LOCK
  204. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:returnToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  205. returnToken = token;
  206. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithFileID:fileID token:returnToken];
  207. // DELETE METADATA
  208. [communication deleteEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  209. NSLog(@"Found metadata and delete");
  210. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  211. NSLog(@"%@", [NSString stringWithFormat:@"Remove metadata error %lu", (unsigned long)response.statusCode]);
  212. }];
  213. // DELETE MARK
  214. [communication deletemarkEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  215. // UNLOCK
  216. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:returnToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  217. returnToken = nil;
  218. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithFileID:fileID token:@""];
  219. dispatch_semaphore_signal(semaphore);
  220. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  221. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Unlock folder error %lu", (unsigned long)response.statusCode] forKey:NSLocalizedDescriptionKey]];
  222. dispatch_semaphore_signal(semaphore);
  223. }];
  224. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  225. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Delete mark folder as encrypted error %lu", (unsigned long)response.statusCode] forKey:NSLocalizedDescriptionKey]];
  226. dispatch_semaphore_signal(semaphore);
  227. }];
  228. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  229. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Lock folder error %lu", (unsigned long)response.statusCode] forKey:NSLocalizedDescriptionKey]];
  230. dispatch_semaphore_signal(semaphore);
  231. }];
  232. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  233. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Read folder error %lu", (unsigned long)response.statusCode] forKey:NSLocalizedDescriptionKey]];
  234. dispatch_semaphore_signal(semaphore);
  235. }];
  236. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  237. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  238. *token = returnToken;
  239. return returnError;
  240. }
  241. - (NSError *)getEndToEndMetadata:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID metadata:(NSString **)metadata
  242. {
  243. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  244. __block NSError *returnError = nil;
  245. __block NSString *returnMetadata = nil;
  246. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  247. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  248. [communication setUserAgent:[CCUtility getUserAgent]];
  249. [communication getEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  250. returnMetadata = encryptedMetadata;
  251. dispatch_semaphore_signal(semaphore);
  252. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  253. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Get metadata error %lu", (unsigned long)response.statusCode] forKey:NSLocalizedDescriptionKey]];
  254. dispatch_semaphore_signal(semaphore);
  255. }];
  256. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  257. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  258. *metadata = returnMetadata;
  259. return returnError;
  260. }
  261. - (NSError *)storeEndToEndMetadata:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID metadata:(NSString *)metadata token:(NSString **)token
  262. {
  263. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  264. __block NSError *returnError = nil;
  265. __block NSString *returnToken = nil;
  266. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  267. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  268. [communication setUserAgent:[CCUtility getUserAgent]];
  269. // LOCK
  270. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:*token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  271. returnToken = token;
  272. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithFileID:fileID token:returnToken];
  273. // STORE METADATA
  274. [communication storeEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID encryptedMetadata:metadata onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  275. // UNLOCK
  276. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:returnToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  277. returnToken = nil;
  278. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithFileID:fileID token:@""];
  279. dispatch_semaphore_signal(semaphore);
  280. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  281. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Unlock folder error %lu", (unsigned long)response.statusCode] forKey:NSLocalizedDescriptionKey]];
  282. dispatch_semaphore_signal(semaphore);
  283. }];
  284. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  285. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Store metadata error %lu", (unsigned long)response.statusCode] forKey:NSLocalizedDescriptionKey]];
  286. dispatch_semaphore_signal(semaphore);
  287. }];
  288. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  289. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Lock folder error %lu", (unsigned long)response.statusCode] forKey:NSLocalizedDescriptionKey]];
  290. dispatch_semaphore_signal(semaphore);
  291. }];
  292. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  293. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  294. *token = returnToken;
  295. return returnError;
  296. }
  297. - (NSError *)updateEndToEndMetadata:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID metadata:(NSString *)metadata token:(NSString **)token
  298. {
  299. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  300. __block NSError *returnError = nil;
  301. __block NSString *returnToken = nil;
  302. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  303. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  304. [communication setUserAgent:[CCUtility getUserAgent]];
  305. // LOCK
  306. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:*token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  307. returnToken = token;
  308. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithFileID:fileID token:returnToken];
  309. // UPDATA METADATA
  310. [communication updateEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID encryptedMetadata:metadata token:returnToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  311. // UNLOCK
  312. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:returnToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  313. returnToken = nil;
  314. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithFileID:fileID token:@""];
  315. dispatch_semaphore_signal(semaphore);
  316. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  317. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Unlock folder error %lu", (unsigned long)response.statusCode] forKey:NSLocalizedDescriptionKey]];
  318. dispatch_semaphore_signal(semaphore);
  319. }];
  320. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  321. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Update metadata error %lu", (unsigned long)response.statusCode] forKey:NSLocalizedDescriptionKey]];
  322. dispatch_semaphore_signal(semaphore);
  323. }];
  324. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  325. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Lock folder error %lu", (unsigned long)response.statusCode] forKey:NSLocalizedDescriptionKey]];
  326. dispatch_semaphore_signal(semaphore);
  327. }];
  328. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  329. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  330. *token = returnToken;
  331. return returnError;
  332. }
  333. - (NSError *)rebuildEndToEndMetadata:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID metadata:(NSString *)metadata token:(NSString **)token
  334. {
  335. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  336. __block NSError *returnError = nil;
  337. __block NSString *returnToken = nil;
  338. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  339. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  340. [communication setUserAgent:[CCUtility getUserAgent]];
  341. // LOCK
  342. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:*token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  343. returnToken = token;
  344. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithFileID:fileID token:returnToken];
  345. // DELETE METADATA
  346. [communication deleteEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  347. // STORE METADATA
  348. [communication storeEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID encryptedMetadata:metadata onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  349. // UNLOCK
  350. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:returnToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  351. returnToken = nil;
  352. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithFileID:fileID token:@""];
  353. dispatch_semaphore_signal(semaphore);
  354. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  355. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Unlock folder error %lu", (unsigned long)response.statusCode] forKey:NSLocalizedDescriptionKey]];
  356. dispatch_semaphore_signal(semaphore);
  357. }];
  358. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  359. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Store metadata error %lu", (unsigned long)response.statusCode] forKey:NSLocalizedDescriptionKey]];
  360. dispatch_semaphore_signal(semaphore);
  361. }];
  362. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  363. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Update metadata error %lu", (unsigned long)response.statusCode] forKey:NSLocalizedDescriptionKey]];
  364. dispatch_semaphore_signal(semaphore);
  365. }];
  366. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  367. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Lock folder error %lu", (unsigned long)response.statusCode] forKey:NSLocalizedDescriptionKey]];
  368. dispatch_semaphore_signal(semaphore);
  369. }];
  370. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  371. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  372. return returnError;
  373. }
  374. @end