NCNetworkingSync.m 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. - (NSError *)lockEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID token:(NSString **)token
  23. {
  24. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  25. __block NSError *returnError = nil;
  26. __block NSString *paramToken = nil;
  27. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  28. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  29. [communication setUserAgent:[CCUtility getUserAgent]];
  30. [communication lockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID token:*token onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  31. paramToken = token;
  32. dispatch_semaphore_signal(semaphore);
  33. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  34. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Lock folder error" forKey:NSLocalizedDescriptionKey]];
  35. dispatch_semaphore_signal(semaphore);
  36. }];
  37. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  38. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  39. *token = paramToken;
  40. return returnError;
  41. }
  42. - (NSError *)unlockEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID token:(NSString *)token
  43. {
  44. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  45. __block NSError *returnError= nil;
  46. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  47. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  48. [communication setUserAgent:[CCUtility getUserAgent]];
  49. [communication unlockEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  50. dispatch_semaphore_signal(semaphore);
  51. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  52. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Unlock folder error" forKey:NSLocalizedDescriptionKey]];
  53. dispatch_semaphore_signal(semaphore);
  54. }];
  55. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  56. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  57. return returnError;
  58. }
  59. - (NSError *)markEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID
  60. {
  61. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  62. __block NSError *returnError= nil;
  63. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  64. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  65. [communication setUserAgent:[CCUtility getUserAgent]];
  66. [communication markEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  67. dispatch_semaphore_signal(semaphore);
  68. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  69. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Mark folder as encrypted error" forKey:NSLocalizedDescriptionKey]];
  70. dispatch_semaphore_signal(semaphore);
  71. }];
  72. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  73. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  74. return returnError;
  75. }
  76. - (NSError *)deletemarkEndToEndFolderEncrypted:(NSString *)user userID:(NSString *)userID password:(NSString *)password url:(NSString *)url fileID:(NSString *)fileID
  77. {
  78. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  79. __block NSError *returnError= nil;
  80. dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  81. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  82. [communication setUserAgent:[CCUtility getUserAgent]];
  83. [communication deletemarkEndToEndFolderEncrypted:[url stringByAppendingString:@"/"] fileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  84. dispatch_semaphore_signal(semaphore);
  85. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  86. returnError = [NSError errorWithDomain:@"com.nextcloud.nextcloud" code:response.statusCode userInfo:[NSDictionary dictionaryWithObject:@"Remove folder as encrypted error" forKey:NSLocalizedDescriptionKey]];
  87. dispatch_semaphore_signal(semaphore);
  88. }];
  89. while (dispatch_semaphore_wait(semaphore, DISPATCH_TIME_NOW))
  90. [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate dateWithTimeIntervalSinceNow:k_timeout_webdav]];
  91. return returnError;
  92. }
  93. @end