NCNetworkingSync.m 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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", 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", 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
  67. {
  68. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  69. __block NSError *returnError = nil;
  70. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  71. [communication setCredentialsWithUser: user andUserID: userID andPassword: password];
  72. [communication setUserAgent:[CCUtility getUserAgent]];
  73. [communication readFile:filePathName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  74. dispatch_semaphore_signal(semaphore);
  75. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  76. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Read file error %lu", response.statusCode] forKey:NSLocalizedDescriptionKey]];
  77. dispatch_semaphore_signal(semaphore);
  78. }];
  79. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  80. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  81. return returnError;
  82. }
  83. - (NSError *)readFolder:(NSString *)serverUrl user:(NSString *)user userID:(NSString *)userID password:(NSString *)password items:(NSArray **)items
  84. {
  85. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  86. __block NSError *returnError = nil;
  87. __block NSArray *returnItems = nil;
  88. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  89. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  90. [communication setUserAgent:[CCUtility getUserAgent]];
  91. [communication readFolder:serverUrl depth:0 withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  92. returnItems = items;
  93. dispatch_semaphore_signal(semaphore);
  94. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  95. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Read folder error %lu", response.statusCode] forKey:NSLocalizedDescriptionKey]];
  96. dispatch_semaphore_signal(semaphore);
  97. }];
  98. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  99. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  100. *items = returnItems;
  101. return returnError;
  102. }
  103. #pragma --------------------------------------------------------------------------------------------
  104. #pragma mark ===== End-to-End Encryption =====
  105. #pragma --------------------------------------------------------------------------------------------
  106. - (NSError *)markEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID serverUrl:(NSString *)serverUrl token:(NSString **)token
  107. {
  108. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  109. __block NSError *returnError = nil;
  110. __block NSString *returnToken = *token;
  111. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  112. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  113. [communication setUserAgent:[CCUtility getUserAgent]];
  114. // Read Folder
  115. [communication readFolder:serverUrl depth:@"1" withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *tokenReadFolder) {
  116. if (items.count > 1) {
  117. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:999 userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"The directory is not empty", nil) forKey:NSLocalizedDescriptionKey]];
  118. dispatch_semaphore_signal(semaphore);
  119. return;
  120. }
  121. // LOCK
  122. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:returnToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  123. returnToken = token;
  124. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithFileID:fileID token:returnToken];
  125. // REMOVE METADATA
  126. [communication deleteEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  127. NSLog(@"Found metadata and delete");
  128. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  129. NSLog(@"%@", [NSString stringWithFormat:@"Remove metadata error %lu", response.statusCode]);
  130. }];
  131. // MARK
  132. [communication markEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  133. // UNLOCK
  134. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:returnToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  135. returnToken = nil;
  136. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithFileID:fileID token:@""];
  137. dispatch_semaphore_signal(semaphore);
  138. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  139. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Unlock folder error %lu", response.statusCode] forKey:NSLocalizedDescriptionKey]];
  140. dispatch_semaphore_signal(semaphore);
  141. }];
  142. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  143. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Mark folder as encrypted error %lu", response.statusCode] forKey:NSLocalizedDescriptionKey]];
  144. dispatch_semaphore_signal(semaphore);
  145. }];
  146. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  147. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Lock folder error %lu", response.statusCode] forKey:NSLocalizedDescriptionKey]];
  148. dispatch_semaphore_signal(semaphore);
  149. }];
  150. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  151. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Read folder error %lu", response.statusCode] forKey:NSLocalizedDescriptionKey]];
  152. dispatch_semaphore_signal(semaphore);
  153. }];
  154. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  155. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  156. *token = returnToken;
  157. return returnError;
  158. }
  159. - (NSError *)deletemarkEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID serverUrl:(NSString *)serverUrl token:(NSString **)token
  160. {
  161. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  162. __block NSError *returnError = nil;
  163. __block NSString *returnToken = *token;
  164. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  165. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  166. [communication setUserAgent:[CCUtility getUserAgent]];
  167. // Read Folder
  168. [communication readFolder:serverUrl depth:@"1" withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *tokenReadFolder) {
  169. if (items.count > 1) {
  170. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:999 userInfo:[NSDictionary dictionaryWithObject:NSLocalizedString(@"The directory is not empty", nil) forKey:NSLocalizedDescriptionKey]];
  171. dispatch_semaphore_signal(semaphore);
  172. return;
  173. }
  174. // LOCK
  175. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:returnToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  176. returnToken = token;
  177. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithFileID:fileID token:returnToken];
  178. // REMOVE METADATA
  179. [communication deleteEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  180. NSLog(@"Found metadata and delete");
  181. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  182. NSLog(@"%@", [NSString stringWithFormat:@"Remove metadata error %lu", response.statusCode]);
  183. }];
  184. // DELETE MARK
  185. [communication deletemarkEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  186. // UNLOCK
  187. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:returnToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  188. returnToken = nil;
  189. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithFileID:fileID token:@""];
  190. dispatch_semaphore_signal(semaphore);
  191. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  192. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Unlock folder error %lu", response.statusCode] forKey:NSLocalizedDescriptionKey]];
  193. dispatch_semaphore_signal(semaphore);
  194. }];
  195. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  196. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Delete mark folder as encrypted error %lu", response.statusCode] forKey:NSLocalizedDescriptionKey]];
  197. dispatch_semaphore_signal(semaphore);
  198. }];
  199. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  200. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Lock folder error %lu", response.statusCode] forKey:NSLocalizedDescriptionKey]];
  201. dispatch_semaphore_signal(semaphore);
  202. }];
  203. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  204. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Read folder error %lu", response.statusCode] forKey:NSLocalizedDescriptionKey]];
  205. dispatch_semaphore_signal(semaphore);
  206. }];
  207. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  208. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  209. *token = returnToken;
  210. return returnError;
  211. }
  212. - (NSError *)getEndToEndMetadata:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID metadata:(NSString **)metadata
  213. {
  214. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  215. __block NSError *returnError = nil;
  216. __block NSString *returnMetadata = nil;
  217. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  218. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  219. [communication setUserAgent:[CCUtility getUserAgent]];
  220. [communication getEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  221. returnMetadata = encryptedMetadata;
  222. dispatch_semaphore_signal(semaphore);
  223. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  224. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Get metadata error %lu", response.statusCode] forKey:NSLocalizedDescriptionKey]];
  225. dispatch_semaphore_signal(semaphore);
  226. }];
  227. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  228. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  229. *metadata = returnMetadata;
  230. return returnError;
  231. }
  232. - (NSError *)storeEndToEndMetadata:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID metadata:(NSString *)metadata token:(NSString **)token
  233. {
  234. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  235. __block NSError *returnError = nil;
  236. __block NSString *returnToken = nil;
  237. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  238. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  239. [communication setUserAgent:[CCUtility getUserAgent]];
  240. // LOCK
  241. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:*token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  242. returnToken = token;
  243. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithFileID:fileID token:returnToken];
  244. // STORE METADATA
  245. [communication storeEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID encryptedMetadata:metadata onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  246. // UNLOCK
  247. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:returnToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  248. returnToken = nil;
  249. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithFileID:fileID token:@""];
  250. dispatch_semaphore_signal(semaphore);
  251. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  252. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Unlock folder error %lu", response.statusCode] forKey:NSLocalizedDescriptionKey]];
  253. dispatch_semaphore_signal(semaphore);
  254. }];
  255. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  256. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Store metadata error %lu", response.statusCode] forKey:NSLocalizedDescriptionKey]];
  257. dispatch_semaphore_signal(semaphore);
  258. }];
  259. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  260. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Lock folder error %lu", response.statusCode] forKey:NSLocalizedDescriptionKey]];
  261. dispatch_semaphore_signal(semaphore);
  262. }];
  263. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  264. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  265. *token = returnToken;
  266. return returnError;
  267. }
  268. - (NSError *)updateEndToEndMetadata:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID metadata:(NSString *)metadata token:(NSString **)token
  269. {
  270. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  271. __block NSError *returnError = nil;
  272. __block NSString *returnToken = nil;
  273. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  274. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  275. [communication setUserAgent:[CCUtility getUserAgent]];
  276. // LOCK
  277. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:*token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  278. returnToken = token;
  279. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithFileID:fileID token:returnToken];
  280. // UPDATA METADATA
  281. [communication updateEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID encryptedMetadata:metadata token:returnToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  282. // UNLOCK
  283. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:returnToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  284. returnToken = nil;
  285. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithFileID:fileID token:@""];
  286. dispatch_semaphore_signal(semaphore);
  287. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  288. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Unlock folder error %lu", response.statusCode] forKey:NSLocalizedDescriptionKey]];
  289. dispatch_semaphore_signal(semaphore);
  290. }];
  291. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  292. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Update metadata error %lu", response.statusCode] forKey:NSLocalizedDescriptionKey]];
  293. dispatch_semaphore_signal(semaphore);
  294. }];
  295. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  296. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Lock folder error %lu", response.statusCode] forKey:NSLocalizedDescriptionKey]];
  297. dispatch_semaphore_signal(semaphore);
  298. }];
  299. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  300. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  301. *token = returnToken;
  302. return returnError;
  303. }
  304. @end