OCNetworking.m 110 KB

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