OCNetworking.m 119 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400
  1. //
  2. // OCnetworking.m
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 10/05/15.
  6. // Copyright (c) 2017 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  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)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 ===== Server =====
  104. #pragma --------------------------------------------------------------------------------------------
  105. - (void)checkServerUrl:(NSString *)serverUrl completion:(void (^)(NSString *message, NSInteger errorCode))completion
  106. {
  107. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  108. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  109. [communication setUserAgent:[CCUtility getUserAgent]];
  110. [communication checkServer:serverUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  111. completion(nil, 0);
  112. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  113. NSString *message;
  114. NSInteger errorCode = response.statusCode;
  115. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  116. errorCode = error.code;
  117. // Error
  118. if (errorCode == 503)
  119. message = NSLocalizedString(@"_server_error_retry_", nil);
  120. else
  121. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  122. completion(message, errorCode);
  123. }];
  124. }
  125. - (void)serverStatusUrl:(NSString *)serverUrl completion:(void(^)(NSString *serverProductName, NSInteger versionMajor, NSInteger versionMicro, NSInteger versionMinor, NSString *message, NSInteger errorCode))completion
  126. {
  127. NSString *urlTest = [serverUrl stringByAppendingString:k_serverStatus];
  128. // Remove stored cookies
  129. NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
  130. for (NSHTTPCookie *cookie in [storage cookies])
  131. {
  132. [storage deleteCookie:cookie];
  133. }
  134. NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlTest] cachePolicy:0 timeoutInterval:20.0];
  135. [request addValue:[CCUtility getUserAgent] forHTTPHeaderField:@"User-Agent"];
  136. [request addValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  137. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  138. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self.delegate delegateQueue:nil];
  139. NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
  140. dispatch_sync(dispatch_get_main_queue(), ^{
  141. if (error) {
  142. NSString *message;
  143. NSInteger errorCode;
  144. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  145. errorCode = httpResponse.statusCode;
  146. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  147. errorCode = error.code;
  148. // Error
  149. if (errorCode == 503)
  150. message = NSLocalizedString(@"_server_error_retry_", nil);
  151. else
  152. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  153. completion(nil, 0, 0, 0, message, errorCode);
  154. } else {
  155. NSString *serverProductName = @"";
  156. NSString *serverVersion = @"0.0.0";
  157. NSString *serverVersionString = @"0.0.0";
  158. NSInteger versionMajor = 0;
  159. NSInteger versionMicro = 0;
  160. NSInteger versionMinor = 0;
  161. NSError *error;
  162. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
  163. if (error) {
  164. completion(nil, 0, 0, 0, error.description, error.code);
  165. return;
  166. }
  167. serverProductName = [[jsongParsed valueForKey:@"productname"] lowercaseString];
  168. serverVersion = [jsongParsed valueForKey:@"version"];
  169. serverVersionString = [jsongParsed valueForKey:@"versionstring"];
  170. NSArray *arrayVersion = [serverVersionString componentsSeparatedByString:@"."];
  171. if (arrayVersion.count == 1) {
  172. versionMajor = [arrayVersion[0] integerValue];
  173. } else if (arrayVersion.count == 2) {
  174. versionMajor = [arrayVersion[0] integerValue];
  175. versionMinor = [arrayVersion[1] integerValue];
  176. } else if (arrayVersion.count >= 3) {
  177. versionMajor = [arrayVersion[0] integerValue];
  178. versionMinor = [arrayVersion[1] integerValue];
  179. versionMicro = [arrayVersion[2] integerValue];
  180. }
  181. completion(serverProductName, versionMajor, versionMicro, versionMinor, nil, 0);
  182. }
  183. });
  184. }];
  185. [task resume];
  186. }
  187. #pragma --------------------------------------------------------------------------------------------
  188. #pragma mark ===== download =====
  189. #pragma --------------------------------------------------------------------------------------------
  190. - (NSURLSessionTask *)downloadFileNameServerUrl:(NSString *)fileNameServerUrl fileNameLocalPath:(NSString *)fileNameLocalPath communication:(OCCommunication *)communication success:(void (^)(int64_t length, NSString *etag, NSDate *date))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  191. {
  192. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  193. [communication setUserAgent:[CCUtility getUserAgent]];
  194. NSURLSessionTask *sessionTask = [communication downloadFileSession:fileNameServerUrl toDestiny:fileNameLocalPath defaultPriority:YES onCommunication:communication progress:^(NSProgress *progress) {
  195. //float percent = roundf (progress.fractionCompleted * 100);
  196. } successRequest:^(NSURLResponse *response, NSURL *filePath) {
  197. int64_t totalUnitCount = 0;
  198. NSDate *date = [NSDate date];
  199. NSDateFormatter *dateFormatter = [NSDateFormatter new];
  200. NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
  201. [dateFormatter setLocale:enUSPOSIXLocale];
  202. [dateFormatter setDateFormat:@"EEE, dd MMM y HH:mm:ss zzz"];
  203. NSError *error;
  204. NSDictionary *fields = [(NSHTTPURLResponse*)response allHeaderFields];
  205. NSString *contentLength = [fields objectForKey:@"Content-Length"];
  206. if(contentLength) {
  207. totalUnitCount = (int64_t) [contentLength longLongValue];
  208. }
  209. NSString *dateString = [fields objectForKey:@"Date"];
  210. if (dateString) {
  211. if (![dateFormatter getObjectValue:&date forString:dateString range:nil error:&error]) {
  212. date = [NSDate date];
  213. }
  214. } else {
  215. date = [NSDate date];
  216. }
  217. NSString *etag = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-ETag"]];
  218. if (etag == nil) {
  219. failure(@"Internal error", k_CCErrorInternalError);
  220. } else {
  221. success(totalUnitCount, etag, date);
  222. }
  223. } failureRequest:^(NSURLResponse *response, NSError *error) {
  224. NSString *message;
  225. NSInteger errorCode;
  226. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  227. errorCode = httpResponse.statusCode;
  228. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  229. errorCode = error.code;
  230. // Error
  231. if (errorCode == 503)
  232. message = NSLocalizedString(@"_server_error_retry_", nil);
  233. else
  234. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  235. failure(message, errorCode);
  236. }];
  237. return sessionTask;
  238. }
  239. - (NSURLSessionTask *)downloadFile:(NSString *)url fileNameLocalPath:(NSString *)fileNameLocalPath success:(void (^)())success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  240. {
  241. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  242. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  243. [communication setUserAgent:[CCUtility getUserAgent]];
  244. NSURLSessionTask *sessionTask = [communication downloadFileSession:url toDestiny:fileNameLocalPath defaultPriority:YES onCommunication:communication progress:^(NSProgress *progress) {
  245. //float percent = roundf (progress.fractionCompleted * 100);
  246. } successRequest:^(NSURLResponse *response, NSURL *filePath) {
  247. success();
  248. } failureRequest:^(NSURLResponse *response, NSError *error) {
  249. NSString *message;
  250. NSInteger errorCode;
  251. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  252. errorCode = httpResponse.statusCode;
  253. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  254. errorCode = error.code;
  255. // Error
  256. if (errorCode == 503)
  257. message = NSLocalizedString(@"_server_error_retry_", nil);
  258. else
  259. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  260. failure(message, errorCode);
  261. }];
  262. return sessionTask;
  263. }
  264. #pragma --------------------------------------------------------------------------------------------
  265. #pragma mark ===== upload =====
  266. #pragma --------------------------------------------------------------------------------------------
  267. - (NSURLSessionTask *)uploadFileNameServerUrl:(NSString *)fileNameServerUrl fileNameLocalPath:(NSString *)fileNameLocalPath communication:(OCCommunication *)communication success:(void(^)(NSString *fileID, NSString *etag, NSDate *date))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  268. {
  269. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  270. [communication setUserAgent:[CCUtility getUserAgent]];
  271. NSURLSessionTask *sessionTask = [communication uploadFileSession:fileNameLocalPath toDestiny:fileNameServerUrl onCommunication:communication progress:^(NSProgress *progress) {
  272. //float percent = roundf (progress.fractionCompleted * 100);
  273. } successRequest:^(NSURLResponse *response, NSString *redirectedServer) {
  274. NSDictionary *fields = [(NSHTTPURLResponse*)response allHeaderFields];
  275. NSString *fileID = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-FileId"]];
  276. NSString *etag = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-ETag"]];
  277. NSDate *date = [CCUtility dateEnUsPosixFromCloud:[fields objectForKey:@"Date"]];
  278. success(fileID, etag, date);
  279. } failureRequest:^(NSURLResponse *response, NSString *redirectedServer, NSError *error) {
  280. NSString *message;
  281. NSInteger errorCode;
  282. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  283. errorCode = httpResponse.statusCode;
  284. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  285. errorCode = error.code;
  286. // Error
  287. if (errorCode == 503)
  288. message = NSLocalizedString(@"_server_error_retry_", nil);
  289. else
  290. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  291. failure(message, errorCode);
  292. } failureBeforeRequest:^(NSError *error) {
  293. failure(@"", error.code);
  294. }];
  295. return sessionTask;
  296. }
  297. #pragma --------------------------------------------------------------------------------------------
  298. #pragma mark ===== downloadThumbnail / downloadPreview =====
  299. #pragma --------------------------------------------------------------------------------------------
  300. /*
  301. - (void)downloadThumbnailWithMetadata:(tableMetadata*)metadata withWidth:(CGFloat)width andHeight:(CGFloat)height completion:(void (^)(NSString *message, NSInteger errorCode))completion
  302. {
  303. NSString *file = [NSString stringWithFormat:@"%@/%@.ico", [CCUtility getDirectoryProviderStorageFileID:metadata.fileID], metadata.fileNameView];
  304. if ([[NSFileManager defaultManager] fileExistsAtPath:file]) {
  305. completion(nil, 0);
  306. } else {
  307. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  308. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  309. [communication setUserAgent:[CCUtility getUserAgent]];
  310. [communication getRemoteThumbnailByServer:_activeUrl ofFilePath:[CCUtility returnFileNamePathFromFileName:metadata.fileName serverUrl:metadata.serverUrl activeUrl:_activeUrl] withWidth:width andHeight:height onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSData *thumbnail, NSString *redirectedServer) {
  311. [thumbnail writeToFile:file atomically:YES];
  312. completion(nil, 0);
  313. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  314. NSString *message;
  315. NSInteger errorCode = response.statusCode;
  316. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  317. errorCode = error.code;
  318. // Error
  319. if (errorCode == 503)
  320. message = NSLocalizedString(@"_server_error_retry_", nil);
  321. else
  322. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  323. completion(message, errorCode);
  324. }];
  325. }
  326. }
  327. */
  328. - (void)downloadPreviewTrashWithAccount:(NSString *)account FileID:(NSString *)fileID fileName:(NSString *)fileName completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  329. {
  330. NSString *file = [NSString stringWithFormat:@"%@/%@.ico", [CCUtility getDirectoryProviderStorageFileID:fileID], fileName];
  331. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  332. if (tableAccount == nil) {
  333. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  334. }
  335. if ([[NSFileManager defaultManager] fileExistsAtPath:file]) {
  336. completion(account, nil, 0);
  337. } else {
  338. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  339. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  340. [communication setUserAgent:[CCUtility getUserAgent]];
  341. [communication getRemotePreviewTrashByServer:tableAccount.url ofFileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSData *preview, NSString *redirectedServer) {
  342. [preview writeToFile:file atomically:YES];
  343. completion(account, nil, 0);
  344. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  345. NSString *message;
  346. NSInteger errorCode = response.statusCode;
  347. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  348. errorCode = error.code;
  349. // Error
  350. if (errorCode == 503)
  351. message = NSLocalizedString(@"_server_error_retry_", nil);
  352. else
  353. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  354. completion(account, message, errorCode);
  355. }];
  356. }
  357. }
  358. - (void)downloadPreviewWithMetadata:(tableMetadata*)metadata withWidth:(CGFloat)width andHeight:(CGFloat)height completion:(void (^)(NSString *message, NSInteger errorCode))completion
  359. {
  360. NSString *file = [NSString stringWithFormat:@"%@/%@.ico", [CCUtility getDirectoryProviderStorageFileID:metadata.fileID], metadata.fileNameView];
  361. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", metadata.account]];
  362. if (tableAccount == nil) {
  363. completion(NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  364. }
  365. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  366. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  367. [communication setUserAgent:[CCUtility getUserAgent]];
  368. [communication getRemotePreviewByServer:tableAccount.url ofFilePath:[CCUtility returnFileNamePathFromFileName:metadata.fileName serverUrl:metadata.serverUrl activeUrl:tableAccount.url] withWidth:width andHeight:height andA:1 andMode:@"cover" onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSData *preview, NSString *redirectedServer) {
  369. [preview writeToFile:file atomically:YES];
  370. completion(nil, 0);
  371. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  372. NSString *message;
  373. NSInteger errorCode = response.statusCode;
  374. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  375. errorCode = error.code;
  376. // Error
  377. if (errorCode == 503)
  378. message = NSLocalizedString(@"_server_error_retry_", nil);
  379. else
  380. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  381. completion(message, errorCode);
  382. }];
  383. }
  384. #pragma --------------------------------------------------------------------------------------------
  385. #pragma mark ===== Read Folder =====
  386. #pragma --------------------------------------------------------------------------------------------
  387. - (void)readFolderWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl depth:(NSString *)depth completion:(void(^)(NSString *account, NSArray *metadatas, tableMetadata *metadataFolder, NSString *message, NSInteger errorCode))completion
  388. {
  389. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  390. if (tableAccount == nil) {
  391. completion(account, nil, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  392. }
  393. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  394. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  395. [communication setUserAgent:[CCUtility getUserAgent]];
  396. [communication readFolder:serverUrl depth:depth withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  397. // Check items > 0
  398. if ([items count] == 0) {
  399. #ifndef EXTENSION
  400. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  401. [appDelegate messageNotification:@"Server error" description:@"Read Folder WebDAV : [items NULL] please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:k_CCErrorInternalError];
  402. #endif
  403. completion(account, nil, nil, NSLocalizedString(@"Read Folder WebDAV : [items NULL] please fix", nil), k_CCErrorInternalError);
  404. } else {
  405. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  406. BOOL showHiddenFiles = [CCUtility getShowHiddenFiles];
  407. BOOL isFolderEncrypted = [CCUtility isFolderEncrypted:serverUrl account:account];
  408. // directory [0]
  409. OCFileDto *itemDtoFolder = [items objectAtIndex:0];
  410. //NSDate *date = [NSDate dateWithTimeIntervalSince1970:itemDtoDirectory.date];
  411. NSMutableArray *metadatas = [NSMutableArray new];
  412. tableMetadata *metadataFolder = [tableMetadata new];
  413. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  414. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:tableAccount.url];
  415. NSString *serverUrlFolder;
  416. // Metadata . (self Folder)
  417. if ([serverUrl isEqualToString:[CCUtility getHomeServerUrlActiveUrl:tableAccount.url]]) {
  418. // root folder
  419. serverUrlFolder = k_serverUrl_root;
  420. metadataFolder = [CCUtility trasformedOCFileToCCMetadata:itemDtoFolder fileName:@"." serverUrl:serverUrlFolder autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:account isFolderEncrypted:isFolderEncrypted];
  421. } else {
  422. serverUrlFolder = [CCUtility deletingLastPathComponentFromServerUrl:serverUrl];
  423. metadataFolder = [CCUtility trasformedOCFileToCCMetadata:itemDtoFolder fileName:[serverUrl lastPathComponent] serverUrl:serverUrlFolder autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:account isFolderEncrypted:isFolderEncrypted];
  424. }
  425. // Add metadata folder
  426. (void)[[NCManageDatabase sharedInstance] addDirectoryWithEncrypted:itemDtoFolder.isEncrypted favorite:itemDtoFolder.isFavorite fileID:itemDtoFolder.ocId permissions:itemDtoFolder.permissions serverUrl:serverUrl account:account];
  427. NSArray *itemsSortedArray = [items sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
  428. NSString *first = [(OCFileDto*)a fileName];
  429. NSString *second = [(OCFileDto*)b fileName];
  430. return [[first lowercaseString] compare:[second lowercaseString]];
  431. }];
  432. for (NSUInteger i=1; i < [itemsSortedArray count]; i++) {
  433. OCFileDto *itemDto = [itemsSortedArray objectAtIndex:i];
  434. NSString *fileName = [itemDto.fileName stringByReplacingOccurrencesOfString:@"/" withString:@""];
  435. // Skip hidden files
  436. if (fileName.length > 0) {
  437. if (!showHiddenFiles && [[fileName substringToIndex:1] isEqualToString:@"."])
  438. continue;
  439. } else {
  440. continue;
  441. }
  442. if (itemDto.isDirectory) {
  443. (void)[[NCManageDatabase sharedInstance] addDirectoryWithEncrypted:itemDto.isEncrypted favorite:itemDto.isFavorite fileID:itemDto.ocId permissions:itemDto.permissions serverUrl:[CCUtility stringAppendServerUrl:serverUrl addFileName:fileName] account:account];
  444. }
  445. [metadatas addObject:[CCUtility trasformedOCFileToCCMetadata:itemDto fileName:itemDto.fileName serverUrl:serverUrl autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:account isFolderEncrypted:isFolderEncrypted]];
  446. }
  447. dispatch_async(dispatch_get_main_queue(), ^{
  448. completion(account, metadatas, metadataFolder, nil, 0);
  449. });
  450. });
  451. }
  452. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  453. NSString *message;
  454. NSInteger errorCode = response.statusCode;
  455. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  456. errorCode = error.code;
  457. // Error
  458. if (errorCode == 503)
  459. message = NSLocalizedString(@"_server_error_retry_", nil);
  460. else
  461. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  462. // Request trusted certificated
  463. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  464. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  465. // Activity
  466. [[NCManageDatabase sharedInstance] addActivityClient:serverUrl fileID:@"" action:k_activityDebugActionReadFolder selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:tableAccount.url];
  467. completion(account, nil, nil, message, errorCode);
  468. }];
  469. }
  470. #pragma --------------------------------------------------------------------------------------------
  471. #pragma mark ===== ReadFile =====
  472. #pragma --------------------------------------------------------------------------------------------
  473. - (void)readFile:(NSString *)fileName serverUrl:(NSString *)serverUrl account:(NSString *)account success:(void(^)(NSString *account, tableMetadata *metadata))success failure:(void (^)(NSString *account, NSString *message, NSInteger errorCode))failure
  474. {
  475. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  476. if (tableAccount == nil) {
  477. failure(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  478. }
  479. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  480. NSString *fileNamePath;
  481. if (fileName) {
  482. fileNamePath = [NSString stringWithFormat:@"%@/%@", serverUrl, fileName];
  483. } else {
  484. fileName= @".";
  485. fileNamePath = serverUrl;
  486. }
  487. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  488. [communication setUserAgent:[CCUtility getUserAgent]];
  489. [communication readFile:fileNamePath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  490. BOOL isFolderEncrypted = [CCUtility isFolderEncrypted:serverUrl account:account];
  491. if ([items count] > 0) {
  492. tableMetadata *metadata = [tableMetadata new];
  493. OCFileDto *itemDto = [items objectAtIndex:0];
  494. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  495. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:tableAccount.url];
  496. metadata = [CCUtility trasformedOCFileToCCMetadata:itemDto fileName:fileName serverUrl:serverUrl autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:account isFolderEncrypted:isFolderEncrypted];
  497. success(account, metadata);
  498. // BUG 1038 item == 0
  499. } else {
  500. #ifndef EXTENSION
  501. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  502. [appDelegate messageNotification:@"Server error" description:@"Read File WebDAV : [items NULL] please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:k_CCErrorInternalError];
  503. #endif
  504. failure(account, NSLocalizedString(@"Read File WebDAV : [items NULL] please fix", nil), k_CCErrorInternalError);
  505. }
  506. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  507. NSString *message;
  508. NSInteger errorCode = response.statusCode;
  509. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  510. errorCode = error.code;
  511. // Error
  512. if (errorCode == 503)
  513. message = NSLocalizedString(@"_server_error_retry_", nil);
  514. else
  515. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  516. // Request trusted certificated
  517. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  518. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  519. // Activity
  520. [[NCManageDatabase sharedInstance] addActivityClient:serverUrl fileID:@"" action:k_activityDebugActionReadFolder selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:tableAccount.url];
  521. failure(account, message, errorCode);
  522. }];
  523. }
  524. #pragma --------------------------------------------------------------------------------------------
  525. #pragma mark ===== Search =====
  526. #pragma --------------------------------------------------------------------------------------------
  527. - (void)searchWithAccount:(NSString *)account fileName:(NSString *)fileName serverUrl:(NSString *)serverUrl contentType:(NSArray *)contentType date:(NSDate *)date depth:(NSString *)depth completion:(void(^)(NSString *account, NSArray *metadatas, NSString *message, NSInteger errorCode))completion
  528. {
  529. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  530. if (tableAccount == nil) {
  531. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  532. }
  533. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  534. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  535. [communication setUserAgent:[CCUtility getUserAgent]];
  536. NSString *path = [tableAccount.url stringByAppendingString:k_dav];
  537. NSString *folder = [serverUrl stringByReplacingOccurrencesOfString:[CCUtility getHomeServerUrlActiveUrl:tableAccount.url] withString:@""];
  538. NSString *dateLastModified;
  539. if (date) {
  540. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  541. NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
  542. [dateFormatter setLocale:enUSPOSIXLocale];
  543. [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
  544. dateLastModified = [dateFormatter stringFromDate:date];
  545. }
  546. [communication search:path folder:folder fileName: [NSString stringWithFormat:@"%%%@%%", fileName] depth:depth dateLastModified:dateLastModified contentType:contentType withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  547. NSMutableArray *metadatas = [NSMutableArray new];
  548. BOOL showHiddenFiles = [CCUtility getShowHiddenFiles];
  549. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  550. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:tableAccount.url];
  551. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  552. for (OCFileDto *itemDto in items) {
  553. NSString *serverUrl;
  554. BOOL isFolderEncrypted;
  555. NSString *fileName = [itemDto.fileName stringByReplacingOccurrencesOfString:@"/" withString:@""];
  556. // Skip hidden files
  557. if (fileName.length > 0) {
  558. if (!showHiddenFiles && [[fileName substringToIndex:1] isEqualToString:@"."])
  559. continue;
  560. } else
  561. continue;
  562. NSRange firstInstance = [itemDto.filePath rangeOfString:[NSString stringWithFormat:@"%@/files/%@", k_dav, tableAccount.userID]];
  563. NSString *serverPath = [itemDto.filePath substringFromIndex:firstInstance.length+firstInstance.location+1];
  564. if ([serverPath hasSuffix:@"/"]) serverPath = [serverPath substringToIndex:[serverPath length] - 1];
  565. serverUrl = [CCUtility stringAppendServerUrl:[tableAccount.url stringByAppendingString:k_webDAV] addFileName:serverPath];
  566. if (itemDto.isDirectory) {
  567. (void)[[NCManageDatabase sharedInstance] addDirectoryWithEncrypted:itemDto.isEncrypted favorite:itemDto.isFavorite fileID:itemDto.ocId permissions:itemDto.permissions serverUrl:[NSString stringWithFormat:@"%@/%@", serverUrl, fileName] account:account];
  568. }
  569. isFolderEncrypted = [CCUtility isFolderEncrypted:serverUrl account:account];
  570. [metadatas addObject:[CCUtility trasformedOCFileToCCMetadata:itemDto fileName:itemDto.fileName serverUrl:serverUrl autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:account isFolderEncrypted:isFolderEncrypted]];
  571. }
  572. dispatch_async(dispatch_get_main_queue(), ^{
  573. completion(account, metadatas, nil, 0);
  574. });
  575. });
  576. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  577. NSString *message = @"";
  578. NSInteger errorCode = response.statusCode;
  579. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  580. errorCode = error.code;
  581. // Error
  582. if (errorCode == 503) {
  583. message = NSLocalizedString(@"_server_error_retry_", nil);
  584. } else {
  585. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  586. }
  587. // Request trusted certificated
  588. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  589. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  590. completion(account, nil, message, errorCode);
  591. }];
  592. }
  593. #pragma --------------------------------------------------------------------------------------------
  594. #pragma mark ===== Setting Favorite =====
  595. #pragma --------------------------------------------------------------------------------------------
  596. - (void)settingFavorite:(NSString *)fileName account:(NSString *)account favorite:(BOOL)favorite completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  597. {
  598. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  599. if (tableAccount == nil) {
  600. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  601. }
  602. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  603. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  604. [communication setUserAgent:[CCUtility getUserAgent]];
  605. NSString *server = [tableAccount.url stringByAppendingString:k_dav];
  606. [communication settingFavoriteServer:server andFileOrFolderPath:fileName favorite:favorite withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer, NSString *token) {
  607. completion(account, nil, 0);
  608. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  609. NSString *message;
  610. NSInteger errorCode = response.statusCode;
  611. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  612. errorCode = error.code;
  613. // Error
  614. if (errorCode == 503)
  615. message = NSLocalizedString(@"_server_error_retry_", nil);
  616. else
  617. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  618. // Request trusted certificated
  619. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  620. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  621. completion(account, message, errorCode);
  622. }];
  623. }
  624. #pragma --------------------------------------------------------------------------------------------
  625. #pragma mark ===== Listing Favorites =====
  626. #pragma --------------------------------------------------------------------------------------------
  627. - (void)listingFavorites:(NSString *)serverUrl account:(NSString *)account success:(void(^)(NSString *account, NSArray *metadatas))success failure:(void (^)(NSString* account, NSString *message, NSInteger errorCode))failure
  628. {
  629. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  630. if (tableAccount == nil) {
  631. failure(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  632. }
  633. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  634. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  635. [communication setUserAgent:[CCUtility getUserAgent]];
  636. NSString *path = [tableAccount.url stringByAppendingString:k_dav];
  637. NSString *folder = [serverUrl stringByReplacingOccurrencesOfString:[CCUtility getHomeServerUrlActiveUrl:tableAccount.url] withString:@""];
  638. [communication listingFavorites:path folder:folder withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  639. NSMutableArray *metadatas = [NSMutableArray new];
  640. BOOL showHiddenFiles = [CCUtility getShowHiddenFiles];
  641. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  642. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:tableAccount.url];
  643. // Order by fileNamePath
  644. items = [items sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  645. OCFileDto *record1 = obj1, *record2 = obj2;
  646. NSString *path1 = [[record1.filePath stringByAppendingString:record1.fileName] lowercaseString];
  647. NSString *path2 = [[record2.filePath stringByAppendingString:record2.fileName] lowercaseString];
  648. return [path1 compare:path2];
  649. }];
  650. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  651. for(OCFileDto *itemDto in items) {
  652. NSString *serverUrl;
  653. BOOL isFolderEncrypted;
  654. NSString *fileName = [itemDto.fileName stringByReplacingOccurrencesOfString:@"/" withString:@""];
  655. // Skip hidden files
  656. if (fileName.length > 0) {
  657. if (!showHiddenFiles && [[fileName substringToIndex:1] isEqualToString:@"."])
  658. continue;
  659. } else
  660. continue;
  661. NSRange firstInstance = [itemDto.filePath rangeOfString:[NSString stringWithFormat:@"%@/files/%@", k_dav, tableAccount.userID]];
  662. NSString *serverPath = [itemDto.filePath substringFromIndex:firstInstance.length+firstInstance.location+1];
  663. if ([serverPath hasSuffix:@"/"])
  664. serverPath = [serverPath substringToIndex:[serverPath length] - 1];
  665. serverUrl = [CCUtility stringAppendServerUrl:[tableAccount.url stringByAppendingString:k_webDAV] addFileName:serverPath];
  666. if (itemDto.isDirectory) {
  667. (void)[[NCManageDatabase sharedInstance] addDirectoryWithEncrypted:itemDto.isEncrypted favorite:itemDto.isFavorite fileID:itemDto.ocId permissions:itemDto.permissions serverUrl:[NSString stringWithFormat:@"%@/%@", serverUrl, fileName] account:account];
  668. }
  669. isFolderEncrypted = [CCUtility isFolderEncrypted:serverUrl account:account];
  670. [metadatas addObject:[CCUtility trasformedOCFileToCCMetadata:itemDto fileName:itemDto.fileName serverUrl:serverUrl autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:account isFolderEncrypted:isFolderEncrypted]];
  671. }
  672. dispatch_async(dispatch_get_main_queue(), ^{
  673. success(account, metadatas);
  674. });
  675. });
  676. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  677. NSString *message;
  678. NSInteger errorCode = response.statusCode;
  679. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  680. errorCode = error.code;
  681. // Error
  682. if (errorCode == 503)
  683. message = NSLocalizedString(@"_server_error_retry_", nil);
  684. else
  685. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  686. // Request trusted certificated
  687. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  688. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  689. // Activity
  690. [[NCManageDatabase sharedInstance] addActivityClient:serverUrl fileID:@"" action:k_activityDebugActionListingFavorites selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:tableAccount.url];
  691. failure(account, message, errorCode);
  692. }];
  693. }
  694. #pragma --------------------------------------------------------------------------------------------
  695. #pragma mark ===== Create Folder =====
  696. #pragma --------------------------------------------------------------------------------------------
  697. - (void)createFolder:(NSString *)fileName serverUrl:(NSString *)serverUrl account:(NSString *)account success:(void(^)(NSString *account, NSString *fileID, NSDate *date))success failure:(void (^)(NSString *account, NSString *message, NSInteger errorCode))failure
  698. {
  699. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  700. if (tableAccount == nil) {
  701. failure(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  702. }
  703. NSString *path = [NSString stringWithFormat:@"%@/%@", serverUrl, fileName];
  704. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  705. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:tableAccount.url];
  706. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  707. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  708. [communication setUserAgent:[CCUtility getUserAgent]];
  709. [communication createFolder:path onCommunication:communication withForbiddenCharactersSupported:YES successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  710. NSDictionary *fields = [response allHeaderFields];
  711. NSString *fileID = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-FileId"]];
  712. NSDate *date = [CCUtility dateEnUsPosixFromCloud:[fields objectForKey:@"Date"]];
  713. success(account, fileID, date);
  714. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  715. NSString *message;
  716. if (([fileName isEqualToString:autoUploadFileName] && [serverUrl isEqualToString:autoUploadDirectory]))
  717. message = nil;
  718. else
  719. message = [CCError manageErrorOC:response.statusCode error:error];
  720. NSInteger errorCode = response.statusCode;
  721. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  722. errorCode = error.code;
  723. // Request trusted certificated
  724. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  725. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  726. // Activity
  727. [[NCManageDatabase sharedInstance] addActivityClient:path fileID:@"" action:k_activityDebugActionCreateFolder selector:@"" note:NSLocalizedString(@"_not_possible_create_folder_", nil) type:k_activityTypeFailure verbose:k_activityVerboseDefault activeUrl:tableAccount.url];
  728. failure(account, message, errorCode);
  729. } errorBeforeRequest:^(NSError *error) {
  730. NSString *message;
  731. if (([fileName isEqualToString:autoUploadFileName] && [serverUrl isEqualToString:autoUploadDirectory]))
  732. message = nil;
  733. else {
  734. if (error.code == OCErrorForbidenCharacters)
  735. message = NSLocalizedString(@"_forbidden_characters_from_server_", nil);
  736. else
  737. message = NSLocalizedString(@"_unknow_response_server_", nil);
  738. }
  739. failure(account, message, error.code);
  740. }];
  741. }
  742. #pragma --------------------------------------------------------------------------------------------
  743. #pragma mark ===== Delete =====
  744. #pragma --------------------------------------------------------------------------------------------
  745. - (void)deleteFileOrFolder:(NSString *)path account:(NSString *)account completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  746. {
  747. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  748. if (tableAccount == nil) {
  749. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  750. }
  751. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  752. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  753. [communication setUserAgent:[CCUtility getUserAgent]];
  754. [communication deleteFileOrFolder:path onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  755. completion(account, nil, 0);
  756. } failureRquest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  757. NSString *message;
  758. NSInteger errorCode = response.statusCode;
  759. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  760. errorCode = error.code;
  761. // Error
  762. if (errorCode == 503)
  763. message = NSLocalizedString(@"_server_error_retry_", nil);
  764. else
  765. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  766. // Request trusted certificated
  767. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  768. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  769. // Activity
  770. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionDeleteFileFolder selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:tableAccount.url];
  771. completion(account, message, errorCode);
  772. }];
  773. }
  774. #pragma --------------------------------------------------------------------------------------------
  775. #pragma mark ===== Move =====
  776. #pragma --------------------------------------------------------------------------------------------
  777. - (void)moveFileOrFolder:(NSString *)fileName fileNameTo:(NSString *)fileNameTo account:(NSString *)account success:(void (^)(NSString *account))success failure:(void (^)(NSString *account, NSString *message, NSInteger errorCode))failure
  778. {
  779. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  780. if (tableAccount == nil) {
  781. failure(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  782. }
  783. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  784. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  785. [communication setUserAgent:[CCUtility getUserAgent]];
  786. [communication moveFileOrFolder:fileName toDestiny:fileNameTo onCommunication:communication withForbiddenCharactersSupported:YES successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  787. success(account);
  788. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  789. NSInteger errorCode = response.statusCode;
  790. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  791. errorCode = error.code;
  792. NSString *message = [CCError manageErrorOC:response.statusCode error:error];
  793. failure(account, message, error.code);
  794. } errorBeforeRequest:^(NSError *error) {
  795. NSString *message;
  796. if (error.code == OCErrorMovingTheDestinyAndOriginAreTheSame) {
  797. message = NSLocalizedString(@"_error_folder_destiny_is_the_same_", nil);
  798. } else if (error.code == OCErrorMovingFolderInsideHimself) {
  799. message = NSLocalizedString(@"_error_folder_destiny_is_the_same_", nil);
  800. } else if (error.code == OCErrorMovingDestinyNameHaveForbiddenCharacters) {
  801. message = NSLocalizedString(@"_forbidden_characters_from_server_", nil);
  802. } else {
  803. message = NSLocalizedString(@"_unknow_response_server_", nil);
  804. }
  805. failure(account, message, error.code);
  806. }];
  807. }
  808. #pragma --------------------------------------------------------------------------------------------
  809. #pragma mark ===== Shared =====
  810. #pragma --------------------------------------------------------------------------------------------
  811. - (void)readShareServer:(NSString *)account completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  812. {
  813. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  814. if (tableAccount == nil) {
  815. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  816. }
  817. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  818. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  819. [communication setUserAgent:[CCUtility getUserAgent]];
  820. [communication readSharedByServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  821. completion(account, items, nil, 0);
  822. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  823. NSString *message = @"";
  824. NSInteger errorCode = response.statusCode;
  825. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  826. errorCode = error.code;
  827. // Error
  828. if (errorCode == 503) {
  829. message = NSLocalizedString(@"_server_error_retry_", nil);
  830. } else {
  831. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  832. }
  833. // Request trusted certificated
  834. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  835. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  836. completion(account, nil, message, errorCode);
  837. }];
  838. }
  839. /*
  840. - (void)readShareServer
  841. {
  842. #ifndef EXTENSION
  843. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  844. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  845. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  846. [communication setUserAgent:[CCUtility getUserAgent]];
  847. [communication readSharedByServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  848. // Test active account
  849. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  850. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  851. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)])
  852. [self.delegate shareFailure:_metadataNet message:NSLocalizedString(@"_error_user_not_available_", nil) errorCode:k_CCErrorUserNotAvailble];
  853. [self complete];
  854. return;
  855. }
  856. BOOL openWindow = NO;
  857. [appDelegate.sharesID removeAllObjects];
  858. if ([recordAccount.account isEqualToString:_metadataNet.account]) {
  859. for (OCSharedDto *item in items)
  860. [appDelegate.sharesID setObject:item forKey:[@(item.idRemoteShared) stringValue]];
  861. if ([_metadataNet.selector isEqual:selectorOpenWindowShare]) openWindow = YES;
  862. if ([_metadataNet.action isEqual:actionUpdateShare]) openWindow = YES;
  863. if ([_metadataNet.action isEqual:actionShare]) openWindow = YES;
  864. if ([_metadataNet.action isEqual:actionShareWith]) openWindow = YES;
  865. }
  866. if([self.delegate respondsToSelector:@selector(readSharedSuccess:items:openWindow:)])
  867. [self.delegate readSharedSuccess:_metadataNet items:appDelegate.sharesID openWindow:openWindow];
  868. [self complete];
  869. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  870. NSInteger errorCode = response.statusCode;
  871. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  872. errorCode = error.code;
  873. // Error
  874. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  875. if (errorCode == 503)
  876. [self.delegate shareFailure:_metadataNet message:NSLocalizedString(@"_server_error_retry_", nil) errorCode:errorCode];
  877. else
  878. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  879. }
  880. // Request trusted certificated
  881. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  882. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  883. [self complete];
  884. }];
  885. #endif
  886. }
  887. */
  888. - (void)share
  889. {
  890. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  891. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  892. [communication setUserAgent:[CCUtility getUserAgent]];
  893. [communication shareFileOrFolderByServer:[_activeUrl stringByAppendingString:@"/"] andFileOrFolderPath:[_metadataNet.fileName encodeString:NSUTF8StringEncoding] andPassword:[_metadataNet.password encodeString:NSUTF8StringEncoding] andPermission:_metadataNet.sharePermission andHideDownload:_metadataNet.hideDownload onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  894. //[self readShareServer];
  895. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  896. NSInteger errorCode = response.statusCode;
  897. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  898. errorCode = error.code;
  899. // Error
  900. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  901. if (errorCode == 503)
  902. [self.delegate shareFailure:_metadataNet message:NSLocalizedString(@"_server_error_retry_", nil) errorCode:errorCode];
  903. else
  904. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  905. }
  906. // Request trusted certificated
  907. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  908. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  909. [self complete];
  910. }];
  911. }
  912. // * @param shareeType -> NSInteger: to set the type of sharee (user/group/federated)
  913. - (void)shareWith
  914. {
  915. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  916. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  917. [communication setUserAgent:[CCUtility getUserAgent]];
  918. [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) {
  919. //[self readShareServer];
  920. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  921. NSInteger errorCode = response.statusCode;
  922. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  923. errorCode = error.code;
  924. // Error
  925. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  926. if (errorCode == 503)
  927. [self.delegate shareFailure:_metadataNet message:NSLocalizedString(@"_server_error_retry_", nil) errorCode:errorCode];
  928. else
  929. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  930. }
  931. // Request trusted certificated
  932. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  933. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  934. [self complete];
  935. }];
  936. }
  937. - (void)updateShare
  938. {
  939. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  940. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  941. [communication setUserAgent:[CCUtility getUserAgent]];
  942. [communication updateShare:[_metadataNet.share intValue] ofServerPath:[_activeUrl stringByAppendingString:@"/"] withPasswordProtect:[_metadataNet.password encodeString:NSUTF8StringEncoding] andExpirationTime:_metadataNet.expirationTime andPermissions:_metadataNet.sharePermission andHideDownload:_metadataNet.hideDownload onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  943. //[self readShareServer];
  944. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  945. #ifndef EXTENSION
  946. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  947. [appDelegate messageNotification:@"_error_" description:[CCError manageErrorOC:response.statusCode error:error] visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:error.code];
  948. #endif
  949. NSInteger errorCode = response.statusCode;
  950. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  951. errorCode = error.code;
  952. // Error
  953. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  954. if (errorCode == 503)
  955. [self.delegate shareFailure:_metadataNet message:NSLocalizedString(@"_server_error_retry_", nil) errorCode:errorCode];
  956. else
  957. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  958. }
  959. // Request trusted certificated
  960. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  961. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  962. [self complete];
  963. }];
  964. }
  965. - (void)unShare
  966. {
  967. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  968. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  969. [communication setUserAgent:[CCUtility getUserAgent]];
  970. [communication unShareFileOrFolderByServer:[_activeUrl stringByAppendingString:@"/"] andIdRemoteShared:[_metadataNet.share intValue] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  971. if([self.delegate respondsToSelector:@selector(unShareSuccess:)])
  972. [self.delegate unShareSuccess:_metadataNet];
  973. [self complete];
  974. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  975. #ifndef EXTENSION
  976. [(AppDelegate *)[[UIApplication sharedApplication] delegate] messageNotification:@"_error_" description:[CCError manageErrorOC:response.statusCode error:error] visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:error.code];
  977. #endif
  978. NSInteger errorCode = response.statusCode;
  979. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  980. errorCode = error.code;
  981. // Error
  982. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  983. if (errorCode == 503)
  984. [self.delegate shareFailure:_metadataNet message:NSLocalizedString(@"_server_error_retry_", nil) errorCode:errorCode];
  985. else
  986. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  987. }
  988. // Request trusted certificated
  989. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  990. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  991. [self complete];
  992. }];
  993. }
  994. - (void)getUserAndGroup
  995. {
  996. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  997. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  998. [communication setUserAgent:[CCUtility getUserAgent]];
  999. [communication searchUsersAndGroupsWith:_metadataNet.optionAny forPage:1 with:50 ofServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *itemList, NSString *redirectedServer) {
  1000. if([self.delegate respondsToSelector:@selector(getUserAndGroupSuccess:items:)])
  1001. [self.delegate getUserAndGroupSuccess:_metadataNet items:itemList];
  1002. [self complete];
  1003. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1004. NSInteger errorCode = response.statusCode;
  1005. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1006. errorCode = error.code;
  1007. // Error
  1008. if ([self.delegate respondsToSelector:@selector(getUserAndGroupFailure:message:errorCode:)]) {
  1009. if (errorCode == 503)
  1010. [self.delegate getUserAndGroupFailure:_metadataNet message:NSLocalizedString(@"_server_error_retry_", nil) errorCode:errorCode];
  1011. else
  1012. [self.delegate getUserAndGroupFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1013. }
  1014. // Request trusted certificated
  1015. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1016. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1017. [self complete];
  1018. }];
  1019. }
  1020. - (void)getSharePermissionsFile
  1021. {
  1022. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1023. NSString *fileName = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  1024. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1025. [communication setUserAgent:[CCUtility getUserAgent]];
  1026. [communication getSharePermissionsFile:fileName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *permissions, NSString *redirectedServer) {
  1027. // Test active account
  1028. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  1029. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  1030. if ([self.delegate respondsToSelector:@selector(getSharePermissionsFileFailure:message:errorCode:)])
  1031. [self.delegate getSharePermissionsFileFailure:_metadataNet message:NSLocalizedString(@"_error_user_not_available_", nil) errorCode:k_CCErrorUserNotAvailble];
  1032. [self complete];
  1033. return;
  1034. }
  1035. if([self.delegate respondsToSelector:@selector(getSharePermissionsFileSuccess:permissions:)])
  1036. [self.delegate getSharePermissionsFileSuccess:_metadataNet permissions:permissions];
  1037. [self complete];
  1038. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1039. NSInteger errorCode = response.statusCode;
  1040. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1041. errorCode = error.code;
  1042. // Error
  1043. if ([self.delegate respondsToSelector:@selector(getSharePermissionsFileFailure:message:errorCode:)]) {
  1044. if (errorCode == 503)
  1045. [self.delegate getSharePermissionsFileFailure:_metadataNet message:NSLocalizedString(@"_server_error_retry_", nil) errorCode:errorCode];
  1046. else
  1047. [self.delegate getSharePermissionsFileFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1048. }
  1049. // Request trusted certificated
  1050. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1051. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1052. [self complete];
  1053. }];
  1054. }
  1055. #pragma --------------------------------------------------------------------------------------------
  1056. #pragma mark ===== Activity =====
  1057. #pragma --------------------------------------------------------------------------------------------
  1058. - (void)getActivityServer:(NSString *)account success:(void(^)(NSString *account, NSArray *listOfActivity))success failure:(void (^)(NSString *account, NSString *message, NSInteger errorCode))failure
  1059. {
  1060. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1061. if (tableAccount == nil) {
  1062. failure(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1063. }
  1064. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1065. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  1066. [communication setUserAgent:[CCUtility getUserAgent]];
  1067. [communication getActivityServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfActivity, NSString *redirectedServer) {
  1068. success(account, listOfActivity);
  1069. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1070. NSString *message;
  1071. NSInteger errorCode = response.statusCode;
  1072. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1073. errorCode = error.code;
  1074. // Error
  1075. if (errorCode == 503)
  1076. message = NSLocalizedString(@"_server_error_retry_", nil);
  1077. else
  1078. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1079. failure(account, message, errorCode);
  1080. }];
  1081. }
  1082. #pragma --------------------------------------------------------------------------------------------
  1083. #pragma mark ===== External Sites =====
  1084. #pragma --------------------------------------------------------------------------------------------
  1085. - (void)getExternalSitesServer:(NSString *)account completion:(void (^)(NSString *account, NSArray *listOfExternalSites, NSString *message, NSInteger errorCode))completion
  1086. {
  1087. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1088. if (tableAccount == nil) {
  1089. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1090. }
  1091. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1092. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  1093. [communication setUserAgent:[CCUtility getUserAgent]];
  1094. [communication getExternalSitesServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfExternalSites, NSString *redirectedServer) {
  1095. completion(account, listOfExternalSites, nil, 0);
  1096. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1097. NSString *message = @"";
  1098. NSInteger errorCode = response.statusCode;
  1099. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1100. errorCode = error.code;
  1101. // Error
  1102. if (errorCode == 503) {
  1103. message = NSLocalizedString(@"_server_error_retry_", nil);
  1104. } else {
  1105. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1106. }
  1107. // Request trusted certificated
  1108. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1109. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1110. completion(account, nil, message, errorCode);
  1111. }];
  1112. }
  1113. #pragma --------------------------------------------------------------------------------------------
  1114. #pragma mark ===== Middleware Ping =====
  1115. #pragma --------------------------------------------------------------------------------------------
  1116. /*
  1117. - (void)middlewarePing
  1118. {
  1119. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1120. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  1121. [communication setUserAgent:[CCUtility getUserAgent]];
  1122. [communication getMiddlewarePing:_metadataNet.serverUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfExternalSites, NSString *redirectedServer) {
  1123. [self complete];
  1124. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1125. NSInteger errorCode = response.statusCode;
  1126. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1127. errorCode = error.code;
  1128. // Error
  1129. if ([self.delegate respondsToSelector:@selector(getExternalSitesServerFailure:message:errorCode:)]) {
  1130. if (errorCode == 503)
  1131. [self.delegate getExternalSitesServerFailure:_metadataNet message:NSLocalizedString(@"_server_error_retry_", nil) errorCode:errorCode];
  1132. else
  1133. [self.delegate getExternalSitesServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1134. }
  1135. // Request trusted certificated
  1136. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1137. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1138. [self complete];
  1139. }];
  1140. }
  1141. */
  1142. #pragma --------------------------------------------------------------------------------------------
  1143. #pragma mark ===== Notification =====
  1144. #pragma --------------------------------------------------------------------------------------------
  1145. - (void)getNotificationServer:(NSString *)account completion:(void (^)(NSString *account, NSArray *listOfNotifications, NSString *message, NSInteger errorCode))completion
  1146. {
  1147. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1148. if (tableAccount == nil) {
  1149. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1150. }
  1151. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1152. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  1153. [communication setUserAgent:[CCUtility getUserAgent]];
  1154. [communication getNotificationServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfNotifications, NSString *redirectedServer) {
  1155. completion(account, listOfNotifications, nil, 0);
  1156. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1157. NSString *message = @"";
  1158. NSInteger errorCode = response.statusCode;
  1159. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1160. errorCode = error.code;
  1161. // Error
  1162. if (errorCode == 503) {
  1163. message = NSLocalizedString(@"_server_error_retry_", nil);
  1164. } else {
  1165. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1166. }
  1167. // Request trusted certificated
  1168. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1169. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1170. // Activity
  1171. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionGetNotification selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:@""];
  1172. completion(account, nil, message, errorCode);
  1173. }];
  1174. }
  1175. - (void)setNotificationServer:(NSString *)account serverUrl:(NSString *)serverUrl type:(NSString *)type completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  1176. {
  1177. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1178. if (tableAccount == nil) {
  1179. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1180. }
  1181. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1182. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  1183. [communication setUserAgent:[CCUtility getUserAgent]];
  1184. [communication setNotificationServer:serverUrl type:type onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1185. completion(account, nil, 0);
  1186. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1187. NSString *message = @"";
  1188. NSInteger errorCode = response.statusCode;
  1189. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1190. errorCode = error.code;
  1191. // Error
  1192. if (errorCode == 503) {
  1193. message = NSLocalizedString(@"_server_error_retry_", nil);
  1194. } else {
  1195. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1196. }
  1197. // Request trusted certificated
  1198. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1199. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1200. completion(account, message, errorCode);
  1201. }];
  1202. }
  1203. #pragma --------------------------------------------------------------------------------------------
  1204. #pragma mark ===== Push Notification =====
  1205. #pragma --------------------------------------------------------------------------------------------
  1206. - (void)subscribingPushNotificationServer:(NSString *)url pushToken:(NSString *)pushToken Hash:(NSString *)pushTokenHash devicePublicKey:(NSString *)devicePublicKey success:(void(^)(NSString *deviceIdentifier, NSString *deviceIdentifierSignature, NSString *publicKey))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  1207. {
  1208. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1209. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1210. [communication setUserAgent:[CCUtility getUserAgent]];
  1211. devicePublicKey = [CCUtility URLEncodeStringFromString:devicePublicKey];
  1212. [communication subscribingNextcloudServerPush:url pushTokenHash:pushTokenHash devicePublicKey:devicePublicKey proxyServerPath: [NCBrandOptions sharedInstance].pushNotificationServerProxy onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *deviceIdentifier, NSString *signature, NSString *redirectedServer) {
  1213. deviceIdentifier = [CCUtility URLEncodeStringFromString:deviceIdentifier];
  1214. signature = [CCUtility URLEncodeStringFromString:signature];
  1215. publicKey = [CCUtility URLEncodeStringFromString:publicKey];
  1216. [communication subscribingPushProxy:[NCBrandOptions sharedInstance].pushNotificationServerProxy pushToken:pushToken deviceIdentifier:deviceIdentifier deviceIdentifierSignature:signature publicKey:publicKey onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1217. // Activity
  1218. [[NCManageDatabase sharedInstance] addActivityClient:[NCBrandOptions sharedInstance].pushNotificationServerProxy fileID:@"" action:k_activityDebugActionSubscribingPushProxy selector:@"" note:@"Service registered." type:k_activityTypeSuccess verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1219. success(deviceIdentifier, signature, publicKey);
  1220. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1221. NSString *message;
  1222. NSInteger errorCode = response.statusCode;
  1223. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1224. errorCode = error.code;
  1225. // Error
  1226. if (errorCode == 503)
  1227. message = NSLocalizedString(@"_server_error_retry_", nil);
  1228. else
  1229. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1230. // Activity
  1231. [[NCManageDatabase sharedInstance] addActivityClient:[NCBrandOptions sharedInstance].pushNotificationServerProxy fileID:@"" action:k_activityDebugActionSubscribingPushProxy selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1232. failure(message, errorCode);
  1233. }];
  1234. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1235. NSString *message;
  1236. NSInteger errorCode = response.statusCode;
  1237. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1238. errorCode = error.code;
  1239. // Error
  1240. if (errorCode == 503)
  1241. message = NSLocalizedString(@"_server_error_retry_", nil);
  1242. else
  1243. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1244. // Activity
  1245. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionSubscribingServerPush selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1246. failure(message, errorCode);
  1247. }];
  1248. }
  1249. - (void)unsubscribingPushNotificationServer:(NSString *)url deviceIdentifier:(NSString *)deviceIdentifier deviceIdentifierSignature:(NSString *)deviceIdentifierSignature publicKey:(NSString *)publicKey success:(void (^)(void))success failure:(void (^)(NSString *message, NSInteger errorCode))failure {
  1250. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1251. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1252. [communication setUserAgent:[CCUtility getUserAgent]];
  1253. [communication unsubscribingNextcloudServerPush:url onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1254. [communication unsubscribingPushProxy:[NCBrandOptions sharedInstance].pushNotificationServerProxy deviceIdentifier:deviceIdentifier deviceIdentifierSignature:deviceIdentifierSignature publicKey:publicKey onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1255. success();
  1256. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1257. NSString *message;
  1258. NSInteger errorCode = response.statusCode;
  1259. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1260. errorCode = error.code;
  1261. // Error
  1262. if (errorCode == 503)
  1263. message = NSLocalizedString(@"_server_error_retry_", nil);
  1264. else
  1265. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1266. // Activity
  1267. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionUnsubscribingPushProxy selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1268. failure(message, errorCode);
  1269. }];
  1270. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1271. NSString *message;
  1272. NSInteger errorCode = response.statusCode;
  1273. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1274. errorCode = error.code;
  1275. // Error
  1276. if (errorCode == 503)
  1277. message = NSLocalizedString(@"_server_error_retry_", nil);
  1278. else
  1279. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1280. // Activity
  1281. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionUnsubscribingServerPush selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1282. failure(message, errorCode);
  1283. }];
  1284. }
  1285. #pragma --------------------------------------------------------------------------------------------
  1286. #pragma mark ===== User Profile =====
  1287. #pragma --------------------------------------------------------------------------------------------
  1288. - (void)getUserProfile:(NSString *)account completion:(void (^)(NSString *account, OCUserProfile *userProfile, NSString *message, NSInteger errorCode))completion
  1289. {
  1290. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1291. if (tableAccount == nil) {
  1292. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1293. }
  1294. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1295. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  1296. [communication setUserAgent:[CCUtility getUserAgent]];
  1297. [communication getUserProfileServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCUserProfile *userProfile, NSString *redirectedServer) {
  1298. completion(account, userProfile, nil, 0);
  1299. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1300. NSString *message = @"";
  1301. NSInteger errorCode = response.statusCode;
  1302. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1303. errorCode = error.code;
  1304. // Error
  1305. if (errorCode == 503) {
  1306. message = NSLocalizedString(@"_server_error_retry_", nil);
  1307. } else {
  1308. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1309. }
  1310. // Request trusted certificated
  1311. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1312. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1313. completion(account, nil, message, errorCode);
  1314. }];
  1315. }
  1316. #pragma --------------------------------------------------------------------------------------------
  1317. #pragma mark ===== Capabilities =====
  1318. #pragma --------------------------------------------------------------------------------------------
  1319. - (void)getCapabilitiesOfServer:(NSString *)account completion:(void (^)(NSString *account, OCCapabilities *capabilities, NSString *message, NSInteger errorCode))completion
  1320. {
  1321. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1322. if (tableAccount == nil) {
  1323. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1324. }
  1325. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1326. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  1327. [communication setUserAgent:[CCUtility getUserAgent]];
  1328. [communication getCapabilitiesOfServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCCapabilities *capabilities, NSString *redirectedServer) {
  1329. completion(account, capabilities, nil, 0);
  1330. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1331. NSString *message = @"";
  1332. NSInteger errorCode = response.statusCode;
  1333. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1334. errorCode = error.code;
  1335. // Error
  1336. if (errorCode == 503) {
  1337. message = NSLocalizedString(@"_server_error_retry_", nil);
  1338. } else {
  1339. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1340. }
  1341. // Request trusted certificated
  1342. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1343. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1344. // Activity
  1345. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionCapabilities selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1346. completion(account, nil, message, errorCode);
  1347. }];
  1348. }
  1349. #pragma --------------------------------------------------------------------------------------------
  1350. #pragma mark ===== End-to-End Encryption =====
  1351. #pragma --------------------------------------------------------------------------------------------
  1352. - (void)getEndToEndPublicKeys
  1353. {
  1354. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1355. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1356. [communication setUserAgent:[CCUtility getUserAgent]];
  1357. [communication getEndToEndPublicKeys:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *redirectedServer) {
  1358. _metadataNet.key = publicKey;
  1359. if ([self.delegate respondsToSelector:@selector(getEndToEndPublicKeysSuccess:)])
  1360. [self.delegate getEndToEndPublicKeysSuccess:_metadataNet];
  1361. [self complete];
  1362. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1363. NSInteger errorCode = response.statusCode;
  1364. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1365. errorCode = error.code;
  1366. // Error
  1367. if ([self.delegate respondsToSelector:@selector(getEndToEndPublicKeysFailure:message:errorCode:)])
  1368. [self.delegate getEndToEndPublicKeysFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1369. // Request trusted certificated
  1370. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1371. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1372. [self complete];
  1373. }];
  1374. }
  1375. - (void)getEndToEndPrivateKeyCipher
  1376. {
  1377. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1378. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1379. [communication setUserAgent:[CCUtility getUserAgent]];
  1380. [communication getEndToEndPrivateKeyCipher:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *privateKeyChiper, NSString *redirectedServer) {
  1381. _metadataNet.key = privateKeyChiper;
  1382. if ([self.delegate respondsToSelector:@selector(getEndToEndPrivateKeyCipherSuccess:)])
  1383. [self.delegate getEndToEndPrivateKeyCipherSuccess:_metadataNet];
  1384. [self complete];
  1385. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1386. NSInteger errorCode = response.statusCode;
  1387. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1388. errorCode = error.code;
  1389. // Error
  1390. if ([self.delegate respondsToSelector:@selector(getEndToEndPrivateKeyCipherFailure:message:errorCode:)])
  1391. [self.delegate getEndToEndPrivateKeyCipherFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1392. // Request trusted certificated
  1393. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1394. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1395. [self complete];
  1396. }];
  1397. }
  1398. - (void)signEndToEndPublicKey
  1399. {
  1400. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1401. // URL Encode
  1402. NSString *publicKey = [CCUtility URLEncodeStringFromString:_metadataNet.key];
  1403. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1404. [communication setUserAgent:[CCUtility getUserAgent]];
  1405. [communication signEndToEndPublicKey:[_activeUrl stringByAppendingString:@"/"] publicKey:publicKey onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *redirectedServer) {
  1406. _metadataNet.key = publicKey;
  1407. if ([self.delegate respondsToSelector:@selector(signEndToEndPublicKeySuccess:)])
  1408. [self.delegate signEndToEndPublicKeySuccess:_metadataNet];
  1409. [self complete];
  1410. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1411. NSInteger errorCode = response.statusCode;
  1412. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1413. errorCode = error.code;
  1414. // Error
  1415. if ([self.delegate respondsToSelector:@selector(signEndToEndPublicKeyFailure:message:errorCode:)])
  1416. [self.delegate signEndToEndPublicKeyFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1417. // Request trusted certificated
  1418. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1419. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1420. [self complete];
  1421. }];
  1422. }
  1423. - (void)storeEndToEndPrivateKeyCipher
  1424. {
  1425. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1426. // URL Encode
  1427. NSString *privateKeyChiper = [CCUtility URLEncodeStringFromString:_metadataNet.keyCipher];
  1428. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1429. [communication setUserAgent:[CCUtility getUserAgent]];
  1430. [communication storeEndToEndPrivateKeyCipher:[_activeUrl stringByAppendingString:@"/"] privateKeyChiper:privateKeyChiper onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *privateKey, NSString *redirectedServer) {
  1431. if ([self.delegate respondsToSelector:@selector(storeEndToEndPrivateKeyCipherSuccess:)])
  1432. [self.delegate storeEndToEndPrivateKeyCipherSuccess:_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(storeEndToEndPrivateKeyCipherFailure:message:errorCode:)])
  1440. [self.delegate storeEndToEndPrivateKeyCipherFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1441. // Request trusted certificated
  1442. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1443. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1444. [self complete];
  1445. }];
  1446. }
  1447. - (void)deleteEndToEndPublicKey
  1448. {
  1449. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1450. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1451. [communication setUserAgent:[CCUtility getUserAgent]];
  1452. [communication deleteEndToEndPublicKey:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1453. if ([self.delegate respondsToSelector:@selector(deleteEndToEndPublicKeySuccess:)])
  1454. [self.delegate deleteEndToEndPublicKeySuccess:_metadataNet];
  1455. [self complete];
  1456. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1457. NSInteger errorCode = response.statusCode;
  1458. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1459. errorCode = error.code;
  1460. // Error
  1461. if ([self.delegate respondsToSelector:@selector(deleteEndToEndPublicKeyFailure:message:errorCode:)])
  1462. [self.delegate deleteEndToEndPublicKeyFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1463. // Request trusted certificated
  1464. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1465. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1466. [self complete];
  1467. }];
  1468. }
  1469. - (void)deleteEndToEndPrivateKey
  1470. {
  1471. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1472. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1473. [communication setUserAgent:[CCUtility getUserAgent]];
  1474. [communication deleteEndToEndPrivateKey:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1475. if ([self.delegate respondsToSelector:@selector(deleteEndToEndPrivateKeySuccess:)])
  1476. [self.delegate deleteEndToEndPrivateKeySuccess:_metadataNet];
  1477. [self complete];
  1478. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1479. NSInteger errorCode = response.statusCode;
  1480. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1481. errorCode = error.code;
  1482. // Error
  1483. if ([self.delegate respondsToSelector:@selector(deleteEndToEndPrivateKeyFailure:message:errorCode:)])
  1484. [self.delegate deleteEndToEndPrivateKeyFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1485. // Request trusted certificated
  1486. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1487. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1488. [self complete];
  1489. }];
  1490. }
  1491. - (void)getEndToEndServerPublicKey
  1492. {
  1493. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1494. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1495. [communication setUserAgent:[CCUtility getUserAgent]];
  1496. [communication getEndToEndServerPublicKey:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *redirectedServer) {
  1497. _metadataNet.key = publicKey;
  1498. if ([self.delegate respondsToSelector:@selector(getEndToEndServerPublicKeySuccess:)])
  1499. [self.delegate getEndToEndServerPublicKeySuccess:_metadataNet];
  1500. [self complete];
  1501. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1502. NSInteger errorCode = response.statusCode;
  1503. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1504. errorCode = error.code;
  1505. // Error
  1506. if ([self.delegate respondsToSelector:@selector(getEndToEndServerPublicKeyFailure:message:errorCode:)])
  1507. [self.delegate getEndToEndServerPublicKeyFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1508. // Request trusted certificated
  1509. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1510. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1511. [self complete];
  1512. }];
  1513. }
  1514. #pragma --------------------------------------------------------------------------------------------
  1515. #pragma mark ===== Manage Mobile Editor OCS API =====
  1516. #pragma --------------------------------------------------------------------------------------------
  1517. - (void)createLinkRichdocumentsWithFileID:(NSString *)fileID success:(void(^)(NSString *link))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  1518. {
  1519. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1520. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1521. [communication setUserAgent:[CCUtility getUserAgent]];
  1522. NSString *fileIDServer = [[NCUtility sharedInstance] convertFileIDClientToFileIDServer:fileID];
  1523. [communication createLinkRichdocuments:[_activeUrl stringByAppendingString:@"/"] fileID:fileIDServer onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *link, NSString *redirectedServer) {
  1524. success(link);
  1525. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1526. NSString *message;
  1527. NSInteger errorCode = response.statusCode;
  1528. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1529. errorCode = error.code;
  1530. // Error
  1531. if (errorCode == 503)
  1532. message = NSLocalizedString(@"_server_error_retry_", nil);
  1533. else
  1534. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1535. // Activity
  1536. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionUnsubscribingServerPush selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1537. failure(message, errorCode);
  1538. }];
  1539. }
  1540. - (void)geTemplatesRichdocumentsWithTypeTemplate:(NSString *)typeTemplate success:(void(^)(NSArray *listOfTemplate))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  1541. {
  1542. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1543. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1544. [communication setUserAgent:[CCUtility getUserAgent]];
  1545. [communication geTemplatesRichdocuments:[_activeUrl stringByAppendingString:@"/"] typeTemplate:typeTemplate onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfTemplate, NSString *redirectedServer) {
  1546. success(listOfTemplate);
  1547. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1548. NSString *message;
  1549. NSInteger errorCode = response.statusCode;
  1550. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1551. errorCode = error.code;
  1552. // Error
  1553. if (errorCode == 503)
  1554. message = NSLocalizedString(@"_server_error_retry_", nil);
  1555. else
  1556. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1557. // Activity
  1558. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionUnsubscribingServerPush selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1559. failure(message, errorCode);
  1560. }];
  1561. }
  1562. - (void)createNewRichdocumentsWithFileName:(NSString *)fileName serverUrl:(NSString *)serverUrl templateID:(NSString *)templateID success:(void(^)(NSString *url))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  1563. {
  1564. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1565. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1566. [communication setUserAgent:[CCUtility getUserAgent]];
  1567. [communication createNewRichdocuments:[_activeUrl stringByAppendingString:@"/"] path:fileName templateID:templateID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *url, NSString *redirectedServer) {
  1568. success(url);
  1569. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1570. NSString *message;
  1571. NSInteger errorCode = response.statusCode;
  1572. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1573. errorCode = error.code;
  1574. // Error
  1575. if (errorCode == 503)
  1576. message = NSLocalizedString(@"_server_error_retry_", nil);
  1577. else
  1578. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1579. // Activity
  1580. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionUnsubscribingServerPush selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1581. failure(message, errorCode);
  1582. }];
  1583. }
  1584. - (void)createAssetRichdocumentsWithFileName:(NSString *)fileName serverUrl:(NSString *)serverUrl success:(void(^)(NSString *link))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  1585. {
  1586. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1587. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1588. [communication setUserAgent:[CCUtility getUserAgent]];
  1589. NSString *fileNamePath = [CCUtility returnFileNamePathFromFileName:fileName serverUrl:serverUrl activeUrl:_activeUrl];
  1590. [communication createAssetRichdocuments:[_activeUrl stringByAppendingString:@"/"] path:fileNamePath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *url, NSString *redirectedServer) {
  1591. success(url);
  1592. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1593. NSString *message;
  1594. NSInteger errorCode = response.statusCode;
  1595. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1596. errorCode = error.code;
  1597. // Error
  1598. if (errorCode == 503)
  1599. message = NSLocalizedString(@"_server_error_retry_", nil);
  1600. else
  1601. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1602. // Activity
  1603. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionUnsubscribingServerPush selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1604. failure(message, errorCode);
  1605. }];
  1606. }
  1607. #pragma --------------------------------------------------------------------------------------------
  1608. #pragma mark ===== Trash =====
  1609. #pragma --------------------------------------------------------------------------------------------
  1610. - (void)listingTrash:(NSString *)serverUrl path:(NSString *)path account:(NSString *)account success:(void(^)(NSArray *items))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  1611. {
  1612. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1613. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1614. [communication setUserAgent:[CCUtility getUserAgent]];
  1615. [communication listingTrash:[serverUrl stringByAppendingString:path] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  1616. // Test active account
  1617. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  1618. if (![recordAccount.account isEqualToString:account]) {
  1619. failure(NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1620. } else {
  1621. // Check items > 0
  1622. if ([items count] == 0) {
  1623. #ifndef EXTENSION
  1624. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  1625. [appDelegate messageNotification:@"Server error" description:@"Read Folder WebDAV : [items NULL] please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:k_CCErrorInternalError];
  1626. #endif
  1627. failure(NSLocalizedString(@"Read Folder WebDAV : [items NULL] please fix", nil), k_CCErrorInternalError);
  1628. } else {
  1629. NSMutableArray *listTrash = [NSMutableArray new];
  1630. //OCFileDto *itemDtoFolder = [items objectAtIndex:0];
  1631. if ([items count] > 1) {
  1632. for (NSUInteger i=1; i < [items count]; i++) {
  1633. OCFileDto *itemDto = [items objectAtIndex:i];
  1634. tableTrash *trash = [tableTrash new];
  1635. trash.account = account;
  1636. trash.date = [NSDate dateWithTimeIntervalSince1970:itemDto.date];
  1637. trash.directory = itemDto.isDirectory;
  1638. trash.fileID = itemDto.ocId;
  1639. trash.fileName = itemDto.fileName;
  1640. trash.filePath = itemDto.filePath;
  1641. trash.size = itemDto.size;
  1642. trash.trashbinFileName = itemDto.trashbinFileName;
  1643. trash.trashbinOriginalLocation = itemDto.trashbinOriginalLocation;
  1644. trash.trashbinDeletionTime = [NSDate dateWithTimeIntervalSince1970:itemDto.trashbinDeletionTime];
  1645. [CCUtility insertTypeFileIconName:trash.trashbinFileName metadata:(tableMetadata *)trash];
  1646. [listTrash addObject:trash];
  1647. }
  1648. }
  1649. success(listTrash);
  1650. }
  1651. }
  1652. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1653. NSString *message;
  1654. NSInteger errorCode = response.statusCode;
  1655. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1656. errorCode = error.code;
  1657. // Error
  1658. if (errorCode == 503)
  1659. message = NSLocalizedString(@"_server_error_retry_", nil);
  1660. else
  1661. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1662. // Activity
  1663. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionUnsubscribingServerPush selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1664. failure(message, errorCode);
  1665. }];
  1666. }
  1667. - (void)emptyTrash:(void (^)(NSString *message, NSInteger errorCode))completion
  1668. {
  1669. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1670. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1671. [communication setUserAgent:[CCUtility getUserAgent]];
  1672. NSString *path = [NSString stringWithFormat:@"%@%@/trashbin/%@/trash", _activeUrl, k_dav, _activeUserID];
  1673. [communication emptyTrash:path onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1674. completion(nil, 0);
  1675. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1676. NSString *message;
  1677. NSInteger errorCode = response.statusCode;
  1678. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1679. errorCode = error.code;
  1680. // Error
  1681. if (errorCode == 503)
  1682. message = NSLocalizedString(@"_server_error_retry_", nil);
  1683. else
  1684. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1685. // Activity
  1686. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionUnsubscribingServerPush selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1687. completion(message, errorCode);
  1688. }];
  1689. }
  1690. @end
  1691. #pragma --------------------------------------------------------------------------------------------
  1692. #pragma mark ===== OCURLSessionManager =====
  1693. #pragma --------------------------------------------------------------------------------------------
  1694. @implementation OCURLSessionManager
  1695. - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
  1696. {
  1697. // The pinnning check
  1698. if ([[CCCertificate sharedManager] checkTrustedChallenge:challenge]) {
  1699. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  1700. } else {
  1701. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  1702. }
  1703. }
  1704. @end