NCNetworkingSync.m 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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 %d", (int)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 %d", (int)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 %d", (int) 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 %d", (int)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 %d", (int)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 %d", (int)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 %d", (int)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 %d", (int)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 %d", (int)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 %d", (int)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 %d", (int)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 %d", (int)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 %d", (int)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 %d", (int)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 %d", (int)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 %d", (int)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. - (void)getEndToEndMetadataWithUser:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID success:(void (^)(NSString *encryptedMetadata))success failure:(void (^)(NSError *error))failure
  242. {
  243. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  244. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  245. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  246. [communication setUserAgent:[CCUtility getUserAgent]];
  247. [communication getEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  248. success(encryptedMetadata);
  249. dispatch_semaphore_signal(semaphore);
  250. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  251. failure([NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Get metadata error %d", (int)response.statusCode] forKey:NSLocalizedDescriptionKey]]);
  252. dispatch_semaphore_signal(semaphore);
  253. }];
  254. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  255. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  256. }
  257. - (NSError *)storeEndToEndMetadata:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID metadata:(NSString *)metadata token:(NSString **)token
  258. {
  259. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  260. __block NSError *returnError = nil;
  261. __block NSString *returnToken = nil;
  262. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  263. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  264. [communication setUserAgent:[CCUtility getUserAgent]];
  265. // LOCK
  266. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:*token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  267. returnToken = token;
  268. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithFileID:fileID token:returnToken];
  269. // STORE METADATA
  270. [communication storeEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID encryptedMetadata:metadata onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  271. dispatch_semaphore_signal(semaphore);
  272. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  273. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Store metadata error %d", (int)response.statusCode] forKey:NSLocalizedDescriptionKey]];
  274. dispatch_semaphore_signal(semaphore);
  275. }];
  276. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  277. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Lock folder error %d", (int)response.statusCode] forKey:NSLocalizedDescriptionKey]];
  278. dispatch_semaphore_signal(semaphore);
  279. }];
  280. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  281. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  282. *token = returnToken;
  283. return returnError;
  284. }
  285. - (NSError *)updateEndToEndMetadata:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID metadata:(NSString *)metadata token:(NSString **)token
  286. {
  287. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  288. __block NSError *returnError = nil;
  289. __block NSString *returnToken = nil;
  290. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  291. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  292. [communication setUserAgent:[CCUtility getUserAgent]];
  293. // LOCK
  294. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:*token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  295. returnToken = token;
  296. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithFileID:fileID token:returnToken];
  297. // UPDATA METADATA
  298. [communication updateEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID encryptedMetadata:metadata token:returnToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  299. dispatch_semaphore_signal(semaphore);
  300. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  301. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Update metadata error %d", (int)response.statusCode] forKey:NSLocalizedDescriptionKey]];
  302. dispatch_semaphore_signal(semaphore);
  303. }];
  304. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  305. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Lock folder error %d", (int)response.statusCode] forKey:NSLocalizedDescriptionKey]];
  306. dispatch_semaphore_signal(semaphore);
  307. }];
  308. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  309. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  310. *token = returnToken;
  311. return returnError;
  312. }
  313. - (NSError *)rebuildEndToEndMetadata:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID metadata:(NSString *)metadata token:(NSString **)token
  314. {
  315. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  316. __block NSError *returnError = nil;
  317. __block NSString *returnToken = nil;
  318. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  319. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  320. [communication setUserAgent:[CCUtility getUserAgent]];
  321. // LOCK
  322. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:*token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  323. returnToken = token;
  324. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithFileID:fileID token:returnToken];
  325. // DELETE METADATA
  326. [communication deleteEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  327. if (metadata) {
  328. // STORE METADATA
  329. [communication storeEndToEndMetadata:[url stringByAppendingString:@"/"] fileID:fileID encryptedMetadata:metadata onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *encryptedMetadata, NSString *redirectedServer) {
  330. // UNLOCK
  331. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:returnToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  332. returnToken = nil;
  333. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithFileID:fileID token:@""];
  334. dispatch_semaphore_signal(semaphore);
  335. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  336. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Unlock folder error %d", (int)response.statusCode] forKey:NSLocalizedDescriptionKey]];
  337. dispatch_semaphore_signal(semaphore);
  338. }];
  339. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  340. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Store metadata error %d", (int)response.statusCode] forKey:NSLocalizedDescriptionKey]];
  341. dispatch_semaphore_signal(semaphore);
  342. }];
  343. } else {
  344. // UNLOCK
  345. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:returnToken onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  346. returnToken = nil;
  347. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithFileID:fileID token:@""];
  348. dispatch_semaphore_signal(semaphore);
  349. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  350. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Unlock folder error %d", (int)response.statusCode] forKey:NSLocalizedDescriptionKey]];
  351. dispatch_semaphore_signal(semaphore);
  352. }];
  353. }
  354. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  355. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Update metadata error %d", (int)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:@"Lock folder error %d", (int)response.statusCode] forKey:NSLocalizedDescriptionKey]];
  360. dispatch_semaphore_signal(semaphore);
  361. }];
  362. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  363. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  364. return returnError;
  365. }
  366. - (NSError *)unlockEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID token:(NSString *)token
  367. {
  368. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  369. __block NSError *returnError = nil;
  370. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  371. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  372. [communication setUserAgent:[CCUtility getUserAgent]];
  373. // UNLOCK
  374. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  375. // Write DB token ""
  376. [[NCManageDatabase sharedInstance] setDirectoryE2ETokenLockWithFileID:fileID token:@""];
  377. dispatch_semaphore_signal(semaphore);
  378. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  379. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:[NSString stringWithFormat:@"Unlock folder error %d", (int)response.statusCode] forKey:NSLocalizedDescriptionKey]];
  380. dispatch_semaphore_signal(semaphore);
  381. }];
  382. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  383. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  384. return returnError;
  385. }
  386. @end