OCNetworking.m 123 KB

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