OCNetworking.m 121 KB

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