OCNetworking.m 122 KB

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