OCNetworking.m 96 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942
  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. @implementation OCnetworking
  30. + (OCnetworking *)sharedManager {
  31. static OCnetworking *sharedManager;
  32. @synchronized(self)
  33. {
  34. if (!sharedManager) {
  35. sharedManager = [OCnetworking new];
  36. }
  37. return sharedManager;
  38. }
  39. }
  40. #pragma --------------------------------------------------------------------------------------------
  41. #pragma mark ===== Server =====
  42. #pragma --------------------------------------------------------------------------------------------
  43. - (void)checkServerUrl:(NSString *)serverUrl user:(NSString *)user userID:(NSString *)userID password:(NSString *)password completion:(void (^)(NSString *message, NSInteger errorCode))completion
  44. {
  45. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  46. [communication setCredentialsWithUser:user andUserID:userID andPassword:password];
  47. [communication setUserAgent:[CCUtility getUserAgent]];
  48. [communication checkServer:serverUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  49. completion(nil, 0);
  50. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  51. NSString *message;
  52. NSInteger errorCode = response.statusCode;
  53. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  54. errorCode = error.code;
  55. // Error
  56. if (errorCode == 503)
  57. message = NSLocalizedString(@"_server_error_retry_", nil);
  58. else
  59. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  60. completion(message, errorCode);
  61. }];
  62. }
  63. - (void)serverStatusUrl:(NSString *)serverUrl completion:(void(^)(NSString *serverProductName, NSInteger versionMajor, NSInteger versionMicro, NSInteger versionMinor, NSString *message, NSInteger errorCode))completion
  64. {
  65. NSString *urlTest = [serverUrl stringByAppendingString:k_serverStatus];
  66. // Remove stored cookies
  67. NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
  68. for (NSHTTPCookie *cookie in [storage cookies])
  69. {
  70. [storage deleteCookie:cookie];
  71. }
  72. NSMutableURLRequest* request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlTest] cachePolicy:0 timeoutInterval:20.0];
  73. [request addValue:[CCUtility getUserAgent] forHTTPHeaderField:@"User-Agent"];
  74. [request addValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  75. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  76. NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  77. NSURLSessionDataTask *task = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) {
  78. dispatch_sync(dispatch_get_main_queue(), ^{
  79. if (error) {
  80. NSString *message;
  81. NSInteger errorCode;
  82. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  83. errorCode = httpResponse.statusCode;
  84. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  85. errorCode = error.code;
  86. // Error
  87. if (errorCode == 503)
  88. message = NSLocalizedString(@"_server_error_retry_", nil);
  89. else
  90. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  91. completion(nil, 0, 0, 0, message, errorCode);
  92. } else {
  93. NSString *serverProductName = @"";
  94. NSString *serverVersion = @"0.0.0";
  95. NSString *serverVersionString = @"0.0.0";
  96. NSInteger versionMajor = 0;
  97. NSInteger versionMicro = 0;
  98. NSInteger versionMinor = 0;
  99. NSError *error;
  100. NSDictionary *jsongParsed = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
  101. if (error) {
  102. completion(nil, 0, 0, 0, error.description, error.code);
  103. return;
  104. }
  105. serverProductName = [[jsongParsed valueForKey:@"productname"] lowercaseString];
  106. serverVersion = [jsongParsed valueForKey:@"version"];
  107. serverVersionString = [jsongParsed valueForKey:@"versionstring"];
  108. NSArray *arrayVersion = [serverVersionString componentsSeparatedByString:@"."];
  109. if (arrayVersion.count == 1) {
  110. versionMajor = [arrayVersion[0] integerValue];
  111. } else if (arrayVersion.count == 2) {
  112. versionMajor = [arrayVersion[0] integerValue];
  113. versionMinor = [arrayVersion[1] integerValue];
  114. } else if (arrayVersion.count >= 3) {
  115. versionMajor = [arrayVersion[0] integerValue];
  116. versionMinor = [arrayVersion[1] integerValue];
  117. versionMicro = [arrayVersion[2] integerValue];
  118. }
  119. completion(serverProductName, versionMajor, versionMicro, versionMinor, nil, 0);
  120. }
  121. });
  122. }];
  123. [task resume];
  124. }
  125. #pragma --------------------------------------------------------------------------------------------
  126. #pragma mark ===== download / upload =====
  127. #pragma --------------------------------------------------------------------------------------------
  128. - (NSURLSessionTask *)downloadWithAccount:(NSString *)account fileNameServerUrl:(NSString *)fileNameServerUrl fileNameLocalPath:(NSString *)fileNameLocalPath communication:(OCCommunication *)communication completion:(void (^)(NSString *account, int64_t length, NSString *etag, NSDate *date, NSString *message, NSInteger errorCode))completion
  129. {
  130. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  131. if (tableAccount == nil) {
  132. completion(account, 0, nil, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  133. }
  134. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  135. [communication setUserAgent:[CCUtility getUserAgent]];
  136. NSURLSessionTask *sessionTask = [communication downloadFileSession:fileNameServerUrl toDestiny:fileNameLocalPath defaultPriority:YES onCommunication:communication progress:^(NSProgress *progress) {
  137. //float percent = roundf (progress.fractionCompleted * 100);
  138. } successRequest:^(NSURLResponse *response, NSURL *filePath) {
  139. int64_t totalUnitCount = 0;
  140. NSDate *date = [NSDate date];
  141. NSDateFormatter *dateFormatter = [NSDateFormatter new];
  142. NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
  143. [dateFormatter setLocale:enUSPOSIXLocale];
  144. [dateFormatter setDateFormat:@"EEE, dd MMM y HH:mm:ss zzz"];
  145. NSError *error;
  146. NSDictionary *fields = [(NSHTTPURLResponse*)response allHeaderFields];
  147. NSString *contentLength = [fields objectForKey:@"Content-Length"];
  148. if(contentLength) {
  149. totalUnitCount = (int64_t) [contentLength longLongValue];
  150. }
  151. NSString *dateString = [fields objectForKey:@"Date"];
  152. if (dateString) {
  153. if (![dateFormatter getObjectValue:&date forString:dateString range:nil error:&error]) {
  154. date = [NSDate date];
  155. }
  156. } else {
  157. date = [NSDate date];
  158. }
  159. NSString *etag = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-ETag"]];
  160. if (etag == nil) {
  161. completion(account, 0, nil, nil, NSLocalizedString(@"Internal error", nil), k_CCErrorInternalError);
  162. } else {
  163. completion(account, totalUnitCount, etag, date, nil, 0);
  164. }
  165. } failureRequest:^(NSURLResponse *response, NSError *error) {
  166. NSString *message;
  167. NSInteger errorCode;
  168. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  169. errorCode = httpResponse.statusCode;
  170. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  171. errorCode = error.code;
  172. // Error
  173. if (errorCode == 503)
  174. message = NSLocalizedString(@"_server_error_retry_", nil);
  175. else
  176. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  177. completion(account, 0, nil, nil, message, errorCode);
  178. }];
  179. return sessionTask;
  180. }
  181. - (NSURLSessionTask *)downloadWithAccount:(NSString *)account url:(NSString *)url fileNameLocalPath:(NSString *)fileNameLocalPath completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  182. {
  183. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  184. if (tableAccount == nil) {
  185. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  186. }
  187. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  188. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  189. [communication setUserAgent:[CCUtility getUserAgent]];
  190. NSURLSessionTask *sessionTask = [communication downloadFileSession:url toDestiny:fileNameLocalPath defaultPriority:YES onCommunication:communication progress:^(NSProgress *progress) {
  191. //float percent = roundf (progress.fractionCompleted * 100);
  192. } successRequest:^(NSURLResponse *response, NSURL *filePath) {
  193. completion(account, nil, 0);
  194. } failureRequest:^(NSURLResponse *response, NSError *error) {
  195. NSString *message;
  196. NSInteger errorCode;
  197. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  198. errorCode = httpResponse.statusCode;
  199. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  200. errorCode = error.code;
  201. // Error
  202. if (errorCode == 503)
  203. message = NSLocalizedString(@"_server_error_retry_", nil);
  204. else
  205. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  206. completion(account, message, errorCode);
  207. }];
  208. return sessionTask;
  209. }
  210. - (NSURLSessionTask *)uploadWithAccount:(NSString *)account fileNameServerUrl:(NSString *)fileNameServerUrl fileNameLocalPath:(NSString *)fileNameLocalPath communication:(OCCommunication *)communication completion:(void(^)(NSString *account, NSString *fileID, NSString *etag, NSDate *date, NSString *message, NSInteger errorCode))completion
  211. {
  212. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  213. if (tableAccount == nil) {
  214. completion(account, nil, nil, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  215. }
  216. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  217. [communication setUserAgent:[CCUtility getUserAgent]];
  218. NSURLSessionTask *sessionTask = [communication uploadFileSession:fileNameLocalPath toDestiny:fileNameServerUrl onCommunication:communication progress:^(NSProgress *progress) {
  219. //float percent = roundf (progress.fractionCompleted * 100);
  220. } successRequest:^(NSURLResponse *response, NSString *redirectedServer) {
  221. NSDictionary *fields = [(NSHTTPURLResponse*)response allHeaderFields];
  222. NSString *fileID = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-FileId"]];
  223. NSString *etag = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-ETag"]];
  224. NSDate *date = [CCUtility dateEnUsPosixFromCloud:[fields objectForKey:@"Date"]];
  225. completion(account, fileID, etag, date, nil, 0);
  226. } failureRequest:^(NSURLResponse *response, NSString *redirectedServer, NSError *error) {
  227. NSString *message;
  228. NSInteger errorCode;
  229. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
  230. errorCode = httpResponse.statusCode;
  231. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  232. errorCode = error.code;
  233. // Error
  234. if (errorCode == 503)
  235. message = NSLocalizedString(@"_server_error_retry_", nil);
  236. else
  237. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  238. completion(account, nil, nil, nil, message, errorCode);
  239. } failureBeforeRequest:^(NSError *error) {
  240. completion(account, nil, nil, nil, error.description, error.code);
  241. }];
  242. return sessionTask;
  243. }
  244. #pragma --------------------------------------------------------------------------------------------
  245. #pragma mark ===== WebDav =====
  246. #pragma --------------------------------------------------------------------------------------------
  247. - (void)readFolderWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl depth:(NSString *)depth completion:(void(^)(NSString *account, NSArray *metadatas, tableMetadata *metadataFolder, NSString *message, NSInteger errorCode))completion
  248. {
  249. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  250. if (tableAccount == nil) {
  251. completion(account, nil, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  252. }
  253. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  254. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  255. [communication setUserAgent:[CCUtility getUserAgent]];
  256. [communication readFolder:serverUrl depth:depth withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  257. // Check items > 0
  258. if ([items count] == 0) {
  259. #ifndef EXTENSION
  260. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  261. [appDelegate messageNotification:@"Server error" description:@"Read Folder WebDAV : [items NULL] please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:k_CCErrorInternalError];
  262. #endif
  263. completion(account, nil, nil, NSLocalizedString(@"Read Folder WebDAV : [items NULL] please fix", nil), k_CCErrorInternalError);
  264. } else {
  265. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  266. BOOL showHiddenFiles = [CCUtility getShowHiddenFiles];
  267. BOOL isFolderEncrypted = [CCUtility isFolderEncrypted:serverUrl account:account];
  268. // directory [0]
  269. OCFileDto *itemDtoFolder = [items objectAtIndex:0];
  270. //NSDate *date = [NSDate dateWithTimeIntervalSince1970:itemDtoDirectory.date];
  271. NSMutableArray *metadatas = [NSMutableArray new];
  272. tableMetadata *metadataFolder = [tableMetadata new];
  273. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  274. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:tableAccount.url];
  275. NSString *serverUrlFolder;
  276. // Metadata . (self Folder)
  277. if ([serverUrl isEqualToString:[CCUtility getHomeServerUrlActiveUrl:tableAccount.url]]) {
  278. // root folder
  279. serverUrlFolder = k_serverUrl_root;
  280. metadataFolder = [CCUtility trasformedOCFileToCCMetadata:itemDtoFolder fileName:@"." serverUrl:serverUrlFolder autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:account isFolderEncrypted:isFolderEncrypted];
  281. } else {
  282. serverUrlFolder = [CCUtility deletingLastPathComponentFromServerUrl:serverUrl];
  283. metadataFolder = [CCUtility trasformedOCFileToCCMetadata:itemDtoFolder fileName:[serverUrl lastPathComponent] serverUrl:serverUrlFolder autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:account isFolderEncrypted:isFolderEncrypted];
  284. }
  285. // Add metadata folder
  286. (void)[[NCManageDatabase sharedInstance] addDirectoryWithEncrypted:itemDtoFolder.isEncrypted favorite:itemDtoFolder.isFavorite fileID:itemDtoFolder.ocId permissions:itemDtoFolder.permissions serverUrl:serverUrl account:account];
  287. NSArray *itemsSortedArray = [items sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
  288. NSString *first = [(OCFileDto*)a fileName];
  289. NSString *second = [(OCFileDto*)b fileName];
  290. return [[first lowercaseString] compare:[second lowercaseString]];
  291. }];
  292. for (NSUInteger i=1; i < [itemsSortedArray count]; i++) {
  293. OCFileDto *itemDto = [itemsSortedArray objectAtIndex:i];
  294. NSString *fileName = [itemDto.fileName stringByReplacingOccurrencesOfString:@"/" withString:@""];
  295. // Skip hidden files
  296. if (fileName.length > 0) {
  297. if (!showHiddenFiles && [[fileName substringToIndex:1] isEqualToString:@"."])
  298. continue;
  299. } else {
  300. continue;
  301. }
  302. if (itemDto.isDirectory) {
  303. (void)[[NCManageDatabase sharedInstance] addDirectoryWithEncrypted:itemDto.isEncrypted favorite:itemDto.isFavorite fileID:itemDto.ocId permissions:itemDto.permissions serverUrl:[CCUtility stringAppendServerUrl:serverUrl addFileName:fileName] account:account];
  304. }
  305. [metadatas addObject:[CCUtility trasformedOCFileToCCMetadata:itemDto fileName:itemDto.fileName serverUrl:serverUrl autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:account isFolderEncrypted:isFolderEncrypted]];
  306. }
  307. dispatch_async(dispatch_get_main_queue(), ^{
  308. completion(account, metadatas, metadataFolder, nil, 0);
  309. });
  310. });
  311. }
  312. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  313. NSString *message;
  314. NSInteger errorCode = response.statusCode;
  315. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  316. errorCode = error.code;
  317. // Error
  318. if (errorCode == 503)
  319. message = NSLocalizedString(@"_server_error_retry_", nil);
  320. else
  321. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  322. // Request trusted certificated
  323. //if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  324. // [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  325. // Activity
  326. [[NCManageDatabase sharedInstance] addActivityClient:serverUrl fileID:@"" action:k_activityDebugActionReadFolder selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:tableAccount.url];
  327. completion(account, nil, nil, message, errorCode);
  328. }];
  329. }
  330. - (void)readFileWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl fileName:(NSString *)fileName completion:(void(^)(NSString *account, tableMetadata *metadata, NSString *message, NSInteger errorCode))completion
  331. {
  332. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  333. if (tableAccount == nil) {
  334. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  335. }
  336. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  337. NSString *fileNamePath;
  338. if (fileName) {
  339. fileNamePath = [NSString stringWithFormat:@"%@/%@", serverUrl, fileName];
  340. } else {
  341. fileName= @".";
  342. fileNamePath = serverUrl;
  343. }
  344. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  345. [communication setUserAgent:[CCUtility getUserAgent]];
  346. [communication readFile:fileNamePath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  347. BOOL isFolderEncrypted = [CCUtility isFolderEncrypted:serverUrl account:account];
  348. if ([items count] > 0) {
  349. tableMetadata *metadata = [tableMetadata new];
  350. OCFileDto *itemDto = [items objectAtIndex:0];
  351. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  352. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:tableAccount.url];
  353. metadata = [CCUtility trasformedOCFileToCCMetadata:itemDto fileName:fileName serverUrl:serverUrl autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:account isFolderEncrypted:isFolderEncrypted];
  354. completion(account, metadata, nil, 0);
  355. // BUG 1038 item == 0
  356. } else {
  357. #ifndef EXTENSION
  358. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  359. [appDelegate messageNotification:@"Server error" description:@"Read File WebDAV : [items NULL] please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:k_CCErrorInternalError];
  360. #endif
  361. completion(account, nil, NSLocalizedString(@"Read File WebDAV : [items NULL] please fix", nil), k_CCErrorInternalError);
  362. }
  363. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  364. NSString *message;
  365. NSInteger errorCode = response.statusCode;
  366. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  367. errorCode = error.code;
  368. // Error
  369. if (errorCode == 503)
  370. message = NSLocalizedString(@"_server_error_retry_", nil);
  371. else
  372. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  373. completion(account, nil, message, errorCode);
  374. }];
  375. }
  376. - (void)createFolderWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl fileName:(NSString *)fileName completion:(void(^)(NSString *account, NSString *fileID, NSDate *date, NSString *message, NSInteger errorCode))completion
  377. {
  378. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  379. if (tableAccount == nil) {
  380. completion(account, nil, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  381. }
  382. NSString *path = [NSString stringWithFormat:@"%@/%@", serverUrl, fileName];
  383. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  384. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:tableAccount.url];
  385. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  386. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  387. [communication setUserAgent:[CCUtility getUserAgent]];
  388. [communication createFolder:path onCommunication:communication withForbiddenCharactersSupported:YES successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  389. NSDictionary *fields = [response allHeaderFields];
  390. NSString *fileID = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-FileId"]];
  391. NSDate *date = [CCUtility dateEnUsPosixFromCloud:[fields objectForKey:@"Date"]];
  392. completion(account, fileID, date, nil, 0);
  393. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  394. NSString *message;
  395. if (([fileName isEqualToString:autoUploadFileName] && [serverUrl isEqualToString:autoUploadDirectory]))
  396. message = nil;
  397. else
  398. message = [CCError manageErrorOC:response.statusCode error:error];
  399. NSInteger errorCode = response.statusCode;
  400. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  401. errorCode = error.code;
  402. completion(account, nil, nil, message, errorCode);
  403. } errorBeforeRequest:^(NSError *error) {
  404. NSString *message;
  405. if (([fileName isEqualToString:autoUploadFileName] && [serverUrl isEqualToString:autoUploadDirectory]))
  406. message = nil;
  407. else {
  408. if (error.code == OCErrorForbidenCharacters)
  409. message = NSLocalizedString(@"_forbidden_characters_from_server_", nil);
  410. else
  411. message = NSLocalizedString(@"_unknow_response_server_", nil);
  412. }
  413. completion(account, nil, nil, message, error.code);
  414. }];
  415. }
  416. - (void)deleteFileOrFolderWithAccount:(NSString *)account path:(NSString *)path completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  417. {
  418. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  419. if (tableAccount == nil) {
  420. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  421. }
  422. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  423. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  424. [communication setUserAgent:[CCUtility getUserAgent]];
  425. [communication deleteFileOrFolder:path onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  426. completion(account, nil, 0);
  427. } failureRquest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  428. NSString *message;
  429. NSInteger errorCode = response.statusCode;
  430. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  431. errorCode = error.code;
  432. // Error
  433. if (errorCode == 503)
  434. message = NSLocalizedString(@"_server_error_retry_", nil);
  435. else
  436. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  437. completion(account, message, errorCode);
  438. }];
  439. }
  440. - (void)moveFileOrFolderWithAccount:(NSString *)account fileName:(NSString *)fileName fileNameTo:(NSString *)fileNameTo completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  441. {
  442. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  443. if (tableAccount == nil) {
  444. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  445. }
  446. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  447. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  448. [communication setUserAgent:[CCUtility getUserAgent]];
  449. [communication moveFileOrFolder:fileName toDestiny:fileNameTo onCommunication:communication withForbiddenCharactersSupported:YES successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  450. completion(account, nil, 0);
  451. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  452. NSInteger errorCode = response.statusCode;
  453. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  454. errorCode = error.code;
  455. NSString *message = [CCError manageErrorOC:response.statusCode error:error];
  456. completion(account, message, error.code);
  457. } errorBeforeRequest:^(NSError *error) {
  458. NSString *message;
  459. if (error.code == OCErrorMovingTheDestinyAndOriginAreTheSame) {
  460. message = NSLocalizedString(@"_error_folder_destiny_is_the_same_", nil);
  461. } else if (error.code == OCErrorMovingFolderInsideHimself) {
  462. message = NSLocalizedString(@"_error_folder_destiny_is_the_same_", nil);
  463. } else if (error.code == OCErrorMovingDestinyNameHaveForbiddenCharacters) {
  464. message = NSLocalizedString(@"_forbidden_characters_from_server_", nil);
  465. } else {
  466. message = NSLocalizedString(@"_unknow_response_server_", nil);
  467. }
  468. completion(account, message, error.code);
  469. }];
  470. }
  471. - (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
  472. {
  473. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  474. if (tableAccount == nil) {
  475. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  476. }
  477. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  478. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  479. [communication setUserAgent:[CCUtility getUserAgent]];
  480. NSString *path = [tableAccount.url stringByAppendingString:k_dav];
  481. NSString *folder = [serverUrl stringByReplacingOccurrencesOfString:[CCUtility getHomeServerUrlActiveUrl:tableAccount.url] withString:@""];
  482. NSString *dateLastModified;
  483. if (date) {
  484. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  485. NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
  486. [dateFormatter setLocale:enUSPOSIXLocale];
  487. [dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
  488. dateLastModified = [dateFormatter stringFromDate:date];
  489. }
  490. [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) {
  491. NSMutableArray *metadatas = [NSMutableArray new];
  492. BOOL showHiddenFiles = [CCUtility getShowHiddenFiles];
  493. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  494. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:tableAccount.url];
  495. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  496. for (OCFileDto *itemDto in items) {
  497. NSString *serverUrl;
  498. BOOL isFolderEncrypted;
  499. NSString *fileName = [itemDto.fileName stringByReplacingOccurrencesOfString:@"/" withString:@""];
  500. // Skip hidden files
  501. if (fileName.length > 0) {
  502. if (!showHiddenFiles && [[fileName substringToIndex:1] isEqualToString:@"."])
  503. continue;
  504. } else
  505. continue;
  506. NSRange firstInstance = [itemDto.filePath rangeOfString:[NSString stringWithFormat:@"%@/files/%@", k_dav, tableAccount.userID]];
  507. NSString *serverPath = [itemDto.filePath substringFromIndex:firstInstance.length+firstInstance.location+1];
  508. if ([serverPath hasSuffix:@"/"]) serverPath = [serverPath substringToIndex:[serverPath length] - 1];
  509. serverUrl = [CCUtility stringAppendServerUrl:[tableAccount.url stringByAppendingString:k_webDAV] addFileName:serverPath];
  510. if (itemDto.isDirectory) {
  511. (void)[[NCManageDatabase sharedInstance] addDirectoryWithEncrypted:itemDto.isEncrypted favorite:itemDto.isFavorite fileID:itemDto.ocId permissions:itemDto.permissions serverUrl:[NSString stringWithFormat:@"%@/%@", serverUrl, fileName] account:account];
  512. }
  513. isFolderEncrypted = [CCUtility isFolderEncrypted:serverUrl account:account];
  514. [metadatas addObject:[CCUtility trasformedOCFileToCCMetadata:itemDto fileName:itemDto.fileName serverUrl:serverUrl autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:account isFolderEncrypted:isFolderEncrypted]];
  515. }
  516. dispatch_async(dispatch_get_main_queue(), ^{
  517. completion(account, metadatas, nil, 0);
  518. });
  519. });
  520. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  521. NSString *message = @"";
  522. NSInteger errorCode = response.statusCode;
  523. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  524. errorCode = error.code;
  525. // Error
  526. if (errorCode == 503) {
  527. message = NSLocalizedString(@"_server_error_retry_", nil);
  528. } else {
  529. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  530. }
  531. completion(account, nil, message, errorCode);
  532. }];
  533. }
  534. #pragma --------------------------------------------------------------------------------------------
  535. #pragma mark ===== downloadPreview =====
  536. #pragma --------------------------------------------------------------------------------------------
  537. /*
  538. - (void)downloadThumbnailWithMetadata:(tableMetadata*)metadata withWidth:(CGFloat)width andHeight:(CGFloat)height completion:(void (^)(NSString *message, NSInteger errorCode))completion
  539. {
  540. NSString *file = [NSString stringWithFormat:@"%@/%@.ico", [CCUtility getDirectoryProviderStorageFileID:metadata.fileID], metadata.fileNameView];
  541. if ([[NSFileManager defaultManager] fileExistsAtPath:file]) {
  542. completion(nil, 0);
  543. } else {
  544. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  545. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  546. [communication setUserAgent:[CCUtility getUserAgent]];
  547. [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) {
  548. [thumbnail writeToFile:file atomically:YES];
  549. completion(nil, 0);
  550. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  551. NSString *message;
  552. NSInteger errorCode = response.statusCode;
  553. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  554. errorCode = error.code;
  555. // Error
  556. if (errorCode == 503)
  557. message = NSLocalizedString(@"_server_error_retry_", nil);
  558. else
  559. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  560. completion(message, errorCode);
  561. }];
  562. }
  563. }
  564. */
  565. - (void)downloadPreviewWithAccount:(NSString *)account metadata:(tableMetadata*)metadata withWidth:(CGFloat)width andHeight:(CGFloat)height completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  566. {
  567. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  568. if (tableAccount == nil) {
  569. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  570. }
  571. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  572. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  573. [communication setUserAgent:[CCUtility getUserAgent]];
  574. NSString *file = [NSString stringWithFormat:@"%@/%@.ico", [CCUtility getDirectoryProviderStorageFileID:metadata.fileID], metadata.fileNameView];
  575. [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) {
  576. [preview writeToFile:file atomically:YES];
  577. completion(account, nil, 0);
  578. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  579. NSString *message;
  580. NSInteger errorCode = response.statusCode;
  581. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  582. errorCode = error.code;
  583. // Error
  584. if (errorCode == 503)
  585. message = NSLocalizedString(@"_server_error_retry_", nil);
  586. else
  587. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  588. completion(account, message, errorCode);
  589. }];
  590. }
  591. - (void)downloadPreviewTrashWithAccount:(NSString *)account FileID:(NSString *)fileID fileName:(NSString *)fileName completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  592. {
  593. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  594. if (tableAccount == nil) {
  595. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  596. }
  597. NSString *file = [NSString stringWithFormat:@"%@/%@.ico", [CCUtility getDirectoryProviderStorageFileID:fileID], fileName];
  598. if ([[NSFileManager defaultManager] fileExistsAtPath:file]) {
  599. completion(account, nil, 0);
  600. } else {
  601. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  602. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  603. [communication setUserAgent:[CCUtility getUserAgent]];
  604. [communication getRemotePreviewTrashByServer:tableAccount.url ofFileID:fileID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSData *preview, NSString *redirectedServer) {
  605. [preview writeToFile:file atomically:YES];
  606. completion(account, nil, 0);
  607. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  608. NSString *message;
  609. NSInteger errorCode = response.statusCode;
  610. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  611. errorCode = error.code;
  612. // Error
  613. if (errorCode == 503)
  614. message = NSLocalizedString(@"_server_error_retry_", nil);
  615. else
  616. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  617. completion(account, message, errorCode);
  618. }];
  619. }
  620. }
  621. #pragma --------------------------------------------------------------------------------------------
  622. #pragma mark ===== Favorite =====
  623. #pragma --------------------------------------------------------------------------------------------
  624. - (void)listingFavoritesWithAccount:(NSString *)account completion:(void(^)(NSString *account, NSArray *metadatas, NSString *message, NSInteger errorCode))completion
  625. {
  626. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  627. if (tableAccount == nil) {
  628. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  629. }
  630. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  631. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  632. [communication setUserAgent:[CCUtility getUserAgent]];
  633. NSString *path = [tableAccount.url stringByAppendingString:k_dav];
  634. [communication listingFavorites:path folder:@"" withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer, NSString *token) {
  635. NSMutableArray *metadatas = [NSMutableArray new];
  636. BOOL showHiddenFiles = [CCUtility getShowHiddenFiles];
  637. NSString *autoUploadFileName = [[NCManageDatabase sharedInstance] getAccountAutoUploadFileName];
  638. NSString *autoUploadDirectory = [[NCManageDatabase sharedInstance] getAccountAutoUploadDirectory:tableAccount.url];
  639. // Order by fileNamePath
  640. items = [items sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
  641. OCFileDto *record1 = obj1, *record2 = obj2;
  642. NSString *path1 = [[record1.filePath stringByAppendingString:record1.fileName] lowercaseString];
  643. NSString *path2 = [[record2.filePath stringByAppendingString:record2.fileName] lowercaseString];
  644. return [path1 compare:path2];
  645. }];
  646. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
  647. for(OCFileDto *itemDto in items) {
  648. NSString *serverUrl;
  649. BOOL isFolderEncrypted;
  650. NSString *fileName = [itemDto.fileName stringByReplacingOccurrencesOfString:@"/" withString:@""];
  651. // Skip hidden files
  652. if (fileName.length > 0) {
  653. if (!showHiddenFiles && [[fileName substringToIndex:1] isEqualToString:@"."])
  654. continue;
  655. } else
  656. continue;
  657. NSRange firstInstance = [itemDto.filePath rangeOfString:[NSString stringWithFormat:@"%@/files/%@", k_dav, tableAccount.userID]];
  658. NSString *serverPath = [itemDto.filePath substringFromIndex:firstInstance.length+firstInstance.location+1];
  659. if ([serverPath hasSuffix:@"/"])
  660. serverPath = [serverPath substringToIndex:[serverPath length] - 1];
  661. serverUrl = [CCUtility stringAppendServerUrl:[tableAccount.url stringByAppendingString:k_webDAV] addFileName:serverPath];
  662. if (itemDto.isDirectory) {
  663. (void)[[NCManageDatabase sharedInstance] addDirectoryWithEncrypted:itemDto.isEncrypted favorite:itemDto.isFavorite fileID:itemDto.ocId permissions:itemDto.permissions serverUrl:[NSString stringWithFormat:@"%@/%@", serverUrl, fileName] account:account];
  664. }
  665. isFolderEncrypted = [CCUtility isFolderEncrypted:serverUrl account:account];
  666. [metadatas addObject:[CCUtility trasformedOCFileToCCMetadata:itemDto fileName:itemDto.fileName serverUrl:serverUrl autoUploadFileName:autoUploadFileName autoUploadDirectory:autoUploadDirectory activeAccount:account isFolderEncrypted:isFolderEncrypted]];
  667. }
  668. dispatch_async(dispatch_get_main_queue(), ^{
  669. completion(account, metadatas, nil, 0);
  670. });
  671. });
  672. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  673. NSString *message;
  674. NSInteger errorCode = response.statusCode;
  675. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  676. errorCode = error.code;
  677. // Error
  678. if (errorCode == 503)
  679. message = NSLocalizedString(@"_server_error_retry_", nil);
  680. else
  681. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  682. completion(account, nil, message, errorCode);
  683. }];
  684. }
  685. - (void)settingFavoriteWithAccount:(NSString *)account fileName:(NSString *)fileName favorite:(BOOL)favorite completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  686. {
  687. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  688. if (tableAccount == nil) {
  689. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  690. }
  691. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  692. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  693. [communication setUserAgent:[CCUtility getUserAgent]];
  694. NSString *server = [tableAccount.url stringByAppendingString:k_dav];
  695. [communication settingFavoriteServer:server andFileOrFolderPath:fileName favorite:favorite withUserSessionToken:nil onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer, NSString *token) {
  696. completion(account, nil, 0);
  697. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *token, NSString *redirectedServer) {
  698. NSString *message;
  699. NSInteger errorCode = response.statusCode;
  700. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  701. errorCode = error.code;
  702. // Error
  703. if (errorCode == 503)
  704. message = NSLocalizedString(@"_server_error_retry_", nil);
  705. else
  706. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  707. completion(account, message, errorCode);
  708. }];
  709. }
  710. #pragma --------------------------------------------------------------------------------------------
  711. #pragma mark ===== Share =====
  712. #pragma --------------------------------------------------------------------------------------------
  713. - (void)readShareWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  714. {
  715. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  716. if (tableAccount == nil) {
  717. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  718. }
  719. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  720. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  721. [communication setUserAgent:[CCUtility getUserAgent]];
  722. [communication readSharedByServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  723. completion(account, items, nil, 0);
  724. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  725. NSString *message = @"";
  726. NSInteger errorCode = response.statusCode;
  727. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  728. errorCode = error.code;
  729. // Error
  730. if (errorCode == 503) {
  731. message = NSLocalizedString(@"_server_error_retry_", nil);
  732. } else {
  733. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  734. }
  735. completion(account, nil, message, errorCode);
  736. }];
  737. }
  738. /*
  739. - (void)readShareServer
  740. {
  741. #ifndef EXTENSION
  742. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  743. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  744. [communication setCredentialsWithUser:_activeUser andUserID:_activeUserID andPassword:_activePassword];
  745. [communication setUserAgent:[CCUtility getUserAgent]];
  746. [communication readSharedByServer:[_activeUrl stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  747. // Test active account
  748. tableAccount *recordAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  749. if (![recordAccount.account isEqualToString:_metadataNet.account]) {
  750. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)])
  751. [self.delegate shareFailure:_metadataNet message:NSLocalizedString(@"_error_user_not_available_", nil) errorCode:k_CCErrorUserNotAvailble];
  752. [self complete];
  753. return;
  754. }
  755. BOOL openWindow = NO;
  756. [appDelegate.sharesID removeAllObjects];
  757. if ([recordAccount.account isEqualToString:_metadataNet.account]) {
  758. for (OCSharedDto *item in items)
  759. [appDelegate.sharesID setObject:item forKey:[@(item.idRemoteShared) stringValue]];
  760. if ([_metadataNet.selector isEqual:selectorOpenWindowShare]) openWindow = YES;
  761. if ([_metadataNet.action isEqual:actionUpdateShare]) openWindow = YES;
  762. if ([_metadataNet.action isEqual:actionShare]) openWindow = YES;
  763. if ([_metadataNet.action isEqual:actionShareWith]) openWindow = YES;
  764. }
  765. if([self.delegate respondsToSelector:@selector(readSharedSuccess:items:openWindow:)])
  766. [self.delegate readSharedSuccess:_metadataNet items:appDelegate.sharesID openWindow:openWindow];
  767. [self complete];
  768. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  769. NSInteger errorCode = response.statusCode;
  770. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  771. errorCode = error.code;
  772. // Error
  773. if ([self.delegate respondsToSelector:@selector(shareFailure:message:errorCode:)]) {
  774. if (errorCode == 503)
  775. [self.delegate shareFailure:_metadataNet message:NSLocalizedString(@"_server_error_retry_", nil) errorCode:errorCode];
  776. else
  777. [self.delegate shareFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  778. }
  779. // Request trusted certificated
  780. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  781. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  782. [self complete];
  783. }];
  784. #endif
  785. }
  786. */
  787. - (void)shareWithAccount:(NSString *)account fileName:(NSString *)fileName password:(NSString *)password permission:(NSInteger)permission hideDownload:(BOOL)hideDownload completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  788. {
  789. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  790. if (tableAccount == nil) {
  791. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  792. }
  793. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  794. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  795. [communication setUserAgent:[CCUtility getUserAgent]];
  796. [communication shareFileOrFolderByServer:[tableAccount.url stringByAppendingString:@"/"] andFileOrFolderPath:[fileName encodeString:NSUTF8StringEncoding] andPassword:[password encodeString:NSUTF8StringEncoding] andPermission:permission andHideDownload:hideDownload onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *token, NSString *redirectedServer) {
  797. completion(account, nil, 0);
  798. //[self readShareServer];
  799. } failureRequest :^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  800. NSString *message = @"";
  801. NSInteger errorCode = response.statusCode;
  802. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  803. errorCode = error.code;
  804. // Error
  805. if (errorCode == 503) {
  806. message = NSLocalizedString(@"_server_error_retry_", nil);
  807. } else {
  808. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  809. }
  810. completion(account, message, errorCode);
  811. }];
  812. }
  813. // * @param shareeType -> NSInteger: to set the type of sharee (user/group/federated)
  814. - (void)shareUserGroupWithAccount:(NSString *)account userOrGroup:(NSString *)userOrGroup fileName:(NSString *)fileName permission:(NSInteger)permission shareeType:(NSInteger)shareeType completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  815. {
  816. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  817. if (tableAccount == nil) {
  818. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  819. }
  820. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  821. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  822. [communication setUserAgent:[CCUtility getUserAgent]];
  823. [communication shareWith:userOrGroup shareeType:shareeType inServer:[tableAccount.url stringByAppendingString:@"/"] andFileOrFolderPath:[fileName encodeString:NSUTF8StringEncoding] andPermissions:permission onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  824. //[self readShareServer];
  825. completion(account, nil, 0);
  826. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  827. NSString *message = @"";
  828. NSInteger errorCode = response.statusCode;
  829. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  830. errorCode = error.code;
  831. // Error
  832. if (errorCode == 503) {
  833. message = NSLocalizedString(@"_server_error_retry_", nil);
  834. } else {
  835. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  836. }
  837. completion(account, message, errorCode);
  838. }];
  839. }
  840. - (void)shareUpdateAccount:(NSString *)account shareID:(NSInteger)shareID password:(NSString *)password permission:(NSInteger)permission expirationTime:(NSString *)expirationTime hideDownload:(BOOL)hideDownload completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  841. {
  842. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  843. if (tableAccount == nil) {
  844. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  845. }
  846. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  847. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  848. [communication setUserAgent:[CCUtility getUserAgent]];
  849. [communication updateShare:shareID ofServerPath:[tableAccount.url stringByAppendingString:@"/"] withPasswordProtect:[password encodeString:NSUTF8StringEncoding] andExpirationTime:expirationTime andPermissions:permission andHideDownload:hideDownload onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  850. completion(account, nil, 0);
  851. //[self readShareServer];
  852. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  853. NSString *message = @"";
  854. NSInteger errorCode = response.statusCode;
  855. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  856. errorCode = error.code;
  857. // Error
  858. if (errorCode == 503) {
  859. message = NSLocalizedString(@"_server_error_retry_", nil);
  860. } else {
  861. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  862. }
  863. completion(account, message, errorCode);
  864. }];
  865. }
  866. - (void)unshareAccount:(NSString *)account shareID:(NSInteger)shareID completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  867. {
  868. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  869. if (tableAccount == nil) {
  870. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  871. }
  872. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  873. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  874. [communication setUserAgent:[CCUtility getUserAgent]];
  875. [communication unShareFileOrFolderByServer:[tableAccount.url stringByAppendingString:@"/"] andIdRemoteShared:shareID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  876. completion(account, nil, 0);
  877. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  878. NSString *message = @"";
  879. NSInteger errorCode = response.statusCode;
  880. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  881. errorCode = error.code;
  882. // Error
  883. if (errorCode == 503) {
  884. message = NSLocalizedString(@"_server_error_retry_", nil);
  885. } else {
  886. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  887. }
  888. completion(account, message, errorCode);
  889. }];
  890. }
  891. - (void)getUserGroupWithAccount:(NSString *)account searchString:(NSString *)searchString completion:(void (^)(NSString *account, NSArray *item, NSString *message, NSInteger errorCode))completion
  892. {
  893. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  894. if (tableAccount == nil) {
  895. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  896. }
  897. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  898. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  899. [communication setUserAgent:[CCUtility getUserAgent]];
  900. [communication searchUsersAndGroupsWith:searchString forPage:1 with:50 ofServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *itemList, NSString *redirectedServer) {
  901. completion(account, itemList, nil, 0);
  902. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  903. NSString *message = @"";
  904. NSInteger errorCode = response.statusCode;
  905. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  906. errorCode = error.code;
  907. // Error
  908. if (errorCode == 503) {
  909. message = NSLocalizedString(@"_server_error_retry_", nil);
  910. } else {
  911. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  912. }
  913. completion(account, nil, message, errorCode);
  914. }];
  915. }
  916. - (void)getSharePermissionsFileWithAccount:(NSString *)account fileNamePath:(NSString *)fileNamePath completion:(void (^)(NSString *account, NSString *permissions, NSString *message, NSInteger errorCode))completion
  917. {
  918. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  919. if (tableAccount == nil) {
  920. completion(account, 0, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  921. }
  922. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  923. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  924. [communication setUserAgent:[CCUtility getUserAgent]];
  925. [communication getSharePermissionsFile:fileNamePath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *permissions, NSString *redirectedServer) {
  926. completion(account, permissions, nil ,0);
  927. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  928. NSString *message = @"";
  929. NSInteger errorCode = response.statusCode;
  930. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  931. errorCode = error.code;
  932. // Error
  933. if (errorCode == 503) {
  934. message = NSLocalizedString(@"_server_error_retry_", nil);
  935. } else {
  936. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  937. }
  938. completion(account, nil, message, errorCode);
  939. }];
  940. }
  941. #pragma --------------------------------------------------------------------------------------------
  942. #pragma mark ===== API =====
  943. #pragma --------------------------------------------------------------------------------------------
  944. - (void)getActivityWithAccount:(NSString *)account completion:(void(^)(NSString *account, NSArray *listOfActivity, NSString *message, NSInteger errorCode))completion
  945. {
  946. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  947. if (tableAccount == nil) {
  948. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  949. }
  950. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  951. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  952. [communication setUserAgent:[CCUtility getUserAgent]];
  953. [communication getActivityServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfActivity, NSString *redirectedServer) {
  954. completion(account, listOfActivity, nil, 0);
  955. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  956. NSString *message;
  957. NSInteger errorCode = response.statusCode;
  958. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  959. errorCode = error.code;
  960. // Error
  961. if (errorCode == 503)
  962. message = NSLocalizedString(@"_server_error_retry_", nil);
  963. else
  964. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  965. completion(account, nil, message, errorCode);
  966. }];
  967. }
  968. - (void)getExternalSitesWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSArray *listOfExternalSites, NSString *message, NSInteger errorCode))completion
  969. {
  970. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  971. if (tableAccount == nil) {
  972. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  973. }
  974. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  975. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  976. [communication setUserAgent:[CCUtility getUserAgent]];
  977. [communication getExternalSitesServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfExternalSites, NSString *redirectedServer) {
  978. completion(account, listOfExternalSites, nil, 0);
  979. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  980. NSString *message = @"";
  981. NSInteger errorCode = response.statusCode;
  982. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  983. errorCode = error.code;
  984. // Error
  985. if (errorCode == 503) {
  986. message = NSLocalizedString(@"_server_error_retry_", nil);
  987. } else {
  988. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  989. }
  990. completion(account, nil, message, errorCode);
  991. }];
  992. }
  993. /*
  994. - (void)middlewarePing
  995. {
  996. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  997. [communication setCredentialsWithUser:_activeUser andPassword:_activePassword];
  998. [communication setUserAgent:[CCUtility getUserAgent]];
  999. [communication getMiddlewarePing:_metadataNet.serverUrl onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfExternalSites, NSString *redirectedServer) {
  1000. [self complete];
  1001. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1002. NSInteger errorCode = response.statusCode;
  1003. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1004. errorCode = error.code;
  1005. // Error
  1006. if ([self.delegate respondsToSelector:@selector(getExternalSitesServerFailure:message:errorCode:)]) {
  1007. if (errorCode == 503)
  1008. [self.delegate getExternalSitesServerFailure:_metadataNet message:NSLocalizedString(@"_server_error_retry_", nil) errorCode:errorCode];
  1009. else
  1010. [self.delegate getExternalSitesServerFailure:_metadataNet message:[error.userInfo valueForKey:@"NSLocalizedDescription"] errorCode:errorCode];
  1011. }
  1012. // Request trusted certificated
  1013. if ([error code] == NSURLErrorServerCertificateUntrusted && self.delegate)
  1014. [[CCCertificate sharedManager] presentViewControllerCertificateWithTitle:[error localizedDescription] viewController:(UIViewController *)self.delegate delegate:self];
  1015. [self complete];
  1016. }];
  1017. }
  1018. */
  1019. - (void)getNotificationWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSArray *listOfNotifications, NSString *message, NSInteger errorCode))completion
  1020. {
  1021. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1022. if (tableAccount == nil) {
  1023. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1024. }
  1025. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1026. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  1027. [communication setUserAgent:[CCUtility getUserAgent]];
  1028. [communication getNotificationServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfNotifications, NSString *redirectedServer) {
  1029. completion(account, listOfNotifications, nil, 0);
  1030. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1031. NSString *message = @"";
  1032. NSInteger errorCode = response.statusCode;
  1033. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1034. errorCode = error.code;
  1035. // Error
  1036. if (errorCode == 503) {
  1037. message = NSLocalizedString(@"_server_error_retry_", nil);
  1038. } else {
  1039. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1040. }
  1041. completion(account, nil, message, errorCode);
  1042. }];
  1043. }
  1044. - (void)setNotificationWithAccount:(NSString *)account serverUrl:(NSString *)serverUrl type:(NSString *)type completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  1045. {
  1046. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1047. if (tableAccount == nil) {
  1048. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1049. }
  1050. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1051. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  1052. [communication setUserAgent:[CCUtility getUserAgent]];
  1053. [communication setNotificationServer:serverUrl type:type onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1054. completion(account, nil, 0);
  1055. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1056. NSString *message = @"";
  1057. NSInteger errorCode = response.statusCode;
  1058. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1059. errorCode = error.code;
  1060. // Error
  1061. if (errorCode == 503) {
  1062. message = NSLocalizedString(@"_server_error_retry_", nil);
  1063. } else {
  1064. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1065. }
  1066. completion(account, message, errorCode);
  1067. }];
  1068. }
  1069. - (void)getCapabilitiesWithAccount:(NSString *)account completion:(void (^)(NSString *account, OCCapabilities *capabilities, NSString *message, NSInteger errorCode))completion
  1070. {
  1071. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1072. if (tableAccount == nil) {
  1073. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1074. }
  1075. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1076. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  1077. [communication setUserAgent:[CCUtility getUserAgent]];
  1078. [communication getCapabilitiesOfServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCCapabilities *capabilities, NSString *redirectedServer) {
  1079. completion(account, capabilities, nil, 0);
  1080. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1081. NSString *message = @"";
  1082. NSInteger errorCode = response.statusCode;
  1083. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1084. errorCode = error.code;
  1085. // Error
  1086. if (errorCode == 503) {
  1087. message = NSLocalizedString(@"_server_error_retry_", nil);
  1088. } else {
  1089. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1090. }
  1091. completion(account, nil, message, errorCode);
  1092. }];
  1093. }
  1094. - (void)getUserProfileWithAccount:(NSString *)account completion:(void (^)(NSString *account, OCUserProfile *userProfile, NSString *message, NSInteger errorCode))completion
  1095. {
  1096. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1097. if (tableAccount == nil) {
  1098. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1099. }
  1100. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1101. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  1102. [communication setUserAgent:[CCUtility getUserAgent]];
  1103. [communication getUserProfileServer:[tableAccount.url stringByAppendingString:@"/"] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, OCUserProfile *userProfile, NSString *redirectedServer) {
  1104. completion(account, userProfile, nil, 0);
  1105. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1106. NSString *message = @"";
  1107. NSInteger errorCode = response.statusCode;
  1108. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1109. errorCode = error.code;
  1110. // Error
  1111. if (errorCode == 503) {
  1112. message = NSLocalizedString(@"_server_error_retry_", nil);
  1113. } else {
  1114. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1115. }
  1116. completion(account, nil, message, errorCode);
  1117. }];
  1118. }
  1119. #pragma --------------------------------------------------------------------------------------------
  1120. #pragma mark ===== Push Notification =====
  1121. #pragma --------------------------------------------------------------------------------------------
  1122. - (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
  1123. {
  1124. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1125. if (tableAccount == nil) {
  1126. completion(account, nil, nil, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1127. }
  1128. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1129. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  1130. [communication setUserAgent:[CCUtility getUserAgent]];
  1131. devicePublicKey = [CCUtility URLEncodeStringFromString:devicePublicKey];
  1132. [communication subscribingNextcloudServerPush:url pushTokenHash:pushTokenHash devicePublicKey:devicePublicKey proxyServerPath: [NCBrandOptions sharedInstance].pushNotificationServerProxy onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *publicKey, NSString *deviceIdentifier, NSString *signature, NSString *redirectedServer) {
  1133. deviceIdentifier = [CCUtility URLEncodeStringFromString:deviceIdentifier];
  1134. signature = [CCUtility URLEncodeStringFromString:signature];
  1135. publicKey = [CCUtility URLEncodeStringFromString:publicKey];
  1136. [communication subscribingPushProxy:[NCBrandOptions sharedInstance].pushNotificationServerProxy pushToken:pushToken deviceIdentifier:deviceIdentifier deviceIdentifierSignature:signature publicKey:publicKey onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1137. // Activity
  1138. [[NCManageDatabase sharedInstance] addActivityClient:[NCBrandOptions sharedInstance].pushNotificationServerProxy fileID:@"" action:k_activityDebugActionSubscribingPushProxy selector:@"" note:@"Service registered." type:k_activityTypeSuccess verbose:k_activityVerboseHigh activeUrl:@""];
  1139. completion(account, deviceIdentifier, signature, publicKey, nil, 0);
  1140. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1141. NSString *message;
  1142. NSInteger errorCode = response.statusCode;
  1143. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1144. errorCode = error.code;
  1145. // Error
  1146. if (errorCode == 503)
  1147. message = NSLocalizedString(@"_server_error_retry_", nil);
  1148. else
  1149. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1150. // Activity
  1151. [[NCManageDatabase sharedInstance] addActivityClient:[NCBrandOptions sharedInstance].pushNotificationServerProxy fileID:@"" action:k_activityDebugActionSubscribingPushProxy selector:@"" note:[error.userInfo valueForKey:@"NSLocalizedDescription"] type:k_activityTypeFailure verbose:k_activityVerboseHigh activeUrl:@""];
  1152. completion(account, nil, nil, nil, message, errorCode);
  1153. }];
  1154. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1155. NSString *message;
  1156. NSInteger errorCode = response.statusCode;
  1157. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1158. errorCode = error.code;
  1159. // Error
  1160. if (errorCode == 503)
  1161. message = NSLocalizedString(@"_server_error_retry_", nil);
  1162. else
  1163. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1164. completion(account, nil, nil, nil, message, errorCode);
  1165. }];
  1166. }
  1167. - (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 {
  1168. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1169. if (tableAccount == nil) {
  1170. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1171. }
  1172. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1173. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  1174. [communication setUserAgent:[CCUtility getUserAgent]];
  1175. [communication unsubscribingNextcloudServerPush:url onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1176. [communication unsubscribingPushProxy:[NCBrandOptions sharedInstance].pushNotificationServerProxy deviceIdentifier:deviceIdentifier deviceIdentifierSignature:deviceIdentifierSignature publicKey:publicKey onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1177. completion(account, nil, 0);
  1178. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1179. NSString *message;
  1180. NSInteger errorCode = response.statusCode;
  1181. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1182. errorCode = error.code;
  1183. // Error
  1184. if (errorCode == 503)
  1185. message = NSLocalizedString(@"_server_error_retry_", nil);
  1186. else
  1187. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1188. completion(account, message, errorCode);
  1189. }];
  1190. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1191. NSString *message;
  1192. NSInteger errorCode = response.statusCode;
  1193. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1194. errorCode = error.code;
  1195. // Error
  1196. if (errorCode == 503)
  1197. message = NSLocalizedString(@"_server_error_retry_", nil);
  1198. else
  1199. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1200. completion(account, message, errorCode);
  1201. }];
  1202. }
  1203. #pragma --------------------------------------------------------------------------------------------
  1204. #pragma mark ===== Manage Mobile Editor OCS API =====
  1205. #pragma --------------------------------------------------------------------------------------------
  1206. - (void)createLinkRichdocumentsWithAccount:(NSString *)account fileID:(NSString *)fileID completion:(void(^)(NSString *account, NSString *link, NSString *message, NSInteger errorCode))completion
  1207. {
  1208. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1209. if (tableAccount == nil) {
  1210. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1211. }
  1212. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1213. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  1214. [communication setUserAgent:[CCUtility getUserAgent]];
  1215. NSString *fileIDServer = [[NCUtility sharedInstance] convertFileIDClientToFileIDServer:fileID];
  1216. [communication createLinkRichdocuments:[tableAccount.url stringByAppendingString:@"/"] fileID:fileIDServer onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *link, NSString *redirectedServer) {
  1217. completion(account, link, nil, 0);
  1218. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1219. NSString *message;
  1220. NSInteger errorCode = response.statusCode;
  1221. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1222. errorCode = error.code;
  1223. // Error
  1224. if (errorCode == 503)
  1225. message = NSLocalizedString(@"_server_error_retry_", nil);
  1226. else
  1227. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1228. completion(account, nil,message, errorCode);
  1229. }];
  1230. }
  1231. - (void)geTemplatesRichdocumentsWithAccount:(NSString *)account typeTemplate:(NSString *)typeTemplate completion:(void(^)(NSString *account, NSArray *listOfTemplate, NSString *message, NSInteger errorCode))completion
  1232. {
  1233. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1234. if (tableAccount == nil) {
  1235. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1236. }
  1237. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1238. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  1239. [communication setUserAgent:[CCUtility getUserAgent]];
  1240. [communication geTemplatesRichdocuments:[tableAccount.url stringByAppendingString:@"/"] typeTemplate:typeTemplate onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *listOfTemplate, NSString *redirectedServer) {
  1241. completion(account, listOfTemplate, nil, 0);
  1242. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1243. NSString *message;
  1244. NSInteger errorCode = response.statusCode;
  1245. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1246. errorCode = error.code;
  1247. // Error
  1248. if (errorCode == 503)
  1249. message = NSLocalizedString(@"_server_error_retry_", nil);
  1250. else
  1251. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1252. completion(account, nil, message, errorCode);
  1253. }];
  1254. }
  1255. - (void)createNewRichdocumentsWithAccount:(NSString *)account fileName:(NSString *)fileName serverUrl:(NSString *)serverUrl templateID:(NSString *)templateID completion:(void(^)(NSString *account, NSString *url, NSString *message, NSInteger errorCode))completion
  1256. {
  1257. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1258. if (tableAccount == nil) {
  1259. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1260. }
  1261. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1262. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  1263. [communication setUserAgent:[CCUtility getUserAgent]];
  1264. [communication createNewRichdocuments:[tableAccount.url stringByAppendingString:@"/"] path:fileName templateID:templateID onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *url, NSString *redirectedServer) {
  1265. completion(account, url, nil, 0);
  1266. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1267. NSString *message;
  1268. NSInteger errorCode = response.statusCode;
  1269. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1270. errorCode = error.code;
  1271. // Error
  1272. if (errorCode == 503)
  1273. message = NSLocalizedString(@"_server_error_retry_", nil);
  1274. else
  1275. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1276. completion(account, nil, message, errorCode);
  1277. }];
  1278. }
  1279. - (void)createAssetRichdocumentsWithAccount:(NSString *)account fileName:(NSString *)fileName serverUrl:(NSString *)serverUrl completion:(void(^)(NSString *account, NSString *link, NSString *message, NSInteger errorCode))completion
  1280. {
  1281. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1282. if (tableAccount == nil) {
  1283. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1284. }
  1285. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1286. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  1287. [communication setUserAgent:[CCUtility getUserAgent]];
  1288. NSString *fileNamePath = [CCUtility returnFileNamePathFromFileName:fileName serverUrl:serverUrl activeUrl:tableAccount.url];
  1289. [communication createAssetRichdocuments:[tableAccount.url stringByAppendingString:@"/"] path:fileNamePath onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *url, NSString *redirectedServer) {
  1290. completion(account, url, nil, 0);
  1291. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1292. NSString *message;
  1293. NSInteger errorCode = response.statusCode;
  1294. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1295. errorCode = error.code;
  1296. // Error
  1297. if (errorCode == 503)
  1298. message = NSLocalizedString(@"_server_error_retry_", nil);
  1299. else
  1300. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1301. completion(account, nil, message, errorCode);
  1302. }];
  1303. }
  1304. #pragma --------------------------------------------------------------------------------------------
  1305. #pragma mark ===== Trash =====
  1306. #pragma --------------------------------------------------------------------------------------------
  1307. - (void)listingTrashWithAccount:(NSString *)account path:(NSString *)path serverUrl:(NSString *)serverUrl completion:(void (^)(NSString *account, NSArray *items, NSString *message, NSInteger errorCode))completion
  1308. {
  1309. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1310. if (tableAccount == nil) {
  1311. completion(account, nil, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1312. }
  1313. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1314. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  1315. [communication setUserAgent:[CCUtility getUserAgent]];
  1316. [communication listingTrash:[serverUrl stringByAppendingString:path] onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSArray *items, NSString *redirectedServer) {
  1317. // Check items > 0
  1318. if ([items count] == 0) {
  1319. #ifndef EXTENSION
  1320. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  1321. [appDelegate messageNotification:@"Server error" description:@"Read Folder WebDAV : [items NULL] please fix" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:k_CCErrorInternalError];
  1322. #endif
  1323. completion(account, nil, NSLocalizedString(@"Read Folder WebDAV : [items NULL] please fix", nil), k_CCErrorInternalError);
  1324. } else {
  1325. NSMutableArray *listTrash = [NSMutableArray new];
  1326. //OCFileDto *itemDtoFolder = [items objectAtIndex:0];
  1327. if ([items count] > 1) {
  1328. for (NSUInteger i=1; i < [items count]; i++) {
  1329. OCFileDto *itemDto = [items objectAtIndex:i];
  1330. tableTrash *trash = [tableTrash new];
  1331. trash.account = account;
  1332. trash.date = [NSDate dateWithTimeIntervalSince1970:itemDto.date];
  1333. trash.directory = itemDto.isDirectory;
  1334. trash.fileID = itemDto.ocId;
  1335. trash.fileName = itemDto.fileName;
  1336. trash.filePath = itemDto.filePath;
  1337. trash.size = itemDto.size;
  1338. trash.trashbinFileName = itemDto.trashbinFileName;
  1339. trash.trashbinOriginalLocation = itemDto.trashbinOriginalLocation;
  1340. trash.trashbinDeletionTime = [NSDate dateWithTimeIntervalSince1970:itemDto.trashbinDeletionTime];
  1341. [CCUtility insertTypeFileIconName:trash.trashbinFileName metadata:(tableMetadata *)trash];
  1342. [listTrash addObject:trash];
  1343. }
  1344. }
  1345. completion(account, listTrash, nil, 0);
  1346. }
  1347. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1348. NSString *message;
  1349. NSInteger errorCode = response.statusCode;
  1350. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1351. errorCode = error.code;
  1352. // Error
  1353. if (errorCode == 503)
  1354. message = NSLocalizedString(@"_server_error_retry_", nil);
  1355. else
  1356. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1357. completion(account, nil,message, errorCode);
  1358. }];
  1359. }
  1360. - (void)emptyTrashWithAccount:(NSString *)account completion:(void (^)(NSString *account, NSString *message, NSInteger errorCode))completion
  1361. {
  1362. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", account]];
  1363. if (tableAccount == nil) {
  1364. completion(account, NSLocalizedString(@"_error_user_not_available_", nil), k_CCErrorUserNotAvailble);
  1365. }
  1366. OCCommunication *communication = [CCNetworking sharedNetworking].sharedOCCommunication;
  1367. [communication setCredentialsWithUser:tableAccount.user andUserID:tableAccount.userID andPassword:tableAccount.password];
  1368. [communication setUserAgent:[CCUtility getUserAgent]];
  1369. NSString *path = [NSString stringWithFormat:@"%@%@/trashbin/%@/trash", tableAccount.url, k_dav, tableAccount.userID];
  1370. [communication emptyTrash:path onCommunication:communication successRequest:^(NSHTTPURLResponse *response, NSString *redirectedServer) {
  1371. completion(account, nil, 0);
  1372. } failureRequest:^(NSHTTPURLResponse *response, NSError *error, NSString *redirectedServer) {
  1373. NSString *message;
  1374. NSInteger errorCode = response.statusCode;
  1375. if (errorCode == 0 || (errorCode >= 200 && errorCode < 300))
  1376. errorCode = error.code;
  1377. // Error
  1378. if (errorCode == 503)
  1379. message = NSLocalizedString(@"_server_error_retry_", nil);
  1380. else
  1381. message = [error.userInfo valueForKey:@"NSLocalizedDescription"];
  1382. completion(account, message, errorCode);
  1383. }];
  1384. }
  1385. @end
  1386. #pragma --------------------------------------------------------------------------------------------
  1387. #pragma mark ===== OCURLSessionManager =====
  1388. #pragma --------------------------------------------------------------------------------------------
  1389. @implementation OCURLSessionManager
  1390. - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
  1391. {
  1392. // The pinnning check
  1393. if ([[CCCertificate sharedManager] checkTrustedChallenge:challenge]) {
  1394. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  1395. } else {
  1396. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  1397. }
  1398. }
  1399. @end