OCNetworking.m 118 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396
  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)readFileWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl fileName:(NSString *)fileName completion:(void(^)(NSString *account, tableMetadata *metadata, NSString *message, NSInteger errorCode))completion
  474. {
  475. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  476. if (tableAccount == nil) {
  477. completion(account, nil, 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. completion(account, metadata, nil, 0);
  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. completion(account, nil, 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. completion(account, nil, 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)settingFavoriteWithAccount:(NSString *)account fileName:(NSString *)fileName 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)listingFavoritesWithAccount:(NSString *)account completion:(void(^)(NSString *account, NSArray *metadatas, NSString *message, NSInteger errorCode))completion
  628. {
  629. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  630. if (tableAccount == nil) {
  631. completion(account, nil, 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. [communication listingFavorites:path folder:@"" withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  638. NSMutableArray *metadatas = [NSMutableArray new];
  639. BOOL showHiddenFiles = [CCUtility getShowHiddenFiles];
  640. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  641. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:tableAccount.url];
  642. // Order by fileNamePath
  643. items = [items sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  644. OCFileDto *record1 = obj1, *record2 = obj2;
  645. NSString *path1 = [[record1.filePath stringByAppendingString:record1.fileName] lowercaseString];
  646. NSString *path2 = [[record2.filePath stringByAppendingString:record2.fileName] lowercaseString];
  647. return [path1 compare:path2];
  648. }];
  649. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  650. for(OCFileDto *itemDto in items) {
  651. NSString *serverUrl;
  652. BOOL isFolderEncrypted;
  653. NSString *fileName = [itemDto.fileName stringByReplacingOccurrencesOfString:@"/" withString:@""];
  654. // Skip hidden files
  655. if (fileName.length > 0) {
  656. if (!showHiddenFiles && [[fileName substringToIndex:1] isEqualToString:@"."])
  657. continue;
  658. } else
  659. continue;
  660. NSRange firstInstance = [itemDto.filePath rangeOfString:[NSString stringWithFormat:@"%@/files/%@", k_dav, tableAccount.userID]];
  661. NSString *serverPath = [itemDto.filePath substringFromIndex:firstInstance.length+firstInstance.location+1];
  662. if ([serverPath hasSuffix:@"/"])
  663. serverPath = [serverPath substringToIndex:[serverPath length] - 1];
  664. serverUrl = [CCUtility stringAppendServerUrl:[tableAccount.url stringByAppendingString:k_webDAV] addFileName:serverPath];
  665. if (itemDto.isDirectory) {
  666. (void)[[NCManageDatabase sharedInstance] addDirectoryWithEncrypted:itemDto.isEncrypted favorite:itemDto.isFavorite fileID:itemDto.ocId permissions:itemDto.permissions serverUrl:[NSString stringWithFormat:@"%@/%@", serverUrl, fileName] account:account];
  667. }
  668. isFolderEncrypted = [CCUtility isFolderEncrypted:serverUrl account:account];
  669. [metadatas addObject:[CCUtility trasformedOCFileToCCMetadata:itemDto fileName:itemDto.fileName serverUrl:serverUrl autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:account isFolderEncrypted:isFolderEncrypted]];
  670. }
  671. dispatch_async(dispatch_get_main_queue(), ^{
  672. completion(account, metadatas, nil, 0);
  673. });
  674. });
  675. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  676. NSString *message;
  677. NSInteger errorCode = response.statusCode;
  678. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  679. errorCode = error.code;
  680. // Error
  681. if (errorCode == 503)
  682. message = NSLocalizedString(@"_server_error_retry_", nil);
  683. else
  684. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  685. // Request trusted certificated
  686. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  687. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  688. completion(account, nil, message, errorCode);
  689. }];
  690. }
  691. #pragma --------------------------------------------------------------------------------------------
  692. #pragma mark ===== Create Folder =====
  693. #pragma --------------------------------------------------------------------------------------------
  694. - (void)createFolderWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl fileName:(NSString *)fileName completion:(void(^)(NSString *account, NSString *fileID, NSDate *date, NSString *message, NSInteger errorCode))completion
  695. {
  696. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  697. if (tableAccount == nil) {
  698. completion(account, nil, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  699. }
  700. NSString *path = [NSString stringWithFormat:@"%@/%@", serverUrl, fileName];
  701. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  702. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:tableAccount.url];
  703. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  704. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  705. [communication setUserAgent:[CCUtility getUserAgent]];
  706. [communication createFolder:path onCommunication:communication withForbiddenCharactersSupported:YES successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  707. NSDictionary *fields = [response allHeaderFields];
  708. NSString *fileID = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-FileId"]];
  709. NSDate *date = [CCUtility dateEnUsPosixFromCloud:[fields objectForKey:@"Date"]];
  710. completion(account, fileID, date, nil, 0);
  711. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  712. NSString *message;
  713. if (([fileName isEqualToString:autoUploadFileName] && [serverUrl isEqualToString:autoUploadDirectory]))
  714. message = nil;
  715. else
  716. message = [CCError manageErrorOC:response.statusCode error:error];
  717. NSInteger errorCode = response.statusCode;
  718. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  719. errorCode = error.code;
  720. // Request trusted certificated
  721. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  722. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  723. // Activity
  724. [[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];
  725. completion(account, nil, nil, message, errorCode);
  726. } errorBeforeRequest:^(NSError *error) {
  727. NSString *message;
  728. if (([fileName isEqualToString:autoUploadFileName] && [serverUrl isEqualToString:autoUploadDirectory]))
  729. message = nil;
  730. else {
  731. if (error.code == OCErrorForbidenCharacters)
  732. message = NSLocalizedString(@"_forbidden_characters_from_server_", nil);
  733. else
  734. message = NSLocalizedString(@"_unknow_response_server_", nil);
  735. }
  736. completion(account, nil, nil, message, error.code);
  737. }];
  738. }
  739. #pragma --------------------------------------------------------------------------------------------
  740. #pragma mark ===== Delete =====
  741. #pragma --------------------------------------------------------------------------------------------
  742. - (void)deleteFileOrFolderWithAccount:(NSString *)account path:(NSString *)path completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  743. {
  744. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  745. if (tableAccount == nil) {
  746. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  747. }
  748. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  749. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  750. [communication setUserAgent:[CCUtility getUserAgent]];
  751. [communication deleteFileOrFolder:path onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  752. completion(account, nil, 0);
  753. } failureRquest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  754. NSString *message;
  755. NSInteger errorCode = response.statusCode;
  756. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  757. errorCode = error.code;
  758. // Error
  759. if (errorCode == 503)
  760. message = NSLocalizedString(@"_server_error_retry_", nil);
  761. else
  762. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  763. // Request trusted certificated
  764. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  765. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  766. // Activity
  767. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionDeleteFileFolder selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:tableAccount.url];
  768. completion(account, message, errorCode);
  769. }];
  770. }
  771. #pragma --------------------------------------------------------------------------------------------
  772. #pragma mark ===== Move =====
  773. #pragma --------------------------------------------------------------------------------------------
  774. - (void)moveFileOrFolderWithAccount:(NSString *)account fileName:(NSString *)fileName fileNameTo:(NSString *)fileNameTo completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  775. {
  776. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  777. if (tableAccount == nil) {
  778. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  779. }
  780. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  781. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  782. [communication setUserAgent:[CCUtility getUserAgent]];
  783. [communication moveFileOrFolder:fileName toDestiny:fileNameTo onCommunication:communication withForbiddenCharactersSupported:YES successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  784. completion(account, nil, 0);
  785. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  786. NSInteger errorCode = response.statusCode;
  787. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  788. errorCode = error.code;
  789. NSString *message = [CCError manageErrorOC:response.statusCode error:error];
  790. completion(account, message, error.code);
  791. } errorBeforeRequest:^(NSError *error) {
  792. NSString *message;
  793. if (error.code == OCErrorMovingTheDestinyAndOriginAreTheSame) {
  794. message = NSLocalizedString(@"_error_folder_destiny_is_the_same_", nil);
  795. } else if (error.code == OCErrorMovingFolderInsideHimself) {
  796. message = NSLocalizedString(@"_error_folder_destiny_is_the_same_", nil);
  797. } else if (error.code == OCErrorMovingDestinyNameHaveForbiddenCharacters) {
  798. message = NSLocalizedString(@"_forbidden_characters_from_server_", nil);
  799. } else {
  800. message = NSLocalizedString(@"_unknow_response_server_", nil);
  801. }
  802. completion(account, message, error.code);
  803. }];
  804. }
  805. #pragma --------------------------------------------------------------------------------------------
  806. #pragma mark ===== Shared =====
  807. #pragma --------------------------------------------------------------------------------------------
  808. - (void)readShareServerWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  809. {
  810. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  811. if (tableAccount == nil) {
  812. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  813. }
  814. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  815. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  816. [communication setUserAgent:[CCUtility getUserAgent]];
  817. [communication readSharedByServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  818. completion(account, items, nil, 0);
  819. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  820. NSString *message = @"";
  821. NSInteger errorCode = response.statusCode;
  822. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  823. errorCode = error.code;
  824. // Error
  825. if (errorCode == 503) {
  826. message = NSLocalizedString(@"_server_error_retry_", nil);
  827. } else {
  828. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  829. }
  830. // Request trusted certificated
  831. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  832. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  833. completion(account, nil, message, errorCode);
  834. }];
  835. }
  836. /*
  837. - (void)readShareServer
  838. {
  839. #ifndef EXTENSION
  840. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  841. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  842. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  843. [communication setUserAgent:[CCUtility getUserAgent]];
  844. [communication readSharedByServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  845. // Test active account
  846. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  847. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  848. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)])
  849. [self.delegate shareFailure:_metadataNet message:NSLocalizedString(@"_error_user_not_available_", nil) errorCode:k_CCErrorUserNotAvailble];
  850. [self complete];
  851. return;
  852. }
  853. BOOL openWindow = NO;
  854. [appDelegate.sharesID removeAllObjects];
  855. if ([recordAccount.account isEqualToString:_metadataNet.account]) {
  856. for (OCSharedDto *item in items)
  857. [appDelegate.sharesID setObject:item forKey:[@(item.idRemoteShared) stringValue]];
  858. if ([_metadataNet.selector isEqual:selectorOpenWindowShare]) openWindow = YES;
  859. if ([_metadataNet.action isEqual:actionUpdateShare]) openWindow = YES;
  860. if ([_metadataNet.action isEqual:actionShare]) openWindow = YES;
  861. if ([_metadataNet.action isEqual:actionShareWith]) openWindow = YES;
  862. }
  863. if([self.delegate respondsToSelector:@selector(readSharedSuccess:items:openWindow:)])
  864. [self.delegate readSharedSuccess:_metadataNet items:appDelegate.sharesID openWindow:openWindow];
  865. [self complete];
  866. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  867. NSInteger errorCode = response.statusCode;
  868. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  869. errorCode = error.code;
  870. // Error
  871. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  872. if (errorCode == 503)
  873. [self.delegate shareFailure:_metadataNet message:NSLocalizedString(@"_server_error_retry_", nil) errorCode:errorCode];
  874. else
  875. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  876. }
  877. // Request trusted certificated
  878. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  879. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  880. [self complete];
  881. }];
  882. #endif
  883. }
  884. */
  885. - (void)share
  886. {
  887. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  888. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  889. [communication setUserAgent:[CCUtility getUserAgent]];
  890. [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) {
  891. //[self readShareServer];
  892. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  893. NSInteger errorCode = response.statusCode;
  894. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  895. errorCode = error.code;
  896. // Error
  897. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  898. if (errorCode == 503)
  899. [self.delegate shareFailure:_metadataNet message:NSLocalizedString(@"_server_error_retry_", nil) errorCode:errorCode];
  900. else
  901. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  902. }
  903. // Request trusted certificated
  904. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  905. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  906. [self complete];
  907. }];
  908. }
  909. // * @param shareeType -> NSInteger: to set the type of sharee (user/group/federated)
  910. - (void)shareWith
  911. {
  912. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  913. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  914. [communication setUserAgent:[CCUtility getUserAgent]];
  915. [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) {
  916. //[self readShareServer];
  917. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  918. NSInteger errorCode = response.statusCode;
  919. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  920. errorCode = error.code;
  921. // Error
  922. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  923. if (errorCode == 503)
  924. [self.delegate shareFailure:_metadataNet message:NSLocalizedString(@"_server_error_retry_", nil) errorCode:errorCode];
  925. else
  926. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  927. }
  928. // Request trusted certificated
  929. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  930. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  931. [self complete];
  932. }];
  933. }
  934. - (void)updateShare
  935. {
  936. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  937. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  938. [communication setUserAgent:[CCUtility getUserAgent]];
  939. [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) {
  940. //[self readShareServer];
  941. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  942. #ifndef EXTENSION
  943. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  944. [appDelegate messageNotification:@"_error_" description:[CCError manageErrorOC:response.statusCode error:error] visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:error.code];
  945. #endif
  946. NSInteger errorCode = response.statusCode;
  947. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  948. errorCode = error.code;
  949. // Error
  950. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  951. if (errorCode == 503)
  952. [self.delegate shareFailure:_metadataNet message:NSLocalizedString(@"_server_error_retry_", nil) errorCode:errorCode];
  953. else
  954. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  955. }
  956. // Request trusted certificated
  957. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  958. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  959. [self complete];
  960. }];
  961. }
  962. - (void)unShare
  963. {
  964. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  965. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  966. [communication setUserAgent:[CCUtility getUserAgent]];
  967. [communication unShareFileOrFolderByServer:[_activeUrl stringByAppendingString:@"/"] andIdRemoteShared:[_metadataNet.share intValue] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  968. if([self.delegate respondsToSelector:@selector(unShareSuccess:)])
  969. [self.delegate unShareSuccess:_metadataNet];
  970. [self complete];
  971. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  972. #ifndef EXTENSION
  973. [(AppDelegate *)[[UIApplication sharedApplication] delegate] messageNotification:@"_error_" description:[CCError manageErrorOC:response.statusCode error:error] visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:error.code];
  974. #endif
  975. NSInteger errorCode = response.statusCode;
  976. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  977. errorCode = error.code;
  978. // Error
  979. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  980. if (errorCode == 503)
  981. [self.delegate shareFailure:_metadataNet message:NSLocalizedString(@"_server_error_retry_", nil) errorCode:errorCode];
  982. else
  983. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  984. }
  985. // Request trusted certificated
  986. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  987. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  988. [self complete];
  989. }];
  990. }
  991. - (void)getUserAndGroup
  992. {
  993. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  994. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  995. [communication setUserAgent:[CCUtility getUserAgent]];
  996. [communication searchUsersAndGroupsWith:_metadataNet.optionAny forPage:1 with:50 ofServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *itemList, NSString *redirectedServer) {
  997. if([self.delegate respondsToSelector:@selector(getUserAndGroupSuccess:items:)])
  998. [self.delegate getUserAndGroupSuccess:_metadataNet items:itemList];
  999. [self complete];
  1000. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1001. NSInteger errorCode = response.statusCode;
  1002. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1003. errorCode = error.code;
  1004. // Error
  1005. if ([self.delegate respondsToSelector:@selector(getUserAndGroupFailure:message:errorCode:)]) {
  1006. if (errorCode == 503)
  1007. [self.delegate getUserAndGroupFailure:_metadataNet message:NSLocalizedString(@"_server_error_retry_", nil) errorCode:errorCode];
  1008. else
  1009. [self.delegate getUserAndGroupFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1010. }
  1011. // Request trusted certificated
  1012. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1013. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1014. [self complete];
  1015. }];
  1016. }
  1017. - (void)getSharePermissionsFile
  1018. {
  1019. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1020. NSString *fileName = [NSString stringWithFormat:@"%@/%@", _metadataNet.serverUrl, _metadataNet.fileName];
  1021. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1022. [communication setUserAgent:[CCUtility getUserAgent]];
  1023. [communication getSharePermissionsFile:fileName onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *permissions, NSString *redirectedServer) {
  1024. // Test active account
  1025. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  1026. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  1027. if ([self.delegate respondsToSelector:@selector(getSharePermissionsFileFailure:message:errorCode:)])
  1028. [self.delegate getSharePermissionsFileFailure:_metadataNet message:NSLocalizedString(@"_error_user_not_available_", nil) errorCode:k_CCErrorUserNotAvailble];
  1029. [self complete];
  1030. return;
  1031. }
  1032. if([self.delegate respondsToSelector:@selector(getSharePermissionsFileSuccess:permissions:)])
  1033. [self.delegate getSharePermissionsFileSuccess:_metadataNet permissions:permissions];
  1034. [self complete];
  1035. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1036. NSInteger errorCode = response.statusCode;
  1037. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1038. errorCode = error.code;
  1039. // Error
  1040. if ([self.delegate respondsToSelector:@selector(getSharePermissionsFileFailure:message:errorCode:)]) {
  1041. if (errorCode == 503)
  1042. [self.delegate getSharePermissionsFileFailure:_metadataNet message:NSLocalizedString(@"_server_error_retry_", nil) errorCode:errorCode];
  1043. else
  1044. [self.delegate getSharePermissionsFileFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1045. }
  1046. // Request trusted certificated
  1047. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1048. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1049. [self complete];
  1050. }];
  1051. }
  1052. #pragma --------------------------------------------------------------------------------------------
  1053. #pragma mark ===== Activity =====
  1054. #pragma --------------------------------------------------------------------------------------------
  1055. - (void)getActivityWithAccount:(NSString *)account completion:(void(^)(NSString *account, NSArray *listOfActivity, NSString *message, NSInteger errorCode))completion
  1056. {
  1057. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1058. if (tableAccount == nil) {
  1059. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1060. }
  1061. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1062. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  1063. [communication setUserAgent:[CCUtility getUserAgent]];
  1064. [communication getActivityServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfActivity, NSString *redirectedServer) {
  1065. completion(account, listOfActivity, nil, 0);
  1066. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1067. NSString *message;
  1068. NSInteger errorCode = response.statusCode;
  1069. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1070. errorCode = error.code;
  1071. // Error
  1072. if (errorCode == 503)
  1073. message = NSLocalizedString(@"_server_error_retry_", nil);
  1074. else
  1075. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1076. completion(account, nil, message, errorCode);
  1077. }];
  1078. }
  1079. #pragma --------------------------------------------------------------------------------------------
  1080. #pragma mark ===== External Sites =====
  1081. #pragma --------------------------------------------------------------------------------------------
  1082. - (void)getExternalSitesServer:(NSString *)account completion:(void (^)(NSString *account, NSArray *listOfExternalSites, NSString *message, NSInteger errorCode))completion
  1083. {
  1084. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1085. if (tableAccount == nil) {
  1086. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1087. }
  1088. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1089. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  1090. [communication setUserAgent:[CCUtility getUserAgent]];
  1091. [communication getExternalSitesServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfExternalSites, NSString *redirectedServer) {
  1092. completion(account, listOfExternalSites, nil, 0);
  1093. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1094. NSString *message = @"";
  1095. NSInteger errorCode = response.statusCode;
  1096. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1097. errorCode = error.code;
  1098. // Error
  1099. if (errorCode == 503) {
  1100. message = NSLocalizedString(@"_server_error_retry_", nil);
  1101. } else {
  1102. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1103. }
  1104. // Request trusted certificated
  1105. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1106. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1107. completion(account, nil, message, errorCode);
  1108. }];
  1109. }
  1110. #pragma --------------------------------------------------------------------------------------------
  1111. #pragma mark ===== Middleware Ping =====
  1112. #pragma --------------------------------------------------------------------------------------------
  1113. /*
  1114. - (void)middlewarePing
  1115. {
  1116. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1117. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  1118. [communication setUserAgent:[CCUtility getUserAgent]];
  1119. [communication getMiddlewarePing:_metadataNet.serverUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfExternalSites, NSString *redirectedServer) {
  1120. [self complete];
  1121. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1122. NSInteger errorCode = response.statusCode;
  1123. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1124. errorCode = error.code;
  1125. // Error
  1126. if ([self.delegate respondsToSelector:@selector(getExternalSitesServerFailure:message:errorCode:)]) {
  1127. if (errorCode == 503)
  1128. [self.delegate getExternalSitesServerFailure:_metadataNet message:NSLocalizedString(@"_server_error_retry_", nil) errorCode:errorCode];
  1129. else
  1130. [self.delegate getExternalSitesServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1131. }
  1132. // Request trusted certificated
  1133. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1134. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1135. [self complete];
  1136. }];
  1137. }
  1138. */
  1139. #pragma --------------------------------------------------------------------------------------------
  1140. #pragma mark ===== Notification =====
  1141. #pragma --------------------------------------------------------------------------------------------
  1142. - (void)getNotificationServer:(NSString *)account completion:(void (^)(NSString *account, NSArray *listOfNotifications, NSString *message, NSInteger errorCode))completion
  1143. {
  1144. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1145. if (tableAccount == nil) {
  1146. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1147. }
  1148. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1149. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  1150. [communication setUserAgent:[CCUtility getUserAgent]];
  1151. [communication getNotificationServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfNotifications, NSString *redirectedServer) {
  1152. completion(account, listOfNotifications, nil, 0);
  1153. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1154. NSString *message = @"";
  1155. NSInteger errorCode = response.statusCode;
  1156. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1157. errorCode = error.code;
  1158. // Error
  1159. if (errorCode == 503) {
  1160. message = NSLocalizedString(@"_server_error_retry_", nil);
  1161. } else {
  1162. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1163. }
  1164. // Request trusted certificated
  1165. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1166. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1167. // Activity
  1168. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionGetNotification selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:@""];
  1169. completion(account, nil, message, errorCode);
  1170. }];
  1171. }
  1172. - (void)setNotificationServer:(NSString *)account serverUrl:(NSString *)serverUrl type:(NSString *)type completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  1173. {
  1174. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1175. if (tableAccount == nil) {
  1176. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1177. }
  1178. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1179. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  1180. [communication setUserAgent:[CCUtility getUserAgent]];
  1181. [communication setNotificationServer:serverUrl type:type onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1182. completion(account, nil, 0);
  1183. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1184. NSString *message = @"";
  1185. NSInteger errorCode = response.statusCode;
  1186. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1187. errorCode = error.code;
  1188. // Error
  1189. if (errorCode == 503) {
  1190. message = NSLocalizedString(@"_server_error_retry_", nil);
  1191. } else {
  1192. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1193. }
  1194. // Request trusted certificated
  1195. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1196. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1197. completion(account, message, errorCode);
  1198. }];
  1199. }
  1200. #pragma --------------------------------------------------------------------------------------------
  1201. #pragma mark ===== Push Notification =====
  1202. #pragma --------------------------------------------------------------------------------------------
  1203. - (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
  1204. {
  1205. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1206. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1207. [communication setUserAgent:[CCUtility getUserAgent]];
  1208. devicePublicKey = [CCUtility URLEncodeStringFromString:devicePublicKey];
  1209. [communication subscribingNextcloudServerPush:url pushTokenHash:pushTokenHash devicePublicKey:devicePublicKey proxyServerPath: [NCBrandOptions sharedInstance].pushNotificationServerProxy onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *deviceIdentifier, NSString *signature, NSString *redirectedServer) {
  1210. deviceIdentifier = [CCUtility URLEncodeStringFromString:deviceIdentifier];
  1211. signature = [CCUtility URLEncodeStringFromString:signature];
  1212. publicKey = [CCUtility URLEncodeStringFromString:publicKey];
  1213. [communication subscribingPushProxy:[NCBrandOptions sharedInstance].pushNotificationServerProxy pushToken:pushToken deviceIdentifier:deviceIdentifier deviceIdentifierSignature:signature publicKey:publicKey onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1214. // Activity
  1215. [[NCManageDatabase sharedInstance] addActivityClient:[NCBrandOptions sharedInstance].pushNotificationServerProxy fileID:@"" action:k_activityDebugActionSubscribingPushProxy selector:@"" note:@"Service registered." type:k_activityTypeSuccess verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1216. success(deviceIdentifier, signature, publicKey);
  1217. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1218. NSString *message;
  1219. NSInteger errorCode = response.statusCode;
  1220. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1221. errorCode = error.code;
  1222. // Error
  1223. if (errorCode == 503)
  1224. message = NSLocalizedString(@"_server_error_retry_", nil);
  1225. else
  1226. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1227. // Activity
  1228. [[NCManageDatabase sharedInstance] addActivityClient:[NCBrandOptions sharedInstance].pushNotificationServerProxy fileID:@"" action:k_activityDebugActionSubscribingPushProxy selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1229. failure(message, errorCode);
  1230. }];
  1231. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1232. NSString *message;
  1233. NSInteger errorCode = response.statusCode;
  1234. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1235. errorCode = error.code;
  1236. // Error
  1237. if (errorCode == 503)
  1238. message = NSLocalizedString(@"_server_error_retry_", nil);
  1239. else
  1240. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1241. // Activity
  1242. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionSubscribingServerPush selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1243. failure(message, errorCode);
  1244. }];
  1245. }
  1246. - (void)unsubscribingPushNotificationServer:(NSString *)url deviceIdentifier:(NSString *)deviceIdentifier deviceIdentifierSignature:(NSString *)deviceIdentifierSignature publicKey:(NSString *)publicKey success:(void (^)(void))success failure:(void (^)(NSString *message, NSInteger errorCode))failure {
  1247. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1248. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1249. [communication setUserAgent:[CCUtility getUserAgent]];
  1250. [communication unsubscribingNextcloudServerPush:url onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1251. [communication unsubscribingPushProxy:[NCBrandOptions sharedInstance].pushNotificationServerProxy deviceIdentifier:deviceIdentifier deviceIdentifierSignature:deviceIdentifierSignature publicKey:publicKey onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1252. success();
  1253. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1254. NSString *message;
  1255. NSInteger errorCode = response.statusCode;
  1256. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1257. errorCode = error.code;
  1258. // Error
  1259. if (errorCode == 503)
  1260. message = NSLocalizedString(@"_server_error_retry_", nil);
  1261. else
  1262. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1263. // Activity
  1264. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionUnsubscribingPushProxy selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1265. failure(message, errorCode);
  1266. }];
  1267. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1268. NSString *message;
  1269. NSInteger errorCode = response.statusCode;
  1270. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1271. errorCode = error.code;
  1272. // Error
  1273. if (errorCode == 503)
  1274. message = NSLocalizedString(@"_server_error_retry_", nil);
  1275. else
  1276. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1277. // Activity
  1278. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionUnsubscribingServerPush selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1279. failure(message, errorCode);
  1280. }];
  1281. }
  1282. #pragma --------------------------------------------------------------------------------------------
  1283. #pragma mark ===== User Profile =====
  1284. #pragma --------------------------------------------------------------------------------------------
  1285. - (void)getUserProfile:(NSString *)account completion:(void (^)(NSString *account, OCUserProfile *userProfile, NSString *message, NSInteger errorCode))completion
  1286. {
  1287. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1288. if (tableAccount == nil) {
  1289. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1290. }
  1291. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1292. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  1293. [communication setUserAgent:[CCUtility getUserAgent]];
  1294. [communication getUserProfileServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCUserProfile *userProfile, NSString *redirectedServer) {
  1295. completion(account, userProfile, nil, 0);
  1296. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1297. NSString *message = @"";
  1298. NSInteger errorCode = response.statusCode;
  1299. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1300. errorCode = error.code;
  1301. // Error
  1302. if (errorCode == 503) {
  1303. message = NSLocalizedString(@"_server_error_retry_", nil);
  1304. } else {
  1305. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1306. }
  1307. // Request trusted certificated
  1308. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1309. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1310. completion(account, nil, message, errorCode);
  1311. }];
  1312. }
  1313. #pragma --------------------------------------------------------------------------------------------
  1314. #pragma mark ===== Capabilities =====
  1315. #pragma --------------------------------------------------------------------------------------------
  1316. - (void)getCapabilitiesOfServer:(NSString *)account completion:(void (^)(NSString *account, OCCapabilities *capabilities, NSString *message, NSInteger errorCode))completion
  1317. {
  1318. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1319. if (tableAccount == nil) {
  1320. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1321. }
  1322. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1323. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  1324. [communication setUserAgent:[CCUtility getUserAgent]];
  1325. [communication getCapabilitiesOfServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCCapabilities *capabilities, NSString *redirectedServer) {
  1326. completion(account, capabilities, nil, 0);
  1327. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1328. NSString *message = @"";
  1329. NSInteger errorCode = response.statusCode;
  1330. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1331. errorCode = error.code;
  1332. // Error
  1333. if (errorCode == 503) {
  1334. message = NSLocalizedString(@"_server_error_retry_", nil);
  1335. } else {
  1336. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1337. }
  1338. // Request trusted certificated
  1339. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1340. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1341. // Activity
  1342. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionCapabilities selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1343. completion(account, nil, message, errorCode);
  1344. }];
  1345. }
  1346. #pragma --------------------------------------------------------------------------------------------
  1347. #pragma mark ===== End-to-End Encryption =====
  1348. #pragma --------------------------------------------------------------------------------------------
  1349. - (void)getEndToEndPublicKeys
  1350. {
  1351. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1352. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1353. [communication setUserAgent:[CCUtility getUserAgent]];
  1354. [communication getEndToEndPublicKeys:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *redirectedServer) {
  1355. _metadataNet.key = publicKey;
  1356. if ([self.delegate respondsToSelector:@selector(getEndToEndPublicKeysSuccess:)])
  1357. [self.delegate getEndToEndPublicKeysSuccess:_metadataNet];
  1358. [self complete];
  1359. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1360. NSInteger errorCode = response.statusCode;
  1361. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1362. errorCode = error.code;
  1363. // Error
  1364. if ([self.delegate respondsToSelector:@selector(getEndToEndPublicKeysFailure:message:errorCode:)])
  1365. [self.delegate getEndToEndPublicKeysFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1366. // Request trusted certificated
  1367. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1368. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1369. [self complete];
  1370. }];
  1371. }
  1372. - (void)getEndToEndPrivateKeyCipher
  1373. {
  1374. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1375. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1376. [communication setUserAgent:[CCUtility getUserAgent]];
  1377. [communication getEndToEndPrivateKeyCipher:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *privateKeyChiper, NSString *redirectedServer) {
  1378. _metadataNet.key = privateKeyChiper;
  1379. if ([self.delegate respondsToSelector:@selector(getEndToEndPrivateKeyCipherSuccess:)])
  1380. [self.delegate getEndToEndPrivateKeyCipherSuccess:_metadataNet];
  1381. [self complete];
  1382. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1383. NSInteger errorCode = response.statusCode;
  1384. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1385. errorCode = error.code;
  1386. // Error
  1387. if ([self.delegate respondsToSelector:@selector(getEndToEndPrivateKeyCipherFailure:message:errorCode:)])
  1388. [self.delegate getEndToEndPrivateKeyCipherFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1389. // Request trusted certificated
  1390. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1391. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1392. [self complete];
  1393. }];
  1394. }
  1395. - (void)signEndToEndPublicKey
  1396. {
  1397. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1398. // URL Encode
  1399. NSString *publicKey = [CCUtility URLEncodeStringFromString:_metadataNet.key];
  1400. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1401. [communication setUserAgent:[CCUtility getUserAgent]];
  1402. [communication signEndToEndPublicKey:[_activeUrl stringByAppendingString:@"/"] publicKey:publicKey onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *redirectedServer) {
  1403. _metadataNet.key = publicKey;
  1404. if ([self.delegate respondsToSelector:@selector(signEndToEndPublicKeySuccess:)])
  1405. [self.delegate signEndToEndPublicKeySuccess:_metadataNet];
  1406. [self complete];
  1407. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1408. NSInteger errorCode = response.statusCode;
  1409. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1410. errorCode = error.code;
  1411. // Error
  1412. if ([self.delegate respondsToSelector:@selector(signEndToEndPublicKeyFailure:message:errorCode:)])
  1413. [self.delegate signEndToEndPublicKeyFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1414. // Request trusted certificated
  1415. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1416. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1417. [self complete];
  1418. }];
  1419. }
  1420. - (void)storeEndToEndPrivateKeyCipher
  1421. {
  1422. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1423. // URL Encode
  1424. NSString *privateKeyChiper = [CCUtility URLEncodeStringFromString:_metadataNet.keyCipher];
  1425. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1426. [communication setUserAgent:[CCUtility getUserAgent]];
  1427. [communication storeEndToEndPrivateKeyCipher:[_activeUrl stringByAppendingString:@"/"] privateKeyChiper:privateKeyChiper onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *privateKey, NSString *redirectedServer) {
  1428. if ([self.delegate respondsToSelector:@selector(storeEndToEndPrivateKeyCipherSuccess:)])
  1429. [self.delegate storeEndToEndPrivateKeyCipherSuccess:_metadataNet];
  1430. [self complete];
  1431. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1432. NSInteger errorCode = response.statusCode;
  1433. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1434. errorCode = error.code;
  1435. // Error
  1436. if ([self.delegate respondsToSelector:@selector(storeEndToEndPrivateKeyCipherFailure:message:errorCode:)])
  1437. [self.delegate storeEndToEndPrivateKeyCipherFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1438. // Request trusted certificated
  1439. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1440. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1441. [self complete];
  1442. }];
  1443. }
  1444. - (void)deleteEndToEndPublicKey
  1445. {
  1446. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1447. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1448. [communication setUserAgent:[CCUtility getUserAgent]];
  1449. [communication deleteEndToEndPublicKey:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1450. if ([self.delegate respondsToSelector:@selector(deleteEndToEndPublicKeySuccess:)])
  1451. [self.delegate deleteEndToEndPublicKeySuccess:_metadataNet];
  1452. [self complete];
  1453. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1454. NSInteger errorCode = response.statusCode;
  1455. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1456. errorCode = error.code;
  1457. // Error
  1458. if ([self.delegate respondsToSelector:@selector(deleteEndToEndPublicKeyFailure:message:errorCode:)])
  1459. [self.delegate deleteEndToEndPublicKeyFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1460. // Request trusted certificated
  1461. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1462. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1463. [self complete];
  1464. }];
  1465. }
  1466. - (void)deleteEndToEndPrivateKey
  1467. {
  1468. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1469. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1470. [communication setUserAgent:[CCUtility getUserAgent]];
  1471. [communication deleteEndToEndPrivateKey:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1472. if ([self.delegate respondsToSelector:@selector(deleteEndToEndPrivateKeySuccess:)])
  1473. [self.delegate deleteEndToEndPrivateKeySuccess:_metadataNet];
  1474. [self complete];
  1475. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1476. NSInteger errorCode = response.statusCode;
  1477. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1478. errorCode = error.code;
  1479. // Error
  1480. if ([self.delegate respondsToSelector:@selector(deleteEndToEndPrivateKeyFailure:message:errorCode:)])
  1481. [self.delegate deleteEndToEndPrivateKeyFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1482. // Request trusted certificated
  1483. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1484. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1485. [self complete];
  1486. }];
  1487. }
  1488. - (void)getEndToEndServerPublicKey
  1489. {
  1490. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1491. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1492. [communication setUserAgent:[CCUtility getUserAgent]];
  1493. [communication getEndToEndServerPublicKey:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *redirectedServer) {
  1494. _metadataNet.key = publicKey;
  1495. if ([self.delegate respondsToSelector:@selector(getEndToEndServerPublicKeySuccess:)])
  1496. [self.delegate getEndToEndServerPublicKeySuccess:_metadataNet];
  1497. [self complete];
  1498. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1499. NSInteger errorCode = response.statusCode;
  1500. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1501. errorCode = error.code;
  1502. // Error
  1503. if ([self.delegate respondsToSelector:@selector(getEndToEndServerPublicKeyFailure:message:errorCode:)])
  1504. [self.delegate getEndToEndServerPublicKeyFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1505. // Request trusted certificated
  1506. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1507. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1508. [self complete];
  1509. }];
  1510. }
  1511. #pragma --------------------------------------------------------------------------------------------
  1512. #pragma mark ===== Manage Mobile Editor OCS API =====
  1513. #pragma --------------------------------------------------------------------------------------------
  1514. - (void)createLinkRichdocumentsWithFileID:(NSString *)fileID success:(void(^)(NSString *link))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  1515. {
  1516. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1517. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1518. [communication setUserAgent:[CCUtility getUserAgent]];
  1519. NSString *fileIDServer = [[NCUtility sharedInstance] convertFileIDClientToFileIDServer:fileID];
  1520. [communication createLinkRichdocuments:[_activeUrl stringByAppendingString:@"/"] fileID:fileIDServer onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *link, NSString *redirectedServer) {
  1521. success(link);
  1522. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1523. NSString *message;
  1524. NSInteger errorCode = response.statusCode;
  1525. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1526. errorCode = error.code;
  1527. // Error
  1528. if (errorCode == 503)
  1529. message = NSLocalizedString(@"_server_error_retry_", nil);
  1530. else
  1531. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1532. // Activity
  1533. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionUnsubscribingServerPush selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1534. failure(message, errorCode);
  1535. }];
  1536. }
  1537. - (void)geTemplatesRichdocumentsWithTypeTemplate:(NSString *)typeTemplate success:(void(^)(NSArray *listOfTemplate))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  1538. {
  1539. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1540. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1541. [communication setUserAgent:[CCUtility getUserAgent]];
  1542. [communication geTemplatesRichdocuments:[_activeUrl stringByAppendingString:@"/"] typeTemplate:typeTemplate onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfTemplate, NSString *redirectedServer) {
  1543. success(listOfTemplate);
  1544. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1545. NSString *message;
  1546. NSInteger errorCode = response.statusCode;
  1547. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1548. errorCode = error.code;
  1549. // Error
  1550. if (errorCode == 503)
  1551. message = NSLocalizedString(@"_server_error_retry_", nil);
  1552. else
  1553. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1554. // Activity
  1555. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionUnsubscribingServerPush selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1556. failure(message, errorCode);
  1557. }];
  1558. }
  1559. - (void)createNewRichdocumentsWithFileName:(NSString *)fileName serverUrl:(NSString *)serverUrl templateID:(NSString *)templateID success:(void(^)(NSString *url))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  1560. {
  1561. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1562. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1563. [communication setUserAgent:[CCUtility getUserAgent]];
  1564. [communication createNewRichdocuments:[_activeUrl stringByAppendingString:@"/"] path:fileName templateID:templateID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *url, NSString *redirectedServer) {
  1565. success(url);
  1566. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1567. NSString *message;
  1568. NSInteger errorCode = response.statusCode;
  1569. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1570. errorCode = error.code;
  1571. // Error
  1572. if (errorCode == 503)
  1573. message = NSLocalizedString(@"_server_error_retry_", nil);
  1574. else
  1575. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1576. // Activity
  1577. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionUnsubscribingServerPush selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1578. failure(message, errorCode);
  1579. }];
  1580. }
  1581. - (void)createAssetRichdocumentsWithFileName:(NSString *)fileName serverUrl:(NSString *)serverUrl success:(void(^)(NSString *link))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  1582. {
  1583. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1584. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1585. [communication setUserAgent:[CCUtility getUserAgent]];
  1586. NSString *fileNamePath = [CCUtility returnFileNamePathFromFileName:fileName serverUrl:serverUrl activeUrl:_activeUrl];
  1587. [communication createAssetRichdocuments:[_activeUrl stringByAppendingString:@"/"] path:fileNamePath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *url, NSString *redirectedServer) {
  1588. success(url);
  1589. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1590. NSString *message;
  1591. NSInteger errorCode = response.statusCode;
  1592. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1593. errorCode = error.code;
  1594. // Error
  1595. if (errorCode == 503)
  1596. message = NSLocalizedString(@"_server_error_retry_", nil);
  1597. else
  1598. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1599. // Activity
  1600. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionUnsubscribingServerPush selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1601. failure(message, errorCode);
  1602. }];
  1603. }
  1604. #pragma --------------------------------------------------------------------------------------------
  1605. #pragma mark ===== Trash =====
  1606. #pragma --------------------------------------------------------------------------------------------
  1607. - (void)listingTrash:(NSString *)serverUrl path:(NSString *)path account:(NSString *)account success:(void(^)(NSArray *items))success failure:(void (^)(NSString *message, NSInteger errorCode))failure
  1608. {
  1609. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1610. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1611. [communication setUserAgent:[CCUtility getUserAgent]];
  1612. [communication listingTrash:[serverUrl stringByAppendingString:path] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  1613. // Test active account
  1614. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  1615. if (![recordAccount.account isEqualToString:account]) {
  1616. failure(NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1617. } else {
  1618. // Check items > 0
  1619. if ([items count] == 0) {
  1620. #ifndef EXTENSION
  1621. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  1622. [appDelegate messageNotification:@"Server error" description:@"Read Folder WebDAV : [items NULL] please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:k_CCErrorInternalError];
  1623. #endif
  1624. failure(NSLocalizedString(@"Read Folder WebDAV : [items NULL] please fix", nil), k_CCErrorInternalError);
  1625. } else {
  1626. NSMutableArray *listTrash = [NSMutableArray new];
  1627. //OCFileDto *itemDtoFolder = [items objectAtIndex:0];
  1628. if ([items count] > 1) {
  1629. for (NSUInteger i=1; i < [items count]; i++) {
  1630. OCFileDto *itemDto = [items objectAtIndex:i];
  1631. tableTrash *trash = [tableTrash new];
  1632. trash.account = account;
  1633. trash.date = [NSDate dateWithTimeIntervalSince1970:itemDto.date];
  1634. trash.directory = itemDto.isDirectory;
  1635. trash.fileID = itemDto.ocId;
  1636. trash.fileName = itemDto.fileName;
  1637. trash.filePath = itemDto.filePath;
  1638. trash.size = itemDto.size;
  1639. trash.trashbinFileName = itemDto.trashbinFileName;
  1640. trash.trashbinOriginalLocation = itemDto.trashbinOriginalLocation;
  1641. trash.trashbinDeletionTime = [NSDate dateWithTimeIntervalSince1970:itemDto.trashbinDeletionTime];
  1642. [CCUtility insertTypeFileIconName:trash.trashbinFileName metadata:(tableMetadata *)trash];
  1643. [listTrash addObject:trash];
  1644. }
  1645. }
  1646. success(listTrash);
  1647. }
  1648. }
  1649. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1650. NSString *message;
  1651. NSInteger errorCode = response.statusCode;
  1652. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1653. errorCode = error.code;
  1654. // Error
  1655. if (errorCode == 503)
  1656. message = NSLocalizedString(@"_server_error_retry_", nil);
  1657. else
  1658. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1659. // Activity
  1660. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionUnsubscribingServerPush selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1661. failure(message, errorCode);
  1662. }];
  1663. }
  1664. - (void)emptyTrash:(void (^)(NSString *message, NSInteger errorCode))completion
  1665. {
  1666. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1667. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  1668. [communication setUserAgent:[CCUtility getUserAgent]];
  1669. NSString *path = [NSString stringWithFormat:@"%@%@/trashbin/%@/trash", _activeUrl, k_dav, _activeUserID];
  1670. [communication emptyTrash:path onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1671. completion(nil, 0);
  1672. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1673. NSString *message;
  1674. NSInteger errorCode = response.statusCode;
  1675. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1676. errorCode = error.code;
  1677. // Error
  1678. if (errorCode == 503)
  1679. message = NSLocalizedString(@"_server_error_retry_", nil);
  1680. else
  1681. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1682. // Activity
  1683. [[NCManageDatabase sharedInstance] addActivityClient:_activeUrl fileID:@"" action:k_activityDebugActionUnsubscribingServerPush selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:_activeUrl];
  1684. completion(message, errorCode);
  1685. }];
  1686. }
  1687. @end
  1688. #pragma --------------------------------------------------------------------------------------------
  1689. #pragma mark ===== OCURLSessionManager =====
  1690. #pragma --------------------------------------------------------------------------------------------
  1691. @implementation OCURLSessionManager
  1692. - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
  1693. {
  1694. // The pinnning check
  1695. if ([[CCCertificate sharedManager] checkTrustedChallenge:challenge]) {
  1696. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  1697. } else {
  1698. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  1699. }
  1700. }
  1701. @end