NCNetworkingSync.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. @implementation NCNetworkingSync
  12. + (NCNetworkingSync *)sharedManager {
  13. static NCNetworkingSync *sharedManager;
  14. @synchronized(self)
  15. {
  16. if (!sharedManager) {
  17. sharedManager = [NCNetworkingSync new];
  18. }
  19. return sharedManager;
  20. }
  21. }
  22. #pragma --------------------------------------------------------------------------------------------
  23. #pragma mark ============================
  24. #pragma --------------------------------------------------------------------------------------------
  25. - (NSError *)uploadFile:(NSString *)localFilePathName remoteFilePathName:(NSString *)remoteFilePathName user:(NSString *)user userID:(NSString *)userID password:(NSString *)password
  26. {
  27. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  28. __block NSError *returnError = nil;
  29. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  30. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  31. [communication setUserAgent:[CCUtility getUserAgent]];
  32. [communication uploadFileSession:localFilePathName toDestiny:remoteFilePathName onCommunication:communication progress:^(NSProgress *progress) {
  33. // Progress
  34. } successRequest:^(NSURLResponse *response, NSString *redirectedServer) {
  35. dispatch_semaphore_signal(semaphore);
  36. } failureRequest:^(NSURLResponse *response, NSString *redirectedServer, NSError *error) {
  37. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  38. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:httpResponse.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Upload file error" forKey:NSLocalizedDescriptionKey]];
  39. dispatch_semaphore_signal(semaphore);
  40. } failureBeforeRequest:^(NSError *error) {
  41. returnError = error;
  42. dispatch_semaphore_signal(semaphore);
  43. }];
  44. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  45. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  46. return returnError;
  47. }
  48. - (NSError *)checkServer:(NSString *)serverUrl user:(NSString *)user userID:(NSString *)userID password:(NSString *)password
  49. {
  50. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  51. __block NSError *returnError = nil;
  52. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  53. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  54. [communication setUserAgent:[CCUtility getUserAgent]];
  55. [communication checkServer:serverUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  56. dispatch_semaphore_signal(semaphore);
  57. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  58. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Check server error" forKey:NSLocalizedDescriptionKey]];
  59. dispatch_semaphore_signal(semaphore);
  60. }];
  61. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  62. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  63. return returnError;
  64. }
  65. - (NSError *)readFile:(NSString *)filePathName user:(NSString *)user userID:(NSString *)userID password:(NSString *)password
  66. {
  67. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  68. __block NSError *returnError = nil;
  69. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  70. [communication setCredentialsWithUser: user andUserID: userID andPassword: password];
  71. [communication setUserAgent:[CCUtility getUserAgent]];
  72. [communication readFile:filePathName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  73. dispatch_semaphore_signal(semaphore);
  74. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  75. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Read file error" forKey:NSLocalizedDescriptionKey]];
  76. dispatch_semaphore_signal(semaphore);
  77. }];
  78. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  79. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  80. return returnError;
  81. }
  82. #pragma --------------------------------------------------------------------------------------------
  83. #pragma mark ===== End-to-End Encryption =====
  84. #pragma --------------------------------------------------------------------------------------------
  85. - (NSError *)lockEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID token:(NSString **)token
  86. {
  87. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  88. __block NSError *returnError = nil;
  89. __block NSString *returnToken = nil;
  90. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  91. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  92. [communication setUserAgent:[CCUtility getUserAgent]];
  93. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:*token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  94. returnToken = token;
  95. dispatch_semaphore_signal(semaphore);
  96. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  97. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Lock folder error" forKey:NSLocalizedDescriptionKey]];
  98. dispatch_semaphore_signal(semaphore);
  99. }];
  100. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  101. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  102. *token = returnToken;
  103. return returnError;
  104. }
  105. - (NSError *)unlockEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID token:(NSString *)token
  106. {
  107. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  108. __block NSError *returnError= nil;
  109. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  110. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  111. [communication setUserAgent:[CCUtility getUserAgent]];
  112. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  113. dispatch_semaphore_signal(semaphore);
  114. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  115. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Unlock folder error" forKey:NSLocalizedDescriptionKey]];
  116. dispatch_semaphore_signal(semaphore);
  117. }];
  118. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  119. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  120. return returnError;
  121. }
  122. - (NSError *)markEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID
  123. {
  124. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  125. __block NSError *returnError= nil;
  126. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  127. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  128. [communication setUserAgent:[CCUtility getUserAgent]];
  129. [communication markEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  130. dispatch_semaphore_signal(semaphore);
  131. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  132. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Mark folder as encrypted error" forKey:NSLocalizedDescriptionKey]];
  133. dispatch_semaphore_signal(semaphore);
  134. }];
  135. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  136. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  137. return returnError;
  138. }
  139. - (NSError *)deletemarkEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID
  140. {
  141. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  142. __block NSError *returnError= nil;
  143. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  144. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  145. [communication setUserAgent:[CCUtility getUserAgent]];
  146. [communication deletemarkEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  147. dispatch_semaphore_signal(semaphore);
  148. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  149. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Remove folder as encrypted error" forKey:NSLocalizedDescriptionKey]];
  150. dispatch_semaphore_signal(semaphore);
  151. }];
  152. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER))
  153. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  154. return returnError;
  155. }
  156. @end