OCNetworking.m 113 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226
  1. //
  2. // OCnetworking.m
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 10/05/15.
  6. // Copyright (c) 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #import "OCNetworking.h"
  24. #import "CCUtility.h"
  25. #import "CCGraphics.h"
  26. #import "CCCertificate.h"
  27. #import "NSString+Encode.h"
  28. #import "NCBridgeSwift.h"
  29. #pragma clang diagnostic ignored "-Warc-performSelector-leaks"
  30. @interface OCnetworking ()
  31. {
  32. NSString *_activeUser;
  33. NSString *_activeUserID;
  34. NSString *_activePassword;
  35. NSString *_activeUrl;
  36. }
  37. @end
  38. @implementation OCnetworking
  39. - (id)initWithDelegate:(id <OCNetworkingDelegate>)delegate metadataNet:(CCMetadataNet *)metadataNet withUser:(NSString *)withUser withUserID:(NSString *)withUserID withPassword:(NSString *)withPassword withUrl:(NSString *)withUrl
  40. {
  41. self = [super init];
  42. if (self) {
  43. _delegate = delegate;
  44. _metadataNet = [CCMetadataNet new];
  45. _metadataNet = [metadataNet copy];
  46. _activeUser = withUser;
  47. _activeUserID = withUserID;
  48. _activePassword = withPassword;
  49. _activeUrl = withUrl;
  50. }
  51. return self;
  52. }
  53. - (void)start
  54. {
  55. if (![NSThread isMainThread]) {
  56. [self performSelectorOnMainThread:@selector(start) withObject:nil waitUntilDone:NO];
  57. return;
  58. }
  59. [self willChangeValueForKey:@"isExecuting"];
  60. _isExecuting = YES;
  61. [self didChangeValueForKey:@"isExecuting"];
  62. if (self.isCancelled) {
  63. [self finish];
  64. } else {
  65. [self poolNetworking];
  66. }
  67. }
  68. - (void)finish
  69. {
  70. [self willChangeValueForKey:@"isExecuting"];
  71. [self willChangeValueForKey:@"isFinished"];
  72. _isExecuting = NO;
  73. _isFinished = YES;
  74. [self didChangeValueForKey:@"isExecuting"];
  75. [self didChangeValueForKey:@"isFinished"];
  76. }
  77. - (void)cancel
  78. {
  79. if (_isExecuting) {
  80. [self complete];
  81. }
  82. [super cancel];
  83. }
  84. - (void)poolNetworking
  85. {
  86. #ifndef EXTENSION
  87. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];
  88. #endif
  89. if([self respondsToSelector:NSSelectorFromString(_metadataNet.action)])
  90. [self performSelector:NSSelectorFromString(_metadataNet.action)];
  91. }
  92. #pragma --------------------------------------------------------------------------------------------
  93. #pragma mark ===== Delegate =====
  94. #pragma --------------------------------------------------------------------------------------------
  95. - (void)complete
  96. {
  97. [self finish];
  98. #ifndef EXTENSION
  99. [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];
  100. #endif
  101. }
  102. #pragma --------------------------------------------------------------------------------------------
  103. #pragma mark ===== download =====
  104. #pragma --------------------------------------------------------------------------------------------
  105. - (NSURLSessionTask *)downloadFileNameServerUrl:(NSString *)fileNameServerUrl fileNameLocalPath:(NSString *)fileNameLocalPath success:(void (^)(int64_t length))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  106. {
  107. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  108. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  109. [communication setUserAgent:[CCUtility getUserAgent]];
  110. NSURLSessionTask *sessionTask = [communication downloadFileSession:fileNameServerUrl toDestiny:fileNameLocalPath defaultPriority:YES onCommunication:communication progress:^(NSProgress *progress) {
  111. //float percent = roundf (progress.fractionCompleted * 100);
  112. } successRequest:^(NSURLResponse *response, NSURL *filePath) {
  113. int64_t totalUnitCount = 0;
  114. NSDictionary *fields = [(NSHTTPURLResponse*)response allHeaderFields];
  115. NSString *contentLength = [fields objectForKey:@"Content-Length"];
  116. if(contentLength) {
  117. totalUnitCount = (int64_t) [contentLength longLongValue];
  118. }
  119. success(totalUnitCount);
  120. } failureRequest:^(NSURLResponse *response, NSError *error) {
  121. NSString *message;
  122. NSInteger errorCode;
  123. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  124. errorCode = httpResponse.statusCode;
  125. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  126. errorCode = error.code;
  127. // Error
  128. if (errorCode == 503)
  129. message = NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil);
  130. else
  131. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  132. failure(message, errorCode);
  133. }];
  134. return sessionTask;
  135. }
  136. #pragma --------------------------------------------------------------------------------------------
  137. #pragma mark ===== upload =====
  138. #pragma --------------------------------------------------------------------------------------------
  139. - (NSURLSessionTask *)uploadFileNameServerUrl:(NSString *)fileNameServerUrl fileNameLocalPath:(NSString *)fileNameLocalPath success:(void(^)(NSString *fileID, NSString *etag, NSDate *date))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  140. {
  141. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  142. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  143. [communication setUserAgent:[CCUtility getUserAgent]];
  144. NSURLSessionTask *sessionTask = [communication uploadFileSession:fileNameLocalPath toDestiny:fileNameServerUrl onCommunication:communication progress:^(NSProgress *progress) {
  145. //float percent = roundf (progress.fractionCompleted * 100);
  146. } successRequest:^(NSURLResponse *response, NSString *redirectedServer) {
  147. NSDictionary *fields = [(NSHTTPURLResponse*)response allHeaderFields];
  148. NSString *fileID = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-FileId"]];
  149. NSString *etag = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-ETag"]];
  150. NSDate *date = [CCUtility dateEnUsPosixFromCloud:[fields objectForKey:@"Date"]];
  151. success(fileID, etag, date);
  152. } failureRequest:^(NSURLResponse *response, NSString *redirectedServer, NSError *error) {
  153. NSString *message;
  154. NSInteger errorCode;
  155. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  156. errorCode = httpResponse.statusCode;
  157. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  158. errorCode = error.code;
  159. // Error
  160. if (errorCode == 503)
  161. message = NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil);
  162. else
  163. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  164. failure(message, errorCode);
  165. } failureBeforeRequest:^(NSError *error) {
  166. failure(@"", error.code);
  167. }];
  168. return sessionTask;
  169. }
  170. #pragma --------------------------------------------------------------------------------------------
  171. #pragma mark ===== downloadThumbnail =====
  172. #pragma --------------------------------------------------------------------------------------------
  173. - (void)downloadThumbnail
  174. {
  175. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  176. __block NSString *ext;
  177. NSInteger width = 0, height = 0;
  178. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  179. NSString *dimOfThumbnail = (NSString *)_metadataNet.options;
  180. if ([dimOfThumbnail.lowercaseString isEqualToString:@"xs"]) { width = 32; height = 32; ext = @"ico"; }
  181. else if ([dimOfThumbnail.lowercaseString isEqualToString:@"s"]) { width = 64; height = 64; ext = @"ico"; }
  182. else if ([dimOfThumbnail.lowercaseString isEqualToString:@"m"]) { width = 128; height = 128; ext = @"ico"; }
  183. else if ([dimOfThumbnail.lowercaseString isEqualToString:@"l"]) { width = 640; height = 640; ext = @"pvw"; }
  184. else if ([dimOfThumbnail.lowercaseString isEqualToString:@"xl"]) { width = 1024; height = 1024; ext = @"pvw"; }
  185. if ([[NSFileManager defaultManager] fileExistsAtPath:[NSString stringWithFormat:@"%@/%@.%@", directoryUser, _metadataNet.fileID, ext]]) {
  186. if ([self.delegate respondsToSelector:@selector(downloadThumbnailSuccessFailure:message:errorCode:)])
  187. [self.delegate downloadThumbnailSuccessFailure:_metadataNet message:nil errorCode:0];
  188. [self complete];
  189. return;
  190. }
  191. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  192. [communication setUserAgent:[CCUtility getUserAgent]];
  193. [communication getRemoteThumbnailByServer:[_activeUrl stringByAppendingString:@"/"] ofFilePath:_metadataNet.fileName withWidth:width andHeight:height onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSData *thumbnail, NSString *redirectedServer) {
  194. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  195. if ([recordAccount.account isEqualToString:_metadataNet.account] && [thumbnail length] > 0) {
  196. UIImage *thumbnailImage = [UIImage imageWithData:thumbnail];
  197. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  198. [CCGraphics saveIcoWithEtag:_metadataNet.fileID image:thumbnailImage writeToFile:[NSString stringWithFormat:@"%@/%@.%@", directoryUser, _metadataNet.fileID, ext] copy:NO move:NO fromPath:nil toPath:nil];
  199. if ([self.delegate respondsToSelector:@selector(downloadThumbnailSuccessFailure:message:errorCode:)] && [_metadataNet.action isEqualToString:actionDownloadThumbnail])
  200. [self.delegate downloadThumbnailSuccessFailure:_metadataNet message:nil errorCode:0];
  201. } else {
  202. if ([self.delegate respondsToSelector:@selector(downloadThumbnailSuccessFailure:message:errorCode:)] && [_metadataNet.action isEqualToString:actionDownloadThumbnail])
  203. [self.delegate downloadThumbnailSuccessFailure:_metadataNet message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  204. }
  205. [self complete];
  206. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  207. NSString *message = [NSString new];
  208. NSInteger errorCode = [self downloadThumbnailFailureResponse:response error:error message:&message];
  209. if ([self.delegate respondsToSelector:@selector(downloadThumbnailSuccessFailure:message:errorCode:)] && [_metadataNet.action isEqualToString:actionDownloadThumbnail]) {
  210. [self.delegate downloadThumbnailSuccessFailure:_metadataNet message:message errorCode:errorCode];
  211. }
  212. [self complete];
  213. }];
  214. }
  215. - (void)downloadThumbnailWithDimOfThumbnail:(NSString *)dimOfThumbnail fileName:(NSString *)fileName fileNameLocal:(NSString *)fileNameLocal success:(void (^)(void))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  216. {
  217. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  218. __block NSString *ext;
  219. NSInteger width = 0, height = 0;
  220. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  221. if ([dimOfThumbnail.lowercaseString isEqualToString:@"xs"]) { width = 32; height = 32; ext = @"ico"; }
  222. else if ([dimOfThumbnail.lowercaseString isEqualToString:@"s"]) { width = 64; height = 64; ext = @"ico"; }
  223. else if ([dimOfThumbnail.lowercaseString isEqualToString:@"m"]) { width = 128; height = 128; ext = @"ico"; }
  224. else if ([dimOfThumbnail.lowercaseString isEqualToString:@"l"]) { width = 640; height = 640; ext = @"pvw"; }
  225. else if ([dimOfThumbnail.lowercaseString isEqualToString:@"xl"]) { width = 1024; height = 1024; ext = @"pvw"; }
  226. NSString *fileNamePathLocal = [NSString stringWithFormat:@"%@/%@.%@", directoryUser, fileNameLocal, ext];
  227. if ([[NSFileManager defaultManager] fileExistsAtPath:fileNamePathLocal]) {
  228. success();
  229. } else {
  230. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  231. [communication setUserAgent:[CCUtility getUserAgent]];
  232. [communication getRemoteThumbnailByServer:[_activeUrl stringByAppendingString:@"/"] ofFilePath:fileName withWidth:width andHeight:height onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSData *thumbnail, NSString *redirectedServer) {
  233. [CCGraphics saveIcoWithEtag:fileNameLocal image:[UIImage imageWithData:thumbnail] writeToFile:fileNamePathLocal copy:NO move:NO fromPath:nil toPath:nil];
  234. success();
  235. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  236. NSString *message = [NSString new];
  237. NSInteger errorCode = [self downloadThumbnailFailureResponse:response error:error message:&message];
  238. failure(message, errorCode);
  239. }];
  240. }
  241. }
  242. - (NSInteger)downloadThumbnailFailureResponse:(NSHTTPURLResponse *)response error:(NSError *)error message:(NSString **)message
  243. {
  244. NSInteger errorCode = response.statusCode;
  245. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  246. errorCode = error.code;
  247. // Error
  248. if (errorCode == 503)
  249. *message = NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil);
  250. else
  251. *message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  252. return errorCode;
  253. }
  254. #pragma --------------------------------------------------------------------------------------------
  255. #pragma mark ===== Read Folder =====
  256. #pragma --------------------------------------------------------------------------------------------
  257. - (void)readFolder
  258. {
  259. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  260. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  261. [communication setUserAgent:[CCUtility getUserAgent]];
  262. [communication readFolder:_metadataNet.serverUrl depth:_metadataNet.depth withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  263. // Test active account
  264. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  265. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  266. if ([self.delegate respondsToSelector:@selector(readFolderSuccessFailure:metadataFolder:metadatas:message:errorCode:)])
  267. [self.delegate readFolderSuccessFailure:_metadataNet metadataFolder:nil metadatas:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  268. } else {
  269. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  270. NSMutableArray *metadatas = [NSMutableArray new];
  271. tableMetadata *metadataFolder = [tableMetadata new];
  272. _metadataNet.directoryID = [self readFolderSuccessServerUrl:_metadataNet.serverUrl items:items account:_metadataNet.account metadatas:&metadatas metadataFolder:&metadataFolder];
  273. dispatch_async(dispatch_get_main_queue(), ^{
  274. if ([self.delegate respondsToSelector:@selector(readFolderSuccessFailure:metadataFolder:metadatas:message:errorCode:)])
  275. [self.delegate readFolderSuccessFailure:_metadataNet metadataFolder:metadataFolder metadatas:metadatas message:nil errorCode:0];
  276. });
  277. });
  278. }
  279. [self complete];
  280. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  281. NSString *message = [NSString new];
  282. NSInteger errorCode = [self readFolderFailureServerUrl:_metadataNet.serverUrl response:response error:error message:&message];
  283. if ([self.delegate respondsToSelector:@selector(readFolderSuccessFailure:metadataFolder:metadatas:message:errorCode:)]) {
  284. [self.delegate readFolderSuccessFailure:_metadataNet metadataFolder:nil metadatas:nil message:message errorCode:errorCode];
  285. }
  286. [self complete];
  287. }];
  288. }
  289. - (void)readFolderWithServerUrl:(NSString *)serverUrl depth:(NSString *)depth account:(NSString *)account success:(void(^)(NSArray *metadatas, tableMetadata *metadataFolder, NSString *directoryID))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  290. {
  291. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  292. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  293. [communication setUserAgent:[CCUtility getUserAgent]];
  294. [communication readFolder:serverUrl depth:depth withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  295. // Test active account
  296. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  297. if (![recordAccount.account isEqualToString:account]) {
  298. failure(NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil), k_CCErrorUserNotAvailble);
  299. } else {
  300. NSMutableArray *metadatas = [NSMutableArray new];
  301. tableMetadata *metadataFolder = [tableMetadata new];
  302. NSString *directoryID = [self readFolderSuccessServerUrl:serverUrl items:items account:account metadatas:&metadatas metadataFolder:&metadataFolder];
  303. success(metadatas, metadataFolder, directoryID);
  304. }
  305. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  306. NSString *message = [NSString new];
  307. NSInteger errorCode = [self readFolderFailureServerUrl:serverUrl response:response error:error message:&message];
  308. failure(message, errorCode);
  309. }];
  310. }
  311. - (NSInteger)readFolderFailureServerUrl:(NSString *)serverUrl response:(NSHTTPURLResponse *)response error:(NSError *)error message:(NSString **)message
  312. {
  313. NSInteger errorCode = response.statusCode;
  314. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  315. errorCode = error.code;
  316. // Error
  317. if (errorCode == 503)
  318. *message = NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil);
  319. else
  320. *message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  321. // Request trusted certificated
  322. if ([error code] == NSURLErrorServerCertificateUntrusted)
  323. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  324. // Activity
  325. [[NCManageDatabase sharedInstance] addActivityClient:serverUrl fileID:@"" action:k_activityDebugActionReadFolder selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  326. return errorCode;
  327. }
  328. - (NSString *)readFolderSuccessServerUrl:(NSString *)serverUrl items:(NSArray *)items account:(NSString *)account metadatas:(NSMutableArray **)metadatas metadataFolder:(tableMetadata **)metadataFolder
  329. {
  330. BOOL showHiddenFiles = [CCUtility getShowHiddenFiles];
  331. BOOL isFolderEncrypted = [CCUtility isFolderEncrypted:serverUrl account:account];
  332. // Check items > 0
  333. if ([items count] == 0) {
  334. #ifndef EXTENSION
  335. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  336. [appDelegate messageNotification:@"Server error" description:@"Read Folder WebDAV : [items NULL] please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:k_CCErrorInternalError];
  337. #endif
  338. return nil;
  339. }
  340. // directory [0]
  341. OCFileDto *itemDtoFolder = [items objectAtIndex:0];
  342. //NSDate *date = [NSDate dateWithTimeIntervalSince1970:itemDtoDirectory.date];
  343. NSString *directoryID = [[NCManageDatabase sharedInstance] addDirectoryWithEncrypted:itemDtoFolder.isEncrypted favorite:itemDtoFolder.isFavorite fileID:itemDtoFolder.ocId permissions:itemDtoFolder.permissions serverUrl:serverUrl].directoryID;
  344. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  345. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:_activeUrl];
  346. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  347. NSString *directoryIDFolder;
  348. NSString *serverUrlFolder;
  349. // Metadata . (self Folder)
  350. if ([serverUrl isEqualToString:[CCUtility getHomeServerUrlActiveUrl:_activeUrl]]) {
  351. // root folder
  352. serverUrlFolder = @"..";
  353. directoryIDFolder = @"00000000-0000-0000-0000-000000000000";
  354. *metadataFolder = [CCUtility trasformedOCFileToCCMetadata:itemDtoFolder fileName:@"." serverUrl:serverUrlFolder directoryID:directoryIDFolder autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:account directoryUser:directoryUser isFolderEncrypted:isFolderEncrypted];
  355. } else {
  356. serverUrlFolder = [CCUtility deletingLastPathComponentFromServerUrl:serverUrl];
  357. directoryIDFolder = [[NCManageDatabase sharedInstance] getDirectoryID:serverUrlFolder];
  358. if (!directoryIDFolder) {
  359. return directoryID;
  360. }
  361. *metadataFolder = [CCUtility trasformedOCFileToCCMetadata:itemDtoFolder fileName:[serverUrl lastPathComponent] serverUrl:serverUrlFolder directoryID:directoryIDFolder autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:account directoryUser:directoryUser isFolderEncrypted:isFolderEncrypted];
  362. }
  363. NSArray *itemsSortedArray = [items sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
  364. NSString *first = [(OCFileDto*)a fileName];
  365. NSString *second = [(OCFileDto*)b fileName];
  366. return [[first lowercaseString] compare:[second lowercaseString]];
  367. }];
  368. for (NSUInteger i=1; i < [itemsSortedArray count]; i++) {
  369. OCFileDto *itemDto = [itemsSortedArray objectAtIndex:i];
  370. NSString *fileName = [itemDto.fileName stringByReplacingOccurrencesOfString:@"/" withString:@""];
  371. // Skip hidden files
  372. if (fileName.length > 0) {
  373. if (!showHiddenFiles && [[fileName substringToIndex:1] isEqualToString:@"."])
  374. continue;
  375. } else
  376. continue;
  377. if (itemDto.isDirectory) {
  378. (void)[[NCManageDatabase sharedInstance] addDirectoryWithEncrypted:itemDto.isEncrypted favorite:itemDto.isFavorite fileID:itemDto.ocId permissions:itemDto.permissions serverUrl:[CCUtility stringAppendServerUrl:serverUrl addFileName:fileName]];
  379. }
  380. // ----- BUG #942 ---------
  381. if ([itemDto.etag length] == 0) {
  382. #ifndef EXTENSION
  383. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  384. [appDelegate messageNotification:@"Server error" description:@"Metadata fileID absent, record excluded, please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:k_CCErrorInternalError];
  385. #endif
  386. continue;
  387. }
  388. // ------------------------
  389. [*metadatas addObject:[CCUtility trasformedOCFileToCCMetadata:itemDto fileName:itemDto.fileName serverUrl:serverUrl directoryID:directoryID autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:account directoryUser:directoryUser isFolderEncrypted:isFolderEncrypted]];
  390. }
  391. return directoryID;
  392. }
  393. #pragma --------------------------------------------------------------------------------------------
  394. #pragma mark ===== Search =====
  395. #pragma --------------------------------------------------------------------------------------------
  396. - (void)search
  397. {
  398. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  399. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  400. [communication setUserAgent:[CCUtility getUserAgent]];
  401. NSString *path = [_activeUrl stringByAppendingString:dav];
  402. NSString *folder = [_metadataNet.serverUrl stringByReplacingOccurrencesOfString:[CCUtility getHomeServerUrlActiveUrl:_activeUrl] withString:@""];
  403. NSString *dateLastModified;
  404. if (_metadataNet.date) {
  405. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  406. NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
  407. [dateFormatter setLocale:enUSPOSIXLocale];
  408. [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
  409. dateLastModified = [dateFormatter stringFromDate:_metadataNet.date];
  410. }
  411. [communication search:path folder:folder fileName: [NSString stringWithFormat:@"%%%@%%", _metadataNet.fileName] depth:_metadataNet.depth dateLastModified:dateLastModified contentType:_metadataNet.contentType withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  412. // Test active account
  413. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  414. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  415. if ([self.delegate respondsToSelector:@selector(searchSuccessFailure:metadatas:message:errorCode:)])
  416. [self.delegate searchSuccessFailure:_metadataNet metadatas:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  417. [self complete];
  418. return;
  419. }
  420. NSMutableArray *metadatas = [NSMutableArray new];
  421. BOOL showHiddenFiles = [CCUtility getShowHiddenFiles];
  422. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  423. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:_activeUrl];
  424. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  425. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  426. for(OCFileDto *itemDto in items) {
  427. NSString *serverUrl, *directoryID;
  428. BOOL isFolderEncrypted;
  429. NSString *fileName = [itemDto.fileName stringByReplacingOccurrencesOfString:@"/" withString:@""];
  430. // Skip hidden files
  431. if (fileName.length > 0) {
  432. if (!showHiddenFiles && [[fileName substringToIndex:1] isEqualToString:@"."])
  433. continue;
  434. } else
  435. continue;
  436. // ----- BUG #942 ---------
  437. if ([itemDto.etag length] == 0) {
  438. #ifndef EXTENSION
  439. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  440. [appDelegate messageNotification:@"Server error" description:@"Metadata fileID absent, record excluded, please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:k_CCErrorInternalError];
  441. #endif
  442. continue;
  443. }
  444. // ------------------------
  445. serverUrl = [itemDto.filePath stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@/files/%@", dav, _activeUserID] withString:@""];
  446. if ([serverUrl hasPrefix:@"/"])
  447. serverUrl = [serverUrl substringFromIndex:1];
  448. if ([serverUrl hasSuffix:@"/"])
  449. serverUrl = [serverUrl substringToIndex:[serverUrl length] - 1];
  450. serverUrl = [CCUtility stringAppendServerUrl:[_activeUrl stringByAppendingString:webDAV] addFileName:serverUrl];
  451. if (itemDto.isDirectory) {
  452. (void)[[NCManageDatabase sharedInstance] addDirectoryWithEncrypted:itemDto.isEncrypted favorite:itemDto.isFavorite fileID:itemDto.ocId permissions:itemDto.permissions serverUrl:[NSString stringWithFormat:@"%@/%@", serverUrl, fileName]];
  453. }
  454. directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:serverUrl];
  455. isFolderEncrypted = [CCUtility isFolderEncrypted:serverUrl account:_metadataNet.account];
  456. [metadatas addObject:[CCUtility trasformedOCFileToCCMetadata:itemDto fileName:itemDto.fileName serverUrl:serverUrl directoryID:directoryID autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:_metadataNet.account directoryUser:directoryUser isFolderEncrypted:isFolderEncrypted]];
  457. }
  458. dispatch_async(dispatch_get_main_queue(), ^{
  459. if ([self.delegate respondsToSelector:@selector(searchSuccessFailure:metadatas:message:errorCode:)])
  460. [self.delegate searchSuccessFailure:_metadataNet metadatas:metadatas message:nil errorCode:0];
  461. });
  462. });
  463. [self complete];
  464. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  465. NSInteger errorCode = response.statusCode;
  466. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  467. errorCode = error.code;
  468. // Error
  469. if ([self.delegate respondsToSelector:@selector(searchSuccessFailure:metadatas:message:errorCode:)]) {
  470. if (errorCode == 503)
  471. [self.delegate searchSuccessFailure:_metadataNet metadatas:nil message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  472. else
  473. [self.delegate searchSuccessFailure:_metadataNet metadatas:nil message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  474. }
  475. // Request trusted certificated
  476. if ([error code] == NSURLErrorServerCertificateUntrusted)
  477. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  478. [self complete];
  479. }];
  480. }
  481. #pragma --------------------------------------------------------------------------------------------
  482. #pragma mark ===== Setting Favorite =====
  483. #pragma --------------------------------------------------------------------------------------------
  484. - (void)settingFavorite
  485. {
  486. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  487. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  488. [communication setUserAgent:[CCUtility getUserAgent]];
  489. NSString *path = [_activeUrl stringByAppendingString:dav];
  490. [communication settingFavoriteServer:path andFileOrFolderPath:_metadataNet.fileName favorite:[_metadataNet.options boolValue] withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer, NSString *token) {
  491. // Test active account
  492. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  493. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  494. if ([self.delegate respondsToSelector:@selector(settingFavoriteSuccessFailure:message:errorCode:)])
  495. [self.delegate settingFavoriteSuccessFailure:_metadataNet message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  496. [self complete];
  497. return;
  498. }
  499. if ([self.delegate respondsToSelector:@selector(settingFavoriteSuccessFailure:message:errorCode:)])
  500. [self.delegate settingFavoriteSuccessFailure:_metadataNet message:nil errorCode:0];
  501. [self complete];
  502. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  503. NSInteger errorCode = response.statusCode;
  504. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  505. errorCode = error.code;
  506. // Error
  507. if ([self.delegate respondsToSelector:@selector(settingFavoriteSuccessFailure:message:errorCode:)]) {
  508. if (errorCode == 503)
  509. [self.delegate settingFavoriteSuccessFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  510. else
  511. [self.delegate settingFavoriteSuccessFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  512. }
  513. // Request trusted certificated
  514. if ([error code] == NSURLErrorServerCertificateUntrusted)
  515. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  516. [self complete];
  517. }];
  518. }
  519. #pragma --------------------------------------------------------------------------------------------
  520. #pragma mark ===== Listing Favorites =====
  521. #pragma --------------------------------------------------------------------------------------------
  522. - (void)listingFavorites
  523. {
  524. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  525. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  526. [communication setUserAgent:[CCUtility getUserAgent]];
  527. NSString *path = [_activeUrl stringByAppendingString:dav];
  528. NSString *folder = [_metadataNet.serverUrl stringByReplacingOccurrencesOfString:[CCUtility getHomeServerUrlActiveUrl:_activeUrl] withString:@""];
  529. [communication listingFavorites:path folder:folder withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  530. // Test active account
  531. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  532. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  533. if ([self.delegate respondsToSelector:@selector(listingFavoritesSuccessFailure:metadatas:message:errorCode:)])
  534. [self.delegate listingFavoritesSuccessFailure:_metadataNet metadatas:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  535. [self complete];
  536. return;
  537. }
  538. NSMutableArray *metadatas = [NSMutableArray new];
  539. BOOL showHiddenFiles = [CCUtility getShowHiddenFiles];
  540. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  541. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:_activeUrl];
  542. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  543. // Order by fileNamePath
  544. items = [items sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  545. OCFileDto *record1 = obj1, *record2 = obj2;
  546. NSString *path1 = [[record1.filePath stringByAppendingString:record1.fileName] lowercaseString];
  547. NSString *path2 = [[record2.filePath stringByAppendingString:record2.fileName] lowercaseString];
  548. return [path1 compare:path2];
  549. }];
  550. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  551. for(OCFileDto *itemDto in items) {
  552. NSString *serverUrl, *directoryID;
  553. BOOL isFolderEncrypted;
  554. NSString *fileName = [itemDto.fileName stringByReplacingOccurrencesOfString:@"/" withString:@""];
  555. // Skip hidden files
  556. if (fileName.length > 0) {
  557. if (!showHiddenFiles && [[fileName substringToIndex:1] isEqualToString:@"."])
  558. continue;
  559. } else
  560. continue;
  561. // ----- BUG #942 ---------
  562. if ([itemDto.etag length] == 0) {
  563. #ifndef EXTENSION
  564. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  565. [appDelegate messageNotification:@"Server error" description:@"Metadata fileID absent, record excluded, please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:k_CCErrorInternalError];
  566. #endif
  567. continue;
  568. }
  569. // ------------------------
  570. serverUrl = [itemDto.filePath stringByReplacingOccurrencesOfString:[NSString stringWithFormat:@"%@/files/%@", dav, _activeUserID] withString:@""];
  571. if ([serverUrl hasPrefix:@"/"])
  572. serverUrl = [serverUrl substringFromIndex:1];
  573. if ([serverUrl hasSuffix:@"/"])
  574. serverUrl = [serverUrl substringToIndex:[serverUrl length] - 1];
  575. serverUrl = [CCUtility stringAppendServerUrl:[_activeUrl stringByAppendingString:webDAV] addFileName:serverUrl];
  576. if (itemDto.isDirectory) {
  577. (void)[[NCManageDatabase sharedInstance] addDirectoryWithEncrypted:itemDto.isEncrypted favorite:itemDto.isFavorite fileID:itemDto.ocId permissions:itemDto.permissions serverUrl:[NSString stringWithFormat:@"%@/%@", serverUrl, fileName]];
  578. }
  579. directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:serverUrl];
  580. isFolderEncrypted = [CCUtility isFolderEncrypted:serverUrl account:_metadataNet.account];
  581. [metadatas addObject:[CCUtility trasformedOCFileToCCMetadata:itemDto fileName:itemDto.fileName serverUrl:serverUrl directoryID:directoryID autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:_metadataNet.account directoryUser:directoryUser isFolderEncrypted:isFolderEncrypted]];
  582. }
  583. dispatch_async(dispatch_get_main_queue(), ^{
  584. if ([self.delegate respondsToSelector:@selector(listingFavoritesSuccessFailure:metadatas:message:errorCode:)])
  585. [self.delegate listingFavoritesSuccessFailure:_metadataNet metadatas:metadatas message:nil errorCode:0];
  586. });
  587. });
  588. [self complete];
  589. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  590. NSInteger errorCode = response.statusCode;
  591. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  592. errorCode = error.code;
  593. // Error
  594. if ([self.delegate respondsToSelector:@selector(listingFavoritesSuccessFailure:metadatas:message:errorCode:)]) {
  595. if (errorCode == 503)
  596. [self.delegate listingFavoritesSuccessFailure:_metadataNet metadatas:nil message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  597. else
  598. [self.delegate listingFavoritesSuccessFailure:_metadataNet metadatas:nil message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  599. }
  600. // Request trusted certificated
  601. if ([error code] == NSURLErrorServerCertificateUntrusted)
  602. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  603. [self complete];
  604. }];
  605. }
  606. #pragma --------------------------------------------------------------------------------------------
  607. #pragma mark ===== Create Folder =====
  608. #pragma --------------------------------------------------------------------------------------------
  609. - (void)createFolder
  610. {
  611. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  612. NSString *nameFolderURL = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  613. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  614. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:_activeUrl];
  615. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  616. [communication setUserAgent:[CCUtility getUserAgent]];
  617. [communication createFolder:nameFolderURL onCommunication:communication withForbiddenCharactersSupported:YES successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  618. // Test active account
  619. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  620. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  621. if ([self.delegate respondsToSelector:@selector(createFolderSuccessFailure:message:errorCode:)])
  622. [self.delegate createFolderSuccessFailure:_metadataNet message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  623. [self complete];
  624. return;
  625. }
  626. NSDictionary *fields = [response allHeaderFields];
  627. _metadataNet.fileID = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-FileId"]];
  628. _metadataNet.date = [CCUtility dateEnUsPosixFromCloud:[fields objectForKey:@"Date"]];
  629. if ([self.delegate respondsToSelector:@selector(createFolderSuccessFailure:message:errorCode:)])
  630. [self.delegate createFolderSuccessFailure:_metadataNet message:nil errorCode:0];
  631. [self complete];
  632. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  633. NSString *message;
  634. if (([_metadataNet.fileName isEqualToString:autoUploadFileName] == YES && [_metadataNet.serverUrl isEqualToString:autoUploadDirectory] == YES))
  635. message = nil;
  636. else
  637. message = [CCError manageErrorOC:response.statusCode error:error];
  638. NSInteger errorCode = response.statusCode;
  639. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  640. errorCode = error.code;
  641. if ([self.delegate respondsToSelector:@selector(createFolderSuccessFailure:message:errorCode:)])
  642. [self.delegate createFolderSuccessFailure:_metadataNet message:message errorCode:errorCode];
  643. // Request trusted certificated
  644. if ([error code] == NSURLErrorServerCertificateUntrusted)
  645. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  646. [self complete];
  647. } errorBeforeRequest:^(NSError *error) {
  648. NSString *message;
  649. if (([_metadataNet.fileName isEqualToString:autoUploadFileName] == YES && [_metadataNet.serverUrl isEqualToString:autoUploadDirectory] == YES))
  650. message = nil;
  651. else {
  652. if (error.code == OCErrorForbidenCharacters)
  653. message = NSLocalizedStringFromTable(@"_forbidden_characters_from_server_", @"Error", nil);
  654. else
  655. message = NSLocalizedStringFromTable(@"_unknow_response_server_", @"Error", nil);
  656. }
  657. // Error
  658. if ([self.delegate respondsToSelector:@selector(createFolderSuccessFailure:message:errorCode:)]) {
  659. if (error.code == 503)
  660. [self.delegate createFolderSuccessFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:error.code];
  661. else
  662. [self.delegate createFolderSuccessFailure:_metadataNet message:message errorCode:error.code];
  663. }
  664. [self complete];
  665. }];
  666. }
  667. - (void)createFolder:(NSString *)fileName serverUrl:(NSString *)serverUrl account:(NSString *)account success:(void(^)(NSString *fileID, NSDate *date))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  668. {
  669. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  670. NSString *serverFileUrl = [NSString stringWithFormat:@"%@/%@", serverUrl, fileName];
  671. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  672. [communication setUserAgent:[CCUtility getUserAgent]];
  673. [communication createFolder:serverFileUrl onCommunication:communication withForbiddenCharactersSupported:YES successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  674. if (![[[NCManageDatabase sharedInstance] getAccountActive].account isEqualToString:account]) {
  675. failure(NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil), k_CCErrorUserNotAvailble);
  676. } else {
  677. NSDictionary *fields = [response allHeaderFields];
  678. NSString *fileID = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-FileId"]];
  679. NSDate *date = [CCUtility dateEnUsPosixFromCloud:[fields objectForKey:@"Date"]];
  680. success(fileID, date);
  681. }
  682. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  683. failure(@"", error.code);
  684. } errorBeforeRequest:^(NSError *error) {
  685. NSString *message;
  686. if (error.code == OCErrorForbidenCharacters)
  687. message = NSLocalizedStringFromTable(@"_forbidden_characters_from_server_", @"Error", nil);
  688. else
  689. message = NSLocalizedStringFromTable(@"_unknow_response_server_", @"Error", nil);
  690. failure(message, error.code);
  691. }];
  692. }
  693. #pragma --------------------------------------------------------------------------------------------
  694. #pragma mark ===== Delete =====
  695. #pragma --------------------------------------------------------------------------------------------
  696. - (void)deleteFileOrFolder
  697. {
  698. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  699. NSString *serverFileUrl = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  700. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  701. [communication setUserAgent:[CCUtility getUserAgent]];
  702. [communication deleteFileOrFolder:serverFileUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  703. // Test active account
  704. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  705. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  706. if ([self.delegate respondsToSelector:@selector(deleteFileOrFolderSuccessFailure:message:errorCode:)])
  707. [self.delegate deleteFileOrFolderSuccessFailure:_metadataNet message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  708. [self complete];
  709. return;
  710. }
  711. if ([_metadataNet.selector rangeOfString:selectorDelete].location != NSNotFound && [self.delegate respondsToSelector:@selector(deleteFileOrFolderSuccessFailure:message:errorCode:)])
  712. [self.delegate deleteFileOrFolderSuccessFailure:_metadataNet message:@"" errorCode:0];
  713. [self complete];
  714. } failureRquest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  715. NSString *message = [NSString new];
  716. NSInteger errorCode = [self deleteFileOrFolderFailureServerUrl:_metadataNet.serverUrl response:response error:error message:&message];
  717. if ([self.delegate respondsToSelector:@selector(deleteFileOrFolderSuccessFailure:message:errorCode:)]) {
  718. [self.delegate deleteFileOrFolderSuccessFailure:_metadataNet message:message errorCode:errorCode];
  719. }
  720. [self complete];
  721. }];
  722. }
  723. - (void)deleteFileOrFolder:(NSString *)fileName serverUrl:(NSString *)serverUrl success:(void (^)(void))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  724. {
  725. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  726. NSString *serverFileUrl = [NSString stringWithFormat:@"%@/%@", serverUrl, fileName];
  727. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  728. [communication setUserAgent:[CCUtility getUserAgent]];
  729. [communication deleteFileOrFolder:serverFileUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  730. success();
  731. } failureRquest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  732. NSString *message = [NSString new];
  733. NSInteger errorCode = [self deleteFileOrFolderFailureServerUrl:serverUrl response:response error:error message:&message];
  734. failure(message, errorCode);
  735. }];
  736. }
  737. - (NSInteger)deleteFileOrFolderFailureServerUrl:(NSString *)serverUrl response:(NSHTTPURLResponse *)response error:(NSError *)error message:(NSString **)message
  738. {
  739. NSInteger errorCode = response.statusCode;
  740. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  741. errorCode = error.code;
  742. // Error
  743. if (errorCode == 503)
  744. *message = NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil);
  745. else
  746. *message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  747. // Request trusted certificated
  748. if ([error code] == NSURLErrorServerCertificateUntrusted)
  749. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  750. // Activity
  751. [[NCManageDatabase sharedInstance] addActivityClient:serverUrl fileID:@"" action:k_activityDebugActionDeleteFileFolder selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  752. return errorCode;
  753. }
  754. #pragma --------------------------------------------------------------------------------------------
  755. #pragma mark ===== Move =====
  756. #pragma --------------------------------------------------------------------------------------------
  757. - (void)moveFileOrFolder
  758. {
  759. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  760. NSString *origineURL = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  761. NSString *destinazioneURL = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrlTo, _metadataNet.fileNameTo];
  762. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  763. [communication setUserAgent:[CCUtility getUserAgent]];
  764. [communication moveFileOrFolder:origineURL toDestiny:destinazioneURL onCommunication:communication withForbiddenCharactersSupported:YES successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  765. if ([_metadataNet.selector isEqualToString:selectorRename] && [self.delegate respondsToSelector:@selector(renameSuccess:)])
  766. [self.delegate renameSuccess:_metadataNet];
  767. if ([_metadataNet.selector rangeOfString:selectorMove].location != NSNotFound && [self.delegate respondsToSelector:@selector(moveSuccess:)])
  768. [self.delegate moveSuccess:_metadataNet];
  769. [self complete];
  770. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  771. NSInteger errorCode = response.statusCode;
  772. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  773. errorCode = error.code;
  774. if ([self.delegate respondsToSelector:@selector(renameMoveFileOrFolderFailure:message:errorCode:)])
  775. [self.delegate renameMoveFileOrFolderFailure:_metadataNet message:[CCError manageErrorOC:response.statusCode error:error] errorCode:errorCode];
  776. // Request trusted certificated
  777. if ([error code] == NSURLErrorServerCertificateUntrusted)
  778. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  779. [self complete];
  780. } errorBeforeRequest:^(NSError *error) {
  781. NSString *message;
  782. if (error.code == OCErrorMovingTheDestinyAndOriginAreTheSame) {
  783. message = NSLocalizedStringFromTable(@"_error_folder_destiny_is_the_same_", @"Error", nil);
  784. } else if (error.code == OCErrorMovingFolderInsideHimself) {
  785. message = NSLocalizedStringFromTable(@"_error_folder_destiny_is_the_same_", @"Error", nil);
  786. } else if (error.code == OCErrorMovingDestinyNameHaveForbiddenCharacters) {
  787. message = NSLocalizedStringFromTable(@"_forbidden_characters_from_server_", @"Error", nil);
  788. } else {
  789. message = NSLocalizedStringFromTable(@"_unknow_response_server_", @"Error", nil);
  790. }
  791. // Error
  792. if ([self.delegate respondsToSelector:@selector(renameMoveFileOrFolderFailure:message:errorCode:)]) {
  793. if (error.code == 503)
  794. [self.delegate renameMoveFileOrFolderFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:error.code];
  795. else
  796. [self.delegate renameMoveFileOrFolderFailure:_metadataNet message:message errorCode:error.code];
  797. }
  798. [self complete];
  799. }];
  800. }
  801. #pragma --------------------------------------------------------------------------------------------
  802. #pragma mark ===== ReadFile =====
  803. #pragma --------------------------------------------------------------------------------------------
  804. - (void)readFile
  805. {
  806. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  807. NSString *fileName;
  808. if (_metadataNet.fileName) {
  809. fileName = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  810. } else {
  811. _metadataNet.fileName = @".";
  812. fileName = _metadataNet.serverUrl;
  813. }
  814. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  815. [communication setUserAgent:[CCUtility getUserAgent]];
  816. [communication readFile:fileName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  817. // Test active account
  818. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  819. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  820. if ([self.delegate respondsToSelector:@selector(readFileSuccessFailure:metadata:message:errorCode:)])
  821. [self.delegate readFileSuccessFailure:_metadataNet metadata:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  822. [self complete];
  823. return;
  824. }
  825. BOOL isFolderEncrypted = [CCUtility isFolderEncrypted:_metadataNet.serverUrl account:_metadataNet.account];
  826. if ([recordAccount.account isEqualToString:_metadataNet.account] && [items count] > 0) {
  827. tableMetadata *metadata = [tableMetadata new];
  828. OCFileDto *itemDto = [items objectAtIndex:0];
  829. NSString *directoryID = [[NCManageDatabase sharedInstance] getDirectoryID:_metadataNet.serverUrl];
  830. if (directoryID) {
  831. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  832. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:_activeUrl];
  833. NSString *directoryUser = [CCUtility getDirectoryActiveUser:_activeUser activeUrl:_activeUrl];
  834. metadata = [CCUtility trasformedOCFileToCCMetadata:itemDto fileName:_metadataNet.fileName serverUrl:_metadataNet.serverUrl directoryID:directoryID autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:_metadataNet.account directoryUser:directoryUser isFolderEncrypted:isFolderEncrypted];
  835. if([self.delegate respondsToSelector:@selector(readFileSuccessFailure:metadata:message:errorCode:)])
  836. [self.delegate readFileSuccessFailure:_metadataNet metadata:metadata message:nil errorCode:0];
  837. } else {
  838. if([self.delegate respondsToSelector:@selector(readFileSuccessFailure:metadata:message:errorCode:)])
  839. [self.delegate readFileSuccessFailure:_metadataNet metadata:nil message:@"Directory not found" errorCode:k_CCErrorInternalError];
  840. }
  841. }
  842. // BUG 1038
  843. if ([items count] == 0) {
  844. #ifndef EXTENSION
  845. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  846. [appDelegate messageNotification:@"Server error" description:@"Read File WebDAV : [items NULL] please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:k_CCErrorInternalError];
  847. #endif
  848. if([self.delegate respondsToSelector:@selector(readFileSuccessFailure:metadata:message:errorCode:)])
  849. [self.delegate readFileSuccessFailure:_metadataNet metadata:nil message:@"Read File WebDAV : [items NULL] please fix" errorCode:k_CCErrorInternalError];
  850. }
  851. [self complete];
  852. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  853. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  854. _metadataNet.errorRetry++;
  855. NSInteger errorCode = response.statusCode;
  856. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  857. errorCode = error.code;
  858. // Error
  859. if ([self.delegate respondsToSelector:@selector(readFileSuccessFailure:metadata:message:errorCode:)] && [recordAccount.account isEqualToString:_metadataNet.account]) {
  860. if (errorCode == 503)
  861. [self.delegate readFileSuccessFailure:_metadataNet metadata:nil message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  862. else
  863. [self.delegate readFileSuccessFailure:_metadataNet metadata:nil message:[CCError manageErrorOC:response.statusCode error:error] errorCode:errorCode];
  864. }
  865. // Request trusted certificated
  866. if ([error code] == NSURLErrorServerCertificateUntrusted)
  867. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  868. [self complete];
  869. }];
  870. }
  871. #pragma --------------------------------------------------------------------------------------------
  872. #pragma mark ===== Shared =====
  873. #pragma --------------------------------------------------------------------------------------------
  874. - (void)readShareServer
  875. {
  876. #ifndef EXTENSION
  877. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  878. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  879. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  880. [communication setUserAgent:[CCUtility getUserAgent]];
  881. [communication readSharedByServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  882. // Test active account
  883. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  884. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  885. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)])
  886. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  887. [self complete];
  888. return;
  889. }
  890. BOOL openWindow = NO;
  891. [appDelegate.sharesID removeAllObjects];
  892. if ([recordAccount.account isEqualToString:_metadataNet.account]) {
  893. for (OCSharedDto *item in items)
  894. [appDelegate.sharesID setObject:item forKey:[@(item.idRemoteShared) stringValue]];
  895. if ([_metadataNet.selector isEqual:selectorOpenWindowShare]) openWindow = YES;
  896. if ([_metadataNet.action isEqual:actionUpdateShare]) openWindow = YES;
  897. if ([_metadataNet.action isEqual:actionShare]) openWindow = YES;
  898. if ([_metadataNet.action isEqual:actionShareWith]) openWindow = YES;
  899. }
  900. if([self.delegate respondsToSelector:@selector(readSharedSuccess:items:openWindow:)])
  901. [self.delegate readSharedSuccess:_metadataNet items:appDelegate.sharesID openWindow:openWindow];
  902. [self complete];
  903. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  904. NSInteger errorCode = response.statusCode;
  905. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  906. errorCode = error.code;
  907. // Error
  908. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  909. if (errorCode == 503)
  910. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  911. else
  912. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  913. }
  914. // Request trusted certificated
  915. if ([error code] == NSURLErrorServerCertificateUntrusted)
  916. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  917. [self complete];
  918. }];
  919. #endif
  920. }
  921. - (void)share
  922. {
  923. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  924. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  925. [communication setUserAgent:[CCUtility getUserAgent]];
  926. [communication shareFileOrFolderByServer:[_activeUrl stringByAppendingString:@"/"] andFileOrFolderPath:[_metadataNet.fileName encodeString:NSUTF8StringEncoding] andPassword:[_metadataNet.password encodeString:NSUTF8StringEncoding] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  927. [self readShareServer];
  928. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  929. NSInteger errorCode = response.statusCode;
  930. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  931. errorCode = error.code;
  932. // Error
  933. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  934. if (errorCode == 503)
  935. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  936. else
  937. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  938. }
  939. // Request trusted certificated
  940. if ([error code] == NSURLErrorServerCertificateUntrusted)
  941. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  942. [self complete];
  943. }];
  944. }
  945. // * @param shareeType -> NSInteger: to set the type of sharee (user/group/federated)
  946. - (void)shareWith
  947. {
  948. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  949. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  950. [communication setUserAgent:[CCUtility getUserAgent]];
  951. [communication shareWith:_metadataNet.share shareeType:_metadataNet.shareeType inServer:[_activeUrl stringByAppendingString:@"/"] andFileOrFolderPath:[_metadataNet.fileName encodeString:NSUTF8StringEncoding] andPermissions:_metadataNet.sharePermission onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  952. [self readShareServer];
  953. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  954. NSInteger errorCode = response.statusCode;
  955. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  956. errorCode = error.code;
  957. // Error
  958. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  959. if (errorCode == 503)
  960. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  961. else
  962. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  963. }
  964. // Request trusted certificated
  965. if ([error code] == NSURLErrorServerCertificateUntrusted)
  966. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  967. [self complete];
  968. }];
  969. }
  970. - (void)updateShare
  971. {
  972. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  973. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  974. [communication setUserAgent:[CCUtility getUserAgent]];
  975. [communication updateShare:[_metadataNet.share intValue] ofServerPath:[_activeUrl stringByAppendingString:@"/"] withPasswordProtect:[_metadataNet.password encodeString:NSUTF8StringEncoding] andExpirationTime:_metadataNet.expirationTime andPermissions:_metadataNet.sharePermission onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  976. [self readShareServer];
  977. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  978. #ifndef EXTENSION
  979. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  980. [appDelegate messageNotification:@"_error_" description:[CCError manageErrorOC:response.statusCode error:error] visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:error.code];
  981. #endif
  982. NSInteger errorCode = response.statusCode;
  983. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  984. errorCode = error.code;
  985. // Error
  986. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  987. if (errorCode == 503)
  988. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  989. else
  990. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  991. }
  992. // Request trusted certificated
  993. if ([error code] == NSURLErrorServerCertificateUntrusted)
  994. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  995. [self complete];
  996. }];
  997. }
  998. - (void)unShare
  999. {
  1000. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1001. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1002. [communication setUserAgent:[CCUtility getUserAgent]];
  1003. [communication unShareFileOrFolderByServer:[_activeUrl stringByAppendingString:@"/"] andIdRemoteShared:[_metadataNet.share intValue] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1004. if([self.delegate respondsToSelector:@selector(unShareSuccess:)])
  1005. [self.delegate unShareSuccess:_metadataNet];
  1006. [self complete];
  1007. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1008. #ifndef EXTENSION
  1009. [(AppDelegate *)[[UIApplication sharedApplication] delegate] messageNotification:@"_error_" description:[CCError manageErrorOC:response.statusCode error:error] visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:error.code];
  1010. #endif
  1011. NSInteger errorCode = response.statusCode;
  1012. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1013. errorCode = error.code;
  1014. // Error
  1015. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  1016. if (errorCode == 503)
  1017. [self.delegate shareFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1018. else
  1019. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1020. }
  1021. // Request trusted certificated
  1022. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1023. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1024. [self complete];
  1025. }];
  1026. }
  1027. - (void)getUserAndGroup
  1028. {
  1029. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1030. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1031. [communication setUserAgent:[CCUtility getUserAgent]];
  1032. [communication searchUsersAndGroupsWith:_metadataNet.options forPage:1 with:50 ofServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *itemList, NSString *redirectedServer) {
  1033. if([self.delegate respondsToSelector:@selector(getUserAndGroupSuccess:items:)])
  1034. [self.delegate getUserAndGroupSuccess:_metadataNet items:itemList];
  1035. [self complete];
  1036. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1037. NSInteger errorCode = response.statusCode;
  1038. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1039. errorCode = error.code;
  1040. // Error
  1041. if ([self.delegate respondsToSelector:@selector(getUserAndGroupFailure:message:errorCode:)]) {
  1042. if (errorCode == 503)
  1043. [self.delegate getUserAndGroupFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1044. else
  1045. [self.delegate getUserAndGroupFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1046. }
  1047. // Request trusted certificated
  1048. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1049. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1050. [self complete];
  1051. }];
  1052. }
  1053. - (void)getSharePermissionsFile
  1054. {
  1055. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1056. NSString *fileName = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  1057. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1058. [communication setUserAgent:[CCUtility getUserAgent]];
  1059. [communication getSharePermissionsFile:fileName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *permissions, NSString *redirectedServer) {
  1060. // Test active account
  1061. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  1062. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  1063. if ([self.delegate respondsToSelector:@selector(getSharePermissionsFileFailure:message:errorCode:)])
  1064. [self.delegate getSharePermissionsFileFailure:_metadataNet message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  1065. [self complete];
  1066. return;
  1067. }
  1068. if([self.delegate respondsToSelector:@selector(getSharePermissionsFileSuccess:permissions:)])
  1069. [self.delegate getSharePermissionsFileSuccess:_metadataNet permissions:permissions];
  1070. [self complete];
  1071. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1072. NSInteger errorCode = response.statusCode;
  1073. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1074. errorCode = error.code;
  1075. // Error
  1076. if ([self.delegate respondsToSelector:@selector(getSharePermissionsFileFailure:message:errorCode:)]) {
  1077. if (errorCode == 503)
  1078. [self.delegate getSharePermissionsFileFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1079. else
  1080. [self.delegate getSharePermissionsFileFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1081. }
  1082. // Request trusted certificated
  1083. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1084. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1085. [self complete];
  1086. }];
  1087. }
  1088. #pragma --------------------------------------------------------------------------------------------
  1089. #pragma mark ===== Activity =====
  1090. #pragma --------------------------------------------------------------------------------------------
  1091. - (void)getActivityServer
  1092. {
  1093. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1094. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1095. [communication setUserAgent:[CCUtility getUserAgent]];
  1096. [communication getActivityServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfActivity, NSString *redirectedServer) {
  1097. // Test active account
  1098. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  1099. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  1100. if ([self.delegate respondsToSelector:@selector(getActivityServerSuccessFailure:listOfActivity:message:errorCode:)])
  1101. [self.delegate getActivityServerSuccessFailure:_metadataNet listOfActivity:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  1102. [self complete];
  1103. return;
  1104. }
  1105. if ([self.delegate respondsToSelector:@selector(getActivityServerSuccessFailure:listOfActivity:message:errorCode:)])
  1106. [self.delegate getActivityServerSuccessFailure:_metadataNet listOfActivity:listOfActivity message:nil errorCode:0];
  1107. [self complete];
  1108. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1109. NSInteger errorCode = response.statusCode;
  1110. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1111. errorCode = error.code;
  1112. // Error
  1113. if ([self.delegate respondsToSelector:@selector(getActivityServerSuccessFailure:listOfActivity:message:errorCode:)]) {
  1114. if (errorCode == 503)
  1115. [self.delegate getActivityServerSuccessFailure:_metadataNet listOfActivity:nil message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1116. else
  1117. [self.delegate getActivityServerSuccessFailure:_metadataNet listOfActivity:nil message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1118. }
  1119. // Request trusted certificated
  1120. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1121. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1122. [self complete];
  1123. }];
  1124. }
  1125. #pragma --------------------------------------------------------------------------------------------
  1126. #pragma mark ===== External Sites =====
  1127. #pragma --------------------------------------------------------------------------------------------
  1128. - (void)getExternalSitesServer
  1129. {
  1130. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1131. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1132. [communication setUserAgent:[CCUtility getUserAgent]];
  1133. [communication getExternalSitesServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfExternalSites, NSString *redirectedServer) {
  1134. // Test active account
  1135. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  1136. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  1137. if ([self.delegate respondsToSelector:@selector(getExternalSitesServerSuccessFailure:listOfExternalSites:message:errorCode:)])
  1138. [self.delegate getExternalSitesServerSuccessFailure:_metadataNet listOfExternalSites:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  1139. [self complete];
  1140. return;
  1141. }
  1142. if ([self.delegate respondsToSelector:@selector(getExternalSitesServerSuccessFailure:listOfExternalSites:message:errorCode:)])
  1143. [self.delegate getExternalSitesServerSuccessFailure:_metadataNet listOfExternalSites:listOfExternalSites message:nil errorCode:0];
  1144. [self complete];
  1145. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1146. NSInteger errorCode = response.statusCode;
  1147. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1148. errorCode = error.code;
  1149. // Error
  1150. if ([self.delegate respondsToSelector:@selector(getExternalSitesServerSuccessFailure:listOfExternalSites:message:errorCode:)]) {
  1151. if (errorCode == 503)
  1152. [self.delegate getExternalSitesServerSuccessFailure:_metadataNet listOfExternalSites:nil message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1153. else
  1154. [self.delegate getExternalSitesServerSuccessFailure:_metadataNet listOfExternalSites:nil message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1155. }
  1156. // Request trusted certificated
  1157. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1158. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1159. [self complete];
  1160. }];
  1161. }
  1162. #pragma --------------------------------------------------------------------------------------------
  1163. #pragma mark ===== Middleware Ping =====
  1164. #pragma --------------------------------------------------------------------------------------------
  1165. /*
  1166. - (void)middlewarePing
  1167. {
  1168. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1169. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  1170. [communication setUserAgent:[CCUtility getUserAgent]];
  1171. [communication getMiddlewarePing:_metadataNet.serverUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfExternalSites, NSString *redirectedServer) {
  1172. [self complete];
  1173. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1174. NSInteger errorCode = response.statusCode;
  1175. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1176. errorCode = error.code;
  1177. // Error
  1178. if ([self.delegate respondsToSelector:@selector(getExternalSitesServerFailure:message:errorCode:)]) {
  1179. if (errorCode == 503)
  1180. [self.delegate getExternalSitesServerFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1181. else
  1182. [self.delegate getExternalSitesServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1183. }
  1184. // Request trusted certificated
  1185. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1186. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1187. [self complete];
  1188. }];
  1189. }
  1190. */
  1191. #pragma --------------------------------------------------------------------------------------------
  1192. #pragma mark ===== Notification =====
  1193. #pragma --------------------------------------------------------------------------------------------
  1194. - (void)getNotificationServer
  1195. {
  1196. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1197. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1198. [communication setUserAgent:[CCUtility getUserAgent]];
  1199. [communication getNotificationServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfNotifications, NSString *redirectedServer) {
  1200. // Test active account
  1201. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  1202. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  1203. if ([self.delegate respondsToSelector:@selector(getNotificationServerSuccessFailure:listOfNotifications:message:errorCode:)])
  1204. [self.delegate getNotificationServerSuccessFailure:_metadataNet listOfNotifications:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  1205. [self complete];
  1206. return;
  1207. }
  1208. if ([self.delegate respondsToSelector:@selector(getNotificationServerSuccessFailure:listOfNotifications:message:errorCode:)])
  1209. [self.delegate getNotificationServerSuccessFailure:_metadataNet listOfNotifications:listOfNotifications message:nil errorCode:0];
  1210. [self complete];
  1211. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1212. NSInteger errorCode = response.statusCode;
  1213. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1214. errorCode = error.code;
  1215. // Error
  1216. if ([self.delegate respondsToSelector:@selector(getNotificationServerSuccessFailure:listOfNotifications:message:errorCode:)]) {
  1217. if (errorCode == 503)
  1218. [self.delegate getNotificationServerSuccessFailure:_metadataNet listOfNotifications:nil message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1219. else
  1220. [self.delegate getNotificationServerSuccessFailure:_metadataNet listOfNotifications:nil message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1221. }
  1222. // Request trusted certificated
  1223. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1224. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1225. // Activity
  1226. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionGetNotification selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1227. [self complete];
  1228. }];
  1229. }
  1230. - (void)setNotificationServer
  1231. {
  1232. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1233. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1234. [communication setUserAgent:[CCUtility getUserAgent]];
  1235. NSString *type = _metadataNet.options;
  1236. [communication setNotificationServer:_metadataNet.serverUrl type:type onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1237. // Test active account
  1238. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  1239. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  1240. if ([self.delegate respondsToSelector:@selector(setNotificationServerSuccessFailure:message:errorCode:)])
  1241. [self.delegate setNotificationServerSuccessFailure:_metadataNet message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  1242. [self complete];
  1243. return;
  1244. }
  1245. if ([self.delegate respondsToSelector:@selector(setNotificationServerSuccessFailure:message:errorCode:)])
  1246. [self.delegate setNotificationServerSuccessFailure:_metadataNet message:nil errorCode:0];
  1247. [self complete];
  1248. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1249. NSInteger errorCode = response.statusCode;
  1250. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1251. errorCode = error.code;
  1252. // Error
  1253. if ([self.delegate respondsToSelector:@selector(setNotificationServerSuccessFailure:message:errorCode:)]) {
  1254. if (errorCode == 503)
  1255. [self.delegate setNotificationServerSuccessFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1256. else
  1257. [self.delegate setNotificationServerSuccessFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1258. }
  1259. // Request trusted certificated
  1260. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1261. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1262. [self complete];
  1263. }];
  1264. }
  1265. - (void)subscribingNextcloudServer
  1266. {
  1267. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1268. communication.kindOfCredential = credentialNotSet;
  1269. [communication setUserAgent:[CCUtility getUserAgent]];
  1270. NSDictionary *parameter = _metadataNet.options;
  1271. NSString *pushToken = [parameter objectForKey:@"pushToken"];
  1272. NSString *pushTokenHash = [parameter objectForKey:@"pushTokenHash"];
  1273. NSString *devicePublicKey = [parameter objectForKey:@"devicePublicKey"];
  1274. // encode URL
  1275. devicePublicKey = [devicePublicKey stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
  1276. [communication subscribingNextcloudServerPush:_activeUrl pushTokenHash:pushTokenHash devicePublicKey:devicePublicKey proxyServerPath: [NCBrandOptions sharedInstance].pushNotificationServer onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *deviceIdentifier, NSString *signature, NSString *redirectedServer) {
  1277. // encode URL
  1278. deviceIdentifier = [deviceIdentifier stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
  1279. signature = [signature stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
  1280. publicKey = [publicKey stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
  1281. [communication subscribingPushProxy:[NCBrandOptions sharedInstance].pushNotificationServer pushToken:pushToken deviceIdentifier:deviceIdentifier deviceIdentifierSignature:signature userPublicKey:publicKey onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1282. // Activity
  1283. [[NCManageDatabase sharedInstance] addActivityClient:[NCBrandOptions sharedInstance].pushNotificationServer fileID:@"" action:k_activityDebugActionPushProxy selector:@"" note:@"Service registered." type:k_activityTypeSuccess verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1284. [self complete];
  1285. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1286. NSInteger errorCode = response.statusCode;
  1287. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1288. errorCode = error.code;
  1289. // Error
  1290. if ([self.delegate respondsToSelector:@selector(subscribingNextcloudServerFailure:message:errorCode:)]) {
  1291. if (errorCode == 503)
  1292. [self.delegate subscribingNextcloudServerFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1293. else
  1294. [self.delegate subscribingNextcloudServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1295. }
  1296. // Request trusted certificated
  1297. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1298. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1299. // Activity
  1300. [[NCManageDatabase sharedInstance] addActivityClient:[NCBrandOptions sharedInstance].pushNotificationServer fileID:@"" action:k_activityDebugActionPushProxy selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1301. [self complete];
  1302. }];
  1303. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1304. NSInteger errorCode = response.statusCode;
  1305. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1306. errorCode = error.code;
  1307. // Error
  1308. if ([self.delegate respondsToSelector:@selector(subscribingNextcloudServerFailure:message:errorCode:)]) {
  1309. if (errorCode == 503)
  1310. [self.delegate subscribingNextcloudServerFailure:_metadataNet message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1311. else
  1312. [self.delegate subscribingNextcloudServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1313. }
  1314. // Request trusted certificated
  1315. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1316. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1317. // Activity
  1318. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionServerPush selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1319. [self complete];
  1320. }];
  1321. }
  1322. #pragma --------------------------------------------------------------------------------------------
  1323. #pragma mark ===== User Profile =====
  1324. #pragma --------------------------------------------------------------------------------------------
  1325. - (void)getUserProfile
  1326. {
  1327. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1328. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1329. [communication setUserAgent:[CCUtility getUserAgent]];
  1330. [communication getUserProfileServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCUserProfile *userProfile, NSString *redirectedServer) {
  1331. // Test active account
  1332. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  1333. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  1334. if ([self.delegate respondsToSelector:@selector(getUserProfileSuccessFailure:userProfile:message:errorCode:)])
  1335. [self.delegate getUserProfileSuccessFailure:_metadataNet userProfile:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  1336. [self complete];
  1337. return;
  1338. }
  1339. if ([self.delegate respondsToSelector:@selector(getUserProfileSuccessFailure:userProfile:message:errorCode:)])
  1340. [self.delegate getUserProfileSuccessFailure:_metadataNet userProfile:userProfile message:nil errorCode:0];
  1341. [self complete];
  1342. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1343. NSInteger errorCode = response.statusCode;
  1344. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1345. errorCode = error.code;
  1346. // Error
  1347. if ([self.delegate respondsToSelector:@selector(getUserProfileSuccessFailure:userProfile:message:errorCode:)]) {
  1348. if (errorCode == 503)
  1349. [self.delegate getUserProfileSuccessFailure:_metadataNet userProfile:nil message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1350. else
  1351. [self.delegate getUserProfileSuccessFailure:_metadataNet userProfile:nil message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1352. }
  1353. // Request trusted certificated
  1354. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1355. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1356. [self complete];
  1357. }];
  1358. }
  1359. #pragma --------------------------------------------------------------------------------------------
  1360. #pragma mark ===== Capabilities =====
  1361. #pragma --------------------------------------------------------------------------------------------
  1362. - (void)getCapabilitiesOfServer
  1363. {
  1364. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1365. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1366. [communication setUserAgent:[CCUtility getUserAgent]];
  1367. [communication getCapabilitiesOfServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCCapabilities *capabilities, NSString *redirectedServer) {
  1368. // Test active account
  1369. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  1370. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  1371. if ([self.delegate respondsToSelector:@selector(getCapabilitiesOfServerSuccessFailure:capabilities:message:errorCode:)])
  1372. [self.delegate getCapabilitiesOfServerSuccessFailure:_metadataNet capabilities:nil message:NSLocalizedStringFromTable(@"_error_user_not_available_", @"Error", nil) errorCode:k_CCErrorUserNotAvailble];
  1373. [self complete];
  1374. return;
  1375. }
  1376. if ([self.delegate respondsToSelector:@selector(getCapabilitiesOfServerSuccessFailure:capabilities:message:errorCode:)])
  1377. [self.delegate getCapabilitiesOfServerSuccessFailure:_metadataNet capabilities:capabilities message:nil errorCode:0];
  1378. [self complete];
  1379. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1380. NSInteger errorCode = response.statusCode;
  1381. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1382. errorCode = error.code;
  1383. // Error
  1384. if ([self.delegate respondsToSelector:@selector(getCapabilitiesOfServerSuccessFailure:capabilities:message:errorCode:)]) {
  1385. if (errorCode == 503)
  1386. [self.delegate getCapabilitiesOfServerSuccessFailure:_metadataNet capabilities:nil message:NSLocalizedStringFromTable(@"_server_error_retry_", @"Error", nil) errorCode:errorCode];
  1387. else
  1388. [self.delegate getCapabilitiesOfServerSuccessFailure:_metadataNet capabilities:nil message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1389. }
  1390. // Request trusted certificated
  1391. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1392. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1393. // Activity
  1394. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionCapabilities selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1395. [self complete];
  1396. }];
  1397. }
  1398. #pragma --------------------------------------------------------------------------------------------
  1399. #pragma mark ===== End-to-End Encryption =====
  1400. #pragma --------------------------------------------------------------------------------------------
  1401. - (void)getEndToEndPublicKeys
  1402. {
  1403. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1404. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1405. [communication setUserAgent:[CCUtility getUserAgent]];
  1406. [communication getEndToEndPublicKeys:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *redirectedServer) {
  1407. _metadataNet.key = publicKey;
  1408. if ([self.delegate respondsToSelector:@selector(getEndToEndPublicKeysSuccess:)])
  1409. [self.delegate getEndToEndPublicKeysSuccess:_metadataNet];
  1410. [self complete];
  1411. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1412. NSInteger errorCode = response.statusCode;
  1413. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1414. errorCode = error.code;
  1415. // Error
  1416. if ([self.delegate respondsToSelector:@selector(getEndToEndPublicKeysFailure:message:errorCode:)])
  1417. [self.delegate getEndToEndPublicKeysFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1418. // Request trusted certificated
  1419. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1420. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1421. [self complete];
  1422. }];
  1423. }
  1424. - (void)getEndToEndPrivateKeyCipher
  1425. {
  1426. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1427. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1428. [communication setUserAgent:[CCUtility getUserAgent]];
  1429. [communication getEndToEndPrivateKeyCipher:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *privateKeyChiper, NSString *redirectedServer) {
  1430. _metadataNet.key = privateKeyChiper;
  1431. if ([self.delegate respondsToSelector:@selector(getEndToEndPrivateKeyCipherSuccess:)])
  1432. [self.delegate getEndToEndPrivateKeyCipherSuccess:_metadataNet];
  1433. [self complete];
  1434. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1435. NSInteger errorCode = response.statusCode;
  1436. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1437. errorCode = error.code;
  1438. // Error
  1439. if ([self.delegate respondsToSelector:@selector(getEndToEndPrivateKeyCipherFailure:message:errorCode:)])
  1440. [self.delegate getEndToEndPrivateKeyCipherFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1441. // Request trusted certificated
  1442. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1443. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1444. [self complete];
  1445. }];
  1446. }
  1447. - (void)signEndToEndPublicKey
  1448. {
  1449. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1450. // URL Encode
  1451. NSString *publicKey = [CCUtility URLEncodeStringFromString:_metadataNet.key];
  1452. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1453. [communication setUserAgent:[CCUtility getUserAgent]];
  1454. [communication signEndToEndPublicKey:[_activeUrl stringByAppendingString:@"/"] publicKey:publicKey onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *redirectedServer) {
  1455. _metadataNet.key = publicKey;
  1456. if ([self.delegate respondsToSelector:@selector(signEndToEndPublicKeySuccess:)])
  1457. [self.delegate signEndToEndPublicKeySuccess:_metadataNet];
  1458. [self complete];
  1459. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1460. NSInteger errorCode = response.statusCode;
  1461. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1462. errorCode = error.code;
  1463. // Error
  1464. if ([self.delegate respondsToSelector:@selector(signEndToEndPublicKeyFailure:message:errorCode:)])
  1465. [self.delegate signEndToEndPublicKeyFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1466. // Request trusted certificated
  1467. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1468. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1469. [self complete];
  1470. }];
  1471. }
  1472. - (void)storeEndToEndPrivateKeyCipher
  1473. {
  1474. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1475. // URL Encode
  1476. NSString *privateKeyChiper = [CCUtility URLEncodeStringFromString:_metadataNet.keyCipher];
  1477. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1478. [communication setUserAgent:[CCUtility getUserAgent]];
  1479. [communication storeEndToEndPrivateKeyCipher:[_activeUrl stringByAppendingString:@"/"] privateKeyChiper:privateKeyChiper onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *privateKey, NSString *redirectedServer) {
  1480. if ([self.delegate respondsToSelector:@selector(storeEndToEndPrivateKeyCipherSuccess:)])
  1481. [self.delegate storeEndToEndPrivateKeyCipherSuccess:_metadataNet];
  1482. [self complete];
  1483. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1484. NSInteger errorCode = response.statusCode;
  1485. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1486. errorCode = error.code;
  1487. // Error
  1488. if ([self.delegate respondsToSelector:@selector(storeEndToEndPrivateKeyCipherFailure:message:errorCode:)])
  1489. [self.delegate storeEndToEndPrivateKeyCipherFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1490. // Request trusted certificated
  1491. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1492. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1493. [self complete];
  1494. }];
  1495. }
  1496. - (void)deleteEndToEndPublicKey
  1497. {
  1498. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1499. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1500. [communication setUserAgent:[CCUtility getUserAgent]];
  1501. [communication deleteEndToEndPublicKey:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1502. if ([self.delegate respondsToSelector:@selector(deleteEndToEndPublicKeySuccess:)])
  1503. [self.delegate deleteEndToEndPublicKeySuccess:_metadataNet];
  1504. [self complete];
  1505. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1506. NSInteger errorCode = response.statusCode;
  1507. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1508. errorCode = error.code;
  1509. // Error
  1510. if ([self.delegate respondsToSelector:@selector(deleteEndToEndPublicKeyFailure:message:errorCode:)])
  1511. [self.delegate deleteEndToEndPublicKeyFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1512. // Request trusted certificated
  1513. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1514. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1515. [self complete];
  1516. }];
  1517. }
  1518. - (void)deleteEndToEndPrivateKey
  1519. {
  1520. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1521. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1522. [communication setUserAgent:[CCUtility getUserAgent]];
  1523. [communication deleteEndToEndPrivateKey:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1524. if ([self.delegate respondsToSelector:@selector(deleteEndToEndPrivateKeySuccess:)])
  1525. [self.delegate deleteEndToEndPrivateKeySuccess:_metadataNet];
  1526. [self complete];
  1527. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1528. NSInteger errorCode = response.statusCode;
  1529. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1530. errorCode = error.code;
  1531. // Error
  1532. if ([self.delegate respondsToSelector:@selector(deleteEndToEndPrivateKeyFailure:message:errorCode:)])
  1533. [self.delegate deleteEndToEndPrivateKeyFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1534. // Request trusted certificated
  1535. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1536. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1537. [self complete];
  1538. }];
  1539. }
  1540. - (void)getEndToEndServerPublicKey
  1541. {
  1542. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1543. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1544. [communication setUserAgent:[CCUtility getUserAgent]];
  1545. [communication getEndToEndServerPublicKey:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *redirectedServer) {
  1546. _metadataNet.key = publicKey;
  1547. if ([self.delegate respondsToSelector:@selector(getEndToEndServerPublicKeySuccess:)])
  1548. [self.delegate getEndToEndServerPublicKeySuccess:_metadataNet];
  1549. [self complete];
  1550. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1551. NSInteger errorCode = response.statusCode;
  1552. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1553. errorCode = error.code;
  1554. // Error
  1555. if ([self.delegate respondsToSelector:@selector(getEndToEndServerPublicKeyFailure:message:errorCode:)])
  1556. [self.delegate getEndToEndServerPublicKeyFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1557. // Request trusted certificated
  1558. if ([error code] == NSURLErrorServerCertificateUntrusted)
  1559. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1560. [self complete];
  1561. }];
  1562. }
  1563. @end
  1564. #pragma --------------------------------------------------------------------------------------------
  1565. #pragma mark ===== OCURLSessionManager =====
  1566. #pragma --------------------------------------------------------------------------------------------
  1567. @implementation OCURLSessionManager
  1568. - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
  1569. {
  1570. // The pinnning check
  1571. if ([[CCCertificate sharedManager] checkTrustedChallenge:challenge]) {
  1572. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  1573. } else {
  1574. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  1575. }
  1576. }
  1577. @end