OCWebDAVClient.m 88 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563
  1. //
  2. // OCWebDAVClient.m
  3. // OCWebDAVClient
  4. //
  5. // This class is based in https://github.com/zwaldowski/DZWebDAVClient. Copyright (c) 2012 Zachary Waldowski, Troy Brant, Marcus Rohrmoser, and Sam Soffes.
  6. //
  7. // Copyright (C) 2016, ownCloud GmbH. ( http://www.owncloud.org/ )
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. // The above copyright notice and this permission notice shall be included in
  16. // all copies or substantial portions of the Software.
  17. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. // THE SOFTWARE.
  24. //
  25. //
  26. // Add : getNotificationServer & setNotificationServer
  27. // Add : getUserProfileServer
  28. // Add : Support for Favorite
  29. // Add : getActivityServer
  30. //
  31. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  32. // Copyright (c) 2017 Marino Faggiana. All rights reserved.
  33. //
  34. #import "OCWebDAVClient.h"
  35. #import "OCFrameworkConstants.h"
  36. #import "OCCommunication.h"
  37. #import "UtilsFramework.h"
  38. #import "NSString+Encode.h"
  39. #import "OCConstants.h"
  40. #define k_api_user_url_xml @"index.php/ocs/cloud/user"
  41. #define k_api_user_url_json @"index.php/ocs/cloud/user?format=json"
  42. #define k_server_information_json @"status.php"
  43. #define k_api_header_request @"OCS-APIREQUEST"
  44. #define k_group_sharee_type 1
  45. NSString const *OCWebDAVContentTypeKey = @"getcontenttype";
  46. NSString const *OCWebDAVETagKey = @"getetag";
  47. NSString const *OCWebDAVCTagKey = @"getctag";
  48. NSString const *OCWebDAVCreationDateKey = @"creationdate";
  49. NSString const *OCWebDAVModificationDateKey = @"modificationdate";
  50. @interface OCWebDAVClient()
  51. - (void)mr_listPath:(NSString *)path depth:(NSString *)depth onCommunication:
  52. (OCCommunication *)sharedOCCommunication
  53. success:(void(^)(NSHTTPURLResponse *, id))success
  54. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure;
  55. @end
  56. @implementation OCWebDAVClient
  57. - (id) init {
  58. self = [super init];
  59. if (self != nil) {
  60. self.defaultHeaders = [NSMutableDictionary new];
  61. }
  62. return self;
  63. }
  64. - (void)setAuthorizationHeaderWithUsername:(NSString *)username password:(NSString *)password {
  65. NSString *basicAuthCredentials = [NSString stringWithFormat:@"%@:%@", username, password];
  66. [self.defaultHeaders setObject:[NSString stringWithFormat:@"Basic %@", [UtilsFramework AFBase64EncodedStringFromString: basicAuthCredentials]] forKey:@"Authorization"];
  67. }
  68. - (void)setAuthorizationHeaderWithCookie:(NSString *) cookieString {
  69. [self.defaultHeaders setObject:cookieString forKey:@"Cookie"];
  70. }
  71. - (void)setAuthorizationHeaderWithToken:(NSString *)token {
  72. [self.defaultHeaders setObject:token forKey:@"Authorization"];
  73. }
  74. - (void)setDefaultHeader:(NSString *)header value:(NSString *)value {
  75. [self.defaultHeaders setObject:value forKey:header];
  76. }
  77. - (void)setUserAgent:(NSString *)userAgent{
  78. [self.defaultHeaders setObject:userAgent forKey:@"User-Agent"];
  79. }
  80. - (OCHTTPRequestOperation *)mr_operationWithRequest:(NSMutableURLRequest *)request onCommunication:(OCCommunication *)sharedOCCommunication withUserSessionToken:(NSString*)token success:(void(^)(NSHTTPURLResponse *operation, id response, NSString *token))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error, NSString *token))failure {
  81. //If is not nil is a redirection so we keep the original url server
  82. if (!self.originalUrlServer) {
  83. self.originalUrlServer = [request.URL absoluteString];
  84. }
  85. if (sharedOCCommunication.isCookiesAvailable) {
  86. //We add the cookies of that URL
  87. request = [UtilsFramework getRequestWithCookiesByRequest:request andOriginalUrlServer:self.originalUrlServer];
  88. } else {
  89. [UtilsFramework deleteAllCookies];
  90. }
  91. OCHTTPRequestOperation *operation = (OCHTTPRequestOperation*) [sharedOCCommunication.networkSessionManager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
  92. if (!error) {
  93. success((NSHTTPURLResponse*)response,responseObject, token);
  94. } else {
  95. failure((NSHTTPURLResponse*)response, responseObject, error, token);
  96. }
  97. }];
  98. return operation;
  99. }
  100. - (OCHTTPRequestOperation *)mr_operationWithRequest:(NSMutableURLRequest *)request onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *, id))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  101. //If is not nil is a redirection so we keep the original url server
  102. if (!self.originalUrlServer) {
  103. self.originalUrlServer = [request.URL absoluteString];
  104. }
  105. if (sharedOCCommunication.isCookiesAvailable) {
  106. //We add the cookies of that URL
  107. request = [UtilsFramework getRequestWithCookiesByRequest:request andOriginalUrlServer:self.originalUrlServer];
  108. } else {
  109. [UtilsFramework deleteAllCookies];
  110. }
  111. OCHTTPRequestOperation *operation = (OCHTTPRequestOperation*) [sharedOCCommunication.networkSessionManager dataTaskWithRequest:request completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
  112. if (!error) {
  113. success((NSHTTPURLResponse*)response,responseObject);
  114. } else {
  115. failure((NSHTTPURLResponse*)response, responseObject, error);
  116. }
  117. }];
  118. return operation;
  119. }
  120. - (NSMutableURLRequest *)requestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters timeout:(NSTimeInterval)timeout {
  121. NSMutableURLRequest *request = [[AFHTTPRequestSerializer new] requestWithMethod:method URLString:path parameters:nil error:nil];
  122. [request setAllHTTPHeaderFields:self.defaultHeaders];
  123. [request setCachePolicy: NSURLRequestReloadIgnoringLocalCacheData];
  124. [request setTimeoutInterval: timeout];
  125. return request;
  126. }
  127. - (NSMutableURLRequest *)sharedRequestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters timeout:(NSTimeInterval)timeout {
  128. NSMutableURLRequest *request = [[AFHTTPRequestSerializer new] requestWithMethod:method URLString:path parameters:nil error:nil];
  129. [request setAllHTTPHeaderFields:self.defaultHeaders];
  130. //NSMutableURLRequest *request = [super requestWithMethod:method path:path parameters:parameters];
  131. [request setCachePolicy: NSURLRequestReloadIgnoringLocalCacheData];
  132. [request setTimeoutInterval: k_timeout_webdav];
  133. //Header for use the OC API CALL
  134. NSString *ocs_apiquests = @"true";
  135. [request setValue:ocs_apiquests forHTTPHeaderField:k_api_header_request];
  136. [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  137. [request addValue:@"gzip, deflate" forHTTPHeaderField:@"Accept-Encoding"];
  138. return request;
  139. }
  140. - (void)movePath:(NSString *)source toPath:(NSString *)destination
  141. onCommunication:(OCCommunication *)sharedOCCommunication
  142. success:(void(^)(NSHTTPURLResponse *, id))success
  143. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  144. _requestMethod = @"MOVE";
  145. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:source parameters:nil timeout:k_timeout_webdav];
  146. [request setValue:destination forHTTPHeaderField:@"Destination"];
  147. [request setValue:@"T" forHTTPHeaderField:@"Overwrite"];
  148. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  149. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  150. [operation resume];
  151. }
  152. - (void)deletePath:(NSString *)path
  153. onCommunication:(OCCommunication *)sharedOCCommunication
  154. success:(void(^)(NSHTTPURLResponse *, id))success
  155. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  156. _requestMethod = @"DELETE";
  157. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_webdav];
  158. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  159. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  160. [operation resume];
  161. }
  162. - (void)mr_listPath:(NSString *)path depth:(NSString *)depth onCommunication:
  163. (OCCommunication *)sharedOCCommunication
  164. success:(void(^)(NSHTTPURLResponse *, id))success
  165. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  166. NSParameterAssert(success);
  167. _requestMethod = @"PROPFIND";
  168. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_webdav];
  169. [request setValue: depth forHTTPHeaderField: @"Depth"];
  170. [request setHTTPBody:[@"<?xml version=\"1.0\" encoding=\"UTF-8\"?><D:propfind xmlns:D=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">"
  171. "<D:prop>"
  172. "<D:resourcetype/>"
  173. "<D:getlastmodified/>"
  174. "<size xmlns=\"http://owncloud.org/ns\"/>"
  175. "<favorite xmlns=\"http://owncloud.org/ns\"/>"
  176. "<id xmlns=\"http://owncloud.org/ns\"/>"
  177. "<D:getcontentlength/>"
  178. "<D:getetag/>"
  179. "<oc:fileid/>"
  180. "<permissions xmlns=\"http://owncloud.org/ns\"/>"
  181. "<D:getcontenttype/>"
  182. "<nc:is-encrypted/>"
  183. "<nc:has-preview/>"
  184. "</D:prop>"
  185. "</D:propfind>" dataUsingEncoding:NSUTF8StringEncoding]];
  186. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  187. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  188. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  189. [operation resume];
  190. }
  191. - (void)mr_listPath:(NSString *)path depth:(NSString *)depth withUserSessionToken:(NSString*)token onCommunication:
  192. (OCCommunication *)sharedOCCommunication
  193. success:(void(^)(NSHTTPURLResponse *operation, id response, NSString *token))success
  194. failure:(void(^)(NSHTTPURLResponse *response, id _Nullable responseObject, NSError *, NSString *token))failure {
  195. NSParameterAssert(success);
  196. _requestMethod = @"PROPFIND";
  197. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_webdav];
  198. [request setValue: depth forHTTPHeaderField: @"Depth"];
  199. [request setHTTPBody:[@"<?xml version=\"1.0\" encoding=\"UTF-8\"?><D:propfind xmlns:D=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">"
  200. "<D:prop>"
  201. "<D:resourcetype/>"
  202. "<D:getlastmodified/>"
  203. "<size xmlns=\"http://owncloud.org/ns\"/>"
  204. "<favorite xmlns=\"http://owncloud.org/ns\"/>"
  205. "<id xmlns=\"http://owncloud.org/ns\"/>"
  206. "<D:getcontentlength/>"
  207. "<D:getetag/>"
  208. "<oc:fileid/>"
  209. "<permissions xmlns=\"http://owncloud.org/ns\"/>"
  210. "<D:getcontenttype/>"
  211. "<nc:is-encrypted/>"
  212. "<nc:has-preview/>"
  213. "</D:prop>"
  214. "</D:propfind>" dataUsingEncoding:NSUTF8StringEncoding]];
  215. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  216. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  217. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  218. [operation resume];
  219. }
  220. - (void)propertiesOfPath:(NSString *)path
  221. onCommunication: (OCCommunication *)sharedOCCommunication
  222. success:(void(^)(NSHTTPURLResponse *, id ))success
  223. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  224. [self mr_listPath:path depth:@"0" onCommunication:sharedOCCommunication success:success failure:failure];
  225. }
  226. - (void)listPath:(NSString *)path depth:(NSString *)depth
  227. onCommunication:(OCCommunication *)sharedOCCommunication
  228. success:(void(^)(NSHTTPURLResponse *, id))success
  229. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  230. [self mr_listPath:path depth:depth onCommunication:sharedOCCommunication success:success failure:failure];
  231. }
  232. - (void)listPath:(NSString *)path depth:(NSString *)depth
  233. onCommunication:(OCCommunication *)sharedOCCommunication withUserSessionToken:(NSString *)token
  234. success:(void(^)(NSHTTPURLResponse *, id, NSString *token))success
  235. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *, NSString *token))failure {
  236. [self mr_listPath:path depth:depth withUserSessionToken:token onCommunication:sharedOCCommunication success:success failure:failure];
  237. }
  238. - (void)search:(NSString *)path folder:(NSString *)folder fileName:(NSString *)fileName depth:(NSString *)depth lteDateLastModified:(NSString *)lteDateLastModified gteDateLastModified:(NSString *)gteDateLastModified contentType:(NSArray *)contentType user:(NSString *)user userID:(NSString *)userID onCommunication:(OCCommunication *)sharedOCCommunication withUserSessionToken:(NSString *)token success:(void(^)(NSHTTPURLResponse *, id, NSString *token))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *, NSString *token))failure {
  239. NSString *body = @"";
  240. NSString *whereType = @"";
  241. NSString *whereDate = @"";
  242. NSParameterAssert(success);
  243. _requestMethod = @"SEARCH";
  244. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_search];
  245. if (contentType && lteDateLastModified && gteDateLastModified) {
  246. body = [NSString stringWithFormat: @""
  247. "<?xml version=\"1.0\"?>"
  248. "<d:searchrequest xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">"
  249. "<d:basicsearch>"
  250. "<d:select>"
  251. "<d:prop>"
  252. "<d:getlastmodified />"
  253. "<d:getetag />"
  254. "<d:getcontenttype />"
  255. "<d:resourcetype/>"
  256. "<d:getcontentlength />"
  257. "<oc:fileid/>"
  258. "<oc:id/>"
  259. "<oc:permissions />"
  260. "<oc:size />"
  261. "<oc:favorite/>"
  262. "<nc:is-encrypted/>"
  263. "<nc:has-preview/>"
  264. "</d:prop>"
  265. "</d:select>"
  266. "<d:from>"
  267. "<d:scope>"
  268. "<d:href>/files/%@%@</d:href>"
  269. "<d:depth>infinity</d:depth>"
  270. "</d:scope>"
  271. "</d:from>"
  272. "<d:orderby>"
  273. "<d:order>"
  274. "<d:prop><d:getlastmodified/></d:prop>"
  275. "<d:descending/>"
  276. "</d:order>"
  277. "<d:order>"
  278. "<d:prop><d:displayname/></d:prop>"
  279. "<d:descending/>"
  280. "</d:order>"
  281. "</d:orderby>"
  282. "<d:where><d:and><d:or>", userID, folder];
  283. for (NSString *type in contentType) {
  284. whereType = [NSString stringWithFormat: @"%@<d:like><d:prop><d:getcontenttype/></d:prop><d:literal>%@</d:literal></d:like>", whereType, type];
  285. }
  286. body = [NSString stringWithFormat: @"%@%@</d:or><d:or>", body, whereType];
  287. whereDate = [NSString stringWithFormat: @"%@<d:and><d:lte><d:prop><d:getlastmodified/></d:prop><d:literal>%@</d:literal></d:lte><d:gte><d:prop><d:getlastmodified/></d:prop><d:literal>%@</d:literal></d:gte></d:and>", whereDate, lteDateLastModified, gteDateLastModified];
  288. /*
  289. if (gteDateLastModified != nil && lteDateLastModified == nil) {
  290. whereDate = [NSString stringWithFormat: @"%@<d:gte><d:prop><d:getlastmodified/></d:prop><d:literal>%@</d:literal></d:gte>", whereDate, gteDateLastModified];
  291. } else if (gteDateLastModified != nil && lteDateLastModified != nil) {
  292. whereDate = [NSString stringWithFormat: @"%@<d:and><d:lte><d:prop><d:getlastmodified/></d:prop><d:literal>%@</d:literal></d:lte><d:gte><d:prop><d:getlastmodified/></d:prop><d:literal>%@</d:literal></d:gte></d:and>", whereDate, lteDateLastModified, gteDateLastModified];
  293. }
  294. */
  295. body = [NSString stringWithFormat: @"%@%@</d:or></d:and></d:where></d:basicsearch></d:searchrequest>", body, whereDate];
  296. } else {
  297. body = [NSString stringWithFormat: @""
  298. "<?xml version=\"1.0\"?>"
  299. "<d:searchrequest xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">"
  300. "<d:basicsearch>"
  301. "<d:select>"
  302. "<d:prop>"
  303. "<d:getlastmodified/>"
  304. "<d:getetag/>"
  305. "<d:getcontenttype/>"
  306. "<d:resourcetype/>"
  307. "<d:getcontentlength/>"
  308. "<oc:fileid/>"
  309. "<oc:id/>"
  310. "<oc:permissions/>"
  311. "<oc:size/>"
  312. "<oc:favorite/>"
  313. "<nc:is-encrypted/>"
  314. "<nc:has-preview/>"
  315. "</d:prop>"
  316. "</d:select>"
  317. "<d:from>"
  318. "<d:scope>"
  319. "<d:href>/files/%@%@</d:href>"
  320. "<d:depth>infinity</d:depth>"
  321. "</d:scope>"
  322. "</d:from>"
  323. "<d:where>"
  324. "<d:like>"
  325. "<d:prop><d:displayname/></d:prop>"
  326. "<d:literal>%@</d:literal>"
  327. "</d:like>"
  328. "</d:where>"
  329. "</d:basicsearch>"
  330. "</d:searchrequest>", userID, folder, fileName];
  331. }
  332. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  333. [request setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
  334. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  335. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  336. [operation resume];
  337. }
  338. - (void)search:(NSString *)path folderStartWith:(NSString *)folderStartWith dateLastModified:(NSString *)dateLastModified numberOfItem:(NSInteger)numberOfItem userID:(NSString *)userID onCommunication:(OCCommunication *)sharedOCCommunication withUserSessionToken:(NSString *)token success:(void(^)(NSHTTPURLResponse *, id, NSString *token))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *, NSString *token))failure {
  339. NSString *body = @"";
  340. NSParameterAssert(success);
  341. _requestMethod = @"SEARCH";
  342. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_search];
  343. body = [NSString stringWithFormat: @""
  344. "<?xml version=\"1.0\"?>"
  345. "<d:searchrequest xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">"
  346. "<d:basicsearch>"
  347. "<d:select>"
  348. "<d:prop>"
  349. "<d:getlastmodified />"
  350. "<d:getetag />"
  351. "<d:getcontenttype />"
  352. "<d:resourcetype/>"
  353. "<d:getcontentlength />"
  354. "<oc:fileid/>"
  355. "<oc:id/>"
  356. "<oc:permissions />"
  357. "<oc:size />"
  358. "<oc:favorite/>"
  359. "<nc:is-encrypted/>"
  360. "<nc:has-preview/>"
  361. "</d:prop>"
  362. "</d:select>"
  363. "<d:from>"
  364. "<d:scope>"
  365. "<d:href>/files/%@%@</d:href>"
  366. "<d:depth>1</d:depth>"
  367. "</d:scope>"
  368. "</d:from>"
  369. "<d:orderby>"
  370. "<d:order>"
  371. "<d:prop><d:displayname/></d:prop>"
  372. "<d:descending/>"
  373. "</d:order>"
  374. "<d:order>"
  375. "<d:prop><d:getlastmodified/></d:prop>"
  376. "<d:descending/>"
  377. "</d:order>"
  378. "</d:orderby>"
  379. "<d:where><d:and><d:or>"
  380. "<d:gte>"
  381. "<d:prop><d:getlastmodified/></d:prop>"
  382. "<d:literal>%@</d:literal>"
  383. "</d:gte>"
  384. "</d:or></d:and></d:where>"
  385. "<d:limit>"
  386. "<d:nresults>%@</d:nresults>"
  387. "</d:limit>"
  388. "</d:basicsearch>"
  389. "</d:searchrequest>"
  390. ,userID, folderStartWith, dateLastModified, [@(numberOfItem) stringValue]];
  391. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  392. [request setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
  393. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  394. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  395. [operation resume];
  396. }
  397. - (void)settingFavorite:(NSString * _Nonnull)path favorite:(BOOL)favorite onCommunication:(OCCommunication *)sharedOCCommunication withUserSessionToken:(NSString *)token success:(void(^)(NSHTTPURLResponse *, id, NSString *token))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *, NSString *token))failure {
  398. NSParameterAssert(success);
  399. _requestMethod = @"PROPPATCH";
  400. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_webdav];
  401. NSString *body;
  402. body = [NSString stringWithFormat: @""
  403. "<?xml version=\"1.0\"?>"
  404. "<d:propertyupdate xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">"
  405. "<d:set>"
  406. "<d:prop>"
  407. "<oc:favorite>%i</oc:favorite>"
  408. "</d:prop>"
  409. "</d:set>"
  410. "</d:propertyupdate>", (favorite ? 1 : 0)];
  411. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  412. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  413. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  414. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  415. [operation resume];
  416. }
  417. - (void)listingFavorites:(NSString *)path folder:(NSString *)folder user:(NSString *)user userID:(NSString *)userID onCommunication:(OCCommunication *)sharedOCCommunication withUserSessionToken:(NSString *)token success:(void(^)(NSHTTPURLResponse *, id, NSString *token))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *, NSString *token))failure {
  418. NSString *body;
  419. NSParameterAssert(success);
  420. _requestMethod = @"REPORT";
  421. userID = [userID stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
  422. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:[NSString stringWithFormat:@"%@/files/%@%@", path, userID, folder] parameters:nil timeout:k_timeout_webdav];
  423. body = [NSString stringWithFormat: @""
  424. "<?xml version=\"1.0\"?>"
  425. "<oc:filter-files xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">"
  426. "<d:prop>"
  427. "<d:resourcetype/>"
  428. "<oc:fileid/>"
  429. "<d:getetag/>"
  430. "<d:getcontentlength/>"
  431. "<oc:size/>"
  432. "<d:getlastmodified/>"
  433. "<oc:id/>"
  434. "<oc:permissions/>"
  435. "<oc:favorite/>"
  436. "<nc:is-encrypted/>"
  437. "<nc:has-preview/>"
  438. "</d:prop>"
  439. "<oc:filter-rules>"
  440. "<oc:favorite>1</oc:favorite>"
  441. "</oc:filter-rules>"
  442. "</oc:filter-files>"];
  443. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  444. [request setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
  445. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  446. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  447. [operation resume];
  448. }
  449. - (NSURLSessionDownloadTask *)downloadWithSessionPath:(NSString *)remoteSource toPath:(NSString *)localDestination defaultPriority:(BOOL)defaultPriority onCommunication:(OCCommunication *)sharedOCCommunication progress:(void(^)(NSProgress *progress))downloadProgress success:(void(^)(NSURLResponse *response, NSURL *filePath))success failure:(void(^)(NSURLResponse *response, NSError *error))failure{
  450. NSMutableURLRequest *request = [self requestWithMethod:@"GET" path:remoteSource parameters:nil timeout:k_timeout_webdav];
  451. //If is not nil is a redirection so we keep the original url server
  452. if (!self.originalUrlServer) {
  453. self.originalUrlServer = [request.URL absoluteString];
  454. }
  455. //We add the cookies of that URL
  456. request = [UtilsFramework getRequestWithCookiesByRequest:request andOriginalUrlServer:self.originalUrlServer];
  457. NSURLSessionDownloadTask *downloadTask = [sharedOCCommunication.downloadSessionManager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull progress) {
  458. downloadProgress(progress);
  459. } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
  460. [[NSFileManager defaultManager] removeItemAtURL:[NSURL fileURLWithPath:localDestination] error:nil];
  461. return [NSURL fileURLWithPath:localDestination];
  462. } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
  463. if (error) {
  464. failure(response, error);
  465. } else {
  466. success(response,filePath);
  467. }
  468. }];
  469. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.downloadSessionManager];
  470. if (defaultPriority) {
  471. [downloadTask resume];
  472. }
  473. return downloadTask;
  474. }
  475. - (void)checkServer:(NSString *)path onCommunication:
  476. (OCCommunication *)sharedOCCommunication
  477. success:(void(^)(NSHTTPURLResponse *, id))success
  478. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  479. _requestMethod = @"HEAD";
  480. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_webdav];
  481. request.HTTPShouldHandleCookies = false;
  482. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  483. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  484. [operation resume];
  485. }
  486. - (void)makeCollection:(NSString *)path onCommunication:
  487. (OCCommunication *)sharedOCCommunication
  488. success:(void(^)(NSHTTPURLResponse *, id))success
  489. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  490. _requestMethod = @"MKCOL";
  491. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_webdav];
  492. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  493. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  494. [operation resume];
  495. }
  496. - (NSURLSessionUploadTask *)putWithSessionLocalPath:(NSString *)localSource atRemotePath:(NSString *)remoteDestination onCommunication:(OCCommunication *)sharedOCCommunication progress:(void(^)(NSProgress *progress))uploadProgress success:(void(^)(NSURLResponse *, NSString *))success failure:(void(^)(NSURLResponse *, id, NSError *))failure failureBeforeRequest:(void(^)(NSError *)) failureBeforeRequest {
  497. NSFileManager *fileManager = [NSFileManager defaultManager];
  498. if (localSource == nil || ![fileManager fileExistsAtPath:localSource]) {
  499. NSMutableDictionary* details = [NSMutableDictionary dictionary];
  500. [details setValue:@"You are trying upload a file that does not exist" forKey:NSLocalizedDescriptionKey];
  501. NSError *error = [NSError errorWithDomain:k_domain_error_code code:OCErrorFileToUploadDoesNotExist userInfo:details];
  502. failureBeforeRequest(error);
  503. return nil;
  504. } else {
  505. NSMutableURLRequest *request = [self requestWithMethod:@"PUT" path:remoteDestination parameters:nil timeout:k_timeout_webdav];
  506. [request setTimeoutInterval:k_timeout_upload];
  507. [request setValue:[NSString stringWithFormat:@"%lld", [UtilsFramework getSizeInBytesByPath:localSource]] forHTTPHeaderField:@"Content-Length"];
  508. [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
  509. //If is not nil is a redirection so we keep the original url server
  510. if (!self.originalUrlServer) {
  511. self.originalUrlServer = [request.URL absoluteString];
  512. }
  513. if (sharedOCCommunication.isCookiesAvailable) {
  514. //We add the cookies of that URL
  515. request = [UtilsFramework getRequestWithCookiesByRequest:request andOriginalUrlServer:self.originalUrlServer];
  516. } else {
  517. [UtilsFramework deleteAllCookies];
  518. }
  519. NSURL *file = [NSURL fileURLWithPath:localSource];
  520. sharedOCCommunication.uploadSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
  521. NSURLSessionUploadTask *uploadTask = [sharedOCCommunication.uploadSessionManager uploadTaskWithRequest:request fromFile:file progress:^(NSProgress * _Nonnull progress) {
  522. uploadProgress(progress);
  523. } completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
  524. if (error) {
  525. failure(response, responseObject, error);
  526. } else {
  527. success(response,responseObject);
  528. }
  529. }];
  530. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.uploadSessionManager];
  531. [uploadTask resume];
  532. return uploadTask;
  533. }
  534. }
  535. - (void) requestUserNameOfServer:(NSString * _Nonnull) path byCookie:(NSString * _Nonnull) cookieString onCommunication:
  536. (OCCommunication * _Nonnull)sharedOCCommunication success:(void(^ _Nonnull)(NSHTTPURLResponse * _Nonnull, id _Nonnull))success
  537. failure:(void(^ _Nonnull)(NSHTTPURLResponse * _Nonnull, id _Nullable responseObject, NSError * _Nonnull))failure {
  538. NSString *apiUserUrl = nil;
  539. apiUserUrl = [NSString stringWithFormat:@"%@%@", path, k_api_user_url_json];
  540. NSLog(@"api user name call: %@", apiUserUrl);
  541. _requestMethod = @"GET";
  542. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path: apiUserUrl parameters: nil timeout:k_timeout_webdav];
  543. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  544. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  545. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  546. [operation resume];
  547. }
  548. - (void) getStatusOfTheServer:(NSString *)serverPath onCommunication:
  549. (OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id responseObject))success
  550. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  551. NSString *urlString = [NSString stringWithFormat:@"%@%@", serverPath, k_server_information_json];
  552. _requestMethod = @"GET";
  553. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path: urlString parameters: nil timeout:k_timeout_webdav];
  554. request.HTTPShouldHandleCookies = false;
  555. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  556. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  557. [operation resume];
  558. }
  559. - (void)listSharedByServer:(NSString *)serverPath
  560. onCommunication:(OCCommunication *)sharedOCCommunication
  561. success:(void(^)(NSHTTPURLResponse *, id))success
  562. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  563. NSParameterAssert(success);
  564. _requestMethod = @"GET";
  565. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  566. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  567. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  568. [operation resume];
  569. }
  570. - (void)listSharedByServer:(NSString *)serverPath andPath:(NSString *) path
  571. onCommunication:(OCCommunication *)sharedOCCommunication
  572. success:(void(^)(NSHTTPURLResponse *, id))success
  573. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  574. NSParameterAssert(success);
  575. NSString *postString = [NSString stringWithFormat: @"?path=%@&reshares=true",path];
  576. serverPath = [serverPath stringByAppendingString:postString];
  577. _requestMethod = @"GET";
  578. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  579. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  580. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  581. [operation resume];
  582. }
  583. - (void)shareByLinkFileOrFolderByServer:(NSString *)serverPath andPath:(NSString *) filePath andPassword:(NSString *)password andPermission:(NSInteger)permission andHideDownload:(BOOL)hideDownload
  584. onCommunication:(OCCommunication *)sharedOCCommunication
  585. success:(void(^)(NSHTTPURLResponse *, id))success
  586. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  587. NSParameterAssert(success);
  588. _requestMethod = @"POST";
  589. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  590. _postStringForShare = [NSString stringWithFormat: @"path=%@&shareType=3&permissions=%ld&password=%@&hidedownload=%i", filePath, (long)permission, password, hideDownload];
  591. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  592. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  593. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  594. [operation resume];
  595. }
  596. - (void)shareByLinkFileOrFolderByServer:(NSString *)serverPath andPath:(NSString *) filePath
  597. onCommunication:(OCCommunication *)sharedOCCommunication
  598. success:(void(^)(NSHTTPURLResponse *, id))success
  599. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  600. NSParameterAssert(success);
  601. _requestMethod = @"POST";
  602. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  603. _postStringForShare = [NSString stringWithFormat: @"path=%@&shareType=3",filePath];
  604. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  605. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  606. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  607. [operation resume];
  608. }
  609. - (void)shareWith:(NSString *)userOrGroup shareeType:(NSInteger)shareeType inServer:(NSString *) serverPath andPath:(NSString *) filePath andPermissions:(NSInteger) permissions onCommunication:(OCCommunication *)sharedOCCommunication
  610. success:(void(^)(NSHTTPURLResponse *, id))success
  611. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  612. NSParameterAssert(success);
  613. _requestMethod = @"POST";
  614. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  615. self.postStringForShare = [NSString stringWithFormat: @"path=%@&shareType=%ld&shareWith=%@&permissions=%ld",filePath, (long)shareeType, userOrGroup, (long)permissions];
  616. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  617. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  618. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  619. [operation resume];
  620. }
  621. - (void)unShareFileOrFolderByServer:(NSString *)serverPath
  622. onCommunication:(OCCommunication *)sharedOCCommunication
  623. success:(void(^)(NSHTTPURLResponse *, id))success
  624. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  625. NSParameterAssert(success);
  626. _requestMethod = @"DELETE";
  627. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  628. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  629. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  630. [operation resume];
  631. }
  632. - (void)isShareFileOrFolderByServer:(NSString *)serverPath
  633. onCommunication:(OCCommunication *)sharedOCCommunication
  634. success:(void(^)(NSHTTPURLResponse *, id))success
  635. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  636. NSParameterAssert(success);
  637. _requestMethod = @"GET";
  638. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  639. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  640. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  641. [operation resume];
  642. }
  643. - (void) updateShareItem:(NSInteger)shareId ofServerPath:(NSString*)serverPath withPasswordProtect:(NSString*)password andExpirationTime:(NSString*)expirationTime andPermissions:(NSInteger)permissions andHideDownload:(BOOL)hideDownload
  644. onCommunication:(OCCommunication *)sharedOCCommunication
  645. success:(void(^)(NSHTTPURLResponse *, id response))success
  646. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *error))failure{
  647. NSParameterAssert(success);
  648. _requestMethod = @"PUT";
  649. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  650. if (password) {
  651. self.postStringForShare = [NSString stringWithFormat:@"password=%@&",password];
  652. } else if (expirationTime) {
  653. self.postStringForShare = [NSString stringWithFormat:@"expireDate=%@",expirationTime];
  654. } else if (permissions > 0) {
  655. self.postStringForShare = [NSString stringWithFormat:@"permissions=%ld",(long)permissions];
  656. } else {
  657. if (hideDownload) self.postStringForShare = [NSString stringWithFormat:@"hideDownload=true"];
  658. else self.postStringForShare = [NSString stringWithFormat:@"hideDownload=false"];
  659. }
  660. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  661. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  662. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  663. [operation resume];
  664. }
  665. - (void) searchUsersAndGroupsWith:(NSString *)searchString forPage:(NSInteger)page with:(NSInteger)resultsPerPage ofServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  666. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  667. NSParameterAssert(success);
  668. _requestMethod = @"GET";
  669. NSString *searchQuery = [NSString stringWithFormat: @"&search=%@",searchString];
  670. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  671. NSString *queryType = [NSString stringWithFormat:@"&itemType=file"];
  672. NSString *pagination = [NSString stringWithFormat:@"&page=%ld&perPage=%ld", (long)page, (long)resultsPerPage];
  673. serverPath = [serverPath stringByAppendingString:jsonQuery];
  674. serverPath = [serverPath stringByAppendingString:queryType];
  675. serverPath = [serverPath stringByAppendingString:searchQuery];
  676. serverPath = [serverPath stringByAppendingString:pagination];
  677. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  678. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  679. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  680. [operation resume];
  681. }
  682. - (void)getSharePermissionsFile:(NSString*)fileName onCommunication:(OCCommunication *)sharedOCCommunication
  683. success:(void(^)(NSHTTPURLResponse *, id))success
  684. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  685. NSParameterAssert(success);
  686. _requestMethod = @"PROPFIND";
  687. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:fileName parameters:nil timeout:k_timeout_webdav];
  688. [request setHTTPBody:[@"<?xml version=\"1.0\" encoding=\"UTF-8\"?><a:propfind xmlns:a=\"DAV:\" xmlns:b=\"http://open-collaboration-services.org/ns\"><a:prop><b:share-permissions/></a:prop></a:propfind>" dataUsingEncoding:NSUTF8StringEncoding]];
  689. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  690. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  691. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  692. [operation resume];
  693. }
  694. - (void) getCapabilitiesOfServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  695. _requestMethod = @"GET";
  696. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  697. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  698. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  699. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  700. [operation resume];
  701. }
  702. #pragma mark - Remote thumbnails
  703. - (OCHTTPRequestOperation *) getRemoteThumbnailByServer:(NSString*)serverPath ofFilePath:(NSString*)filePath withWidth:(NSInteger)fileWidth andHeight:(NSInteger)fileHeight onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  704. _requestMethod = @"GET";
  705. NSString *query = [NSString stringWithFormat:@"/index.php/apps/files/api/v1/thumbnail/%i/%i/%@", (int)fileWidth, (int)fileHeight, filePath];
  706. serverPath = [serverPath stringByAppendingString:query];
  707. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  708. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  709. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  710. return operation;
  711. }
  712. - (OCHTTPRequestOperation *) getRemotePreviewByServer:(NSString *)serverPath ofFilePath:(NSString *)filePath withWidth:(NSInteger)fileWidth andHeight:(NSInteger)fileHeight andA:(NSInteger)a andMode:(NSString *)mode path:(NSString *)path onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  713. _requestMethod = @"GET";
  714. if (path.length > 0) {
  715. serverPath = path;
  716. } else {
  717. NSString *query = [NSString stringWithFormat:@"/index.php/core/preview.png?file=%@&x=%d&y=%d&a=%d&mode=%@", filePath, (int)fileWidth, (int)fileHeight, (int)a, mode];
  718. serverPath = [serverPath stringByAppendingString:query];
  719. }
  720. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  721. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  722. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  723. return operation;
  724. }
  725. - (OCHTTPRequestOperation *) getRemotePreviewTrashByServer:(NSString *)serverPath ofFileID:(NSString*)fileID onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  726. _requestMethod = @"GET";
  727. NSString *query = [NSString stringWithFormat:@"/index.php/apps/files_trashbin/preview?fileId=%@&x=128&y=128", fileID];
  728. serverPath = [serverPath stringByAppendingString:query];
  729. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  730. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  731. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  732. return operation;
  733. }
  734. #pragma mark - Get Notification
  735. - (void)getNotificationServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  736. _requestMethod = @"GET";
  737. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  738. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  739. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  740. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  741. [operation resume];
  742. }
  743. - (void)setNotificationServer:(NSString *)serverPath type:(NSString *)type onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *, id))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  744. NSParameterAssert(success);
  745. _requestMethod = type;
  746. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  747. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  748. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  749. [operation resume];
  750. }
  751. - (void)subscribingNextcloudServerPush:(NSString *)serverPath pushTokenHash:(NSString *)pushTokenHash devicePublicKey:(NSString *)devicePublicKey proxyServerPath:(NSString *)proxyServerPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *, id))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  752. NSParameterAssert(success);
  753. _requestMethod = @"POST";
  754. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  755. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&pushTokenHash=%@",pushTokenHash]];
  756. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&devicePublicKey=%@",devicePublicKey]];
  757. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&proxyServer=%@",proxyServerPath]];
  758. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  759. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  760. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  761. [operation resume];
  762. }
  763. - (void)unsubscribingNextcloudServerPush:(NSString *)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *, id))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  764. NSParameterAssert(success);
  765. _requestMethod = @"DELETE";
  766. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  767. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  768. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  769. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  770. [operation resume];
  771. }
  772. - (void)subscribingPushProxy:(NSString *)serverPath pushToken:(NSString *)pushToken deviceIdentifier:(NSString *)deviceIdentifier deviceIdentifierSignature:(NSString *)deviceIdentifierSignature publicKey:(NSString *)publicKey onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *, id))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  773. NSParameterAssert(success);
  774. _requestMethod = @"POST";
  775. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  776. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&pushToken=%@",pushToken]];
  777. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&deviceIdentifier=%@",deviceIdentifier]];
  778. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&deviceIdentifierSignature=%@",deviceIdentifierSignature]];
  779. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&userPublicKey=%@",publicKey]];
  780. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  781. [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  782. NSString *userAgent = [request valueForHTTPHeaderField:@"User-Agent"];
  783. //[request setValue:[userAgent stringByAppendingString:@" (SilentPush)"] forHTTPHeaderField:@"User-Agent"];
  784. [request setValue:[userAgent stringByAppendingString:@" (PushKit)"] forHTTPHeaderField:@"User-Agent"];
  785. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  786. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  787. [operation resume];
  788. }
  789. - (void)unsubscribingPushProxy:(NSString *)serverPath deviceIdentifier:(NSString *)deviceIdentifier deviceIdentifierSignature:(NSString *)deviceIdentifierSignature publicKey:(NSString *)publicKey onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *, id))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  790. NSParameterAssert(success);
  791. _requestMethod = @"DELETE";
  792. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  793. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&deviceIdentifier=%@",deviceIdentifier]];
  794. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&deviceIdentifierSignature=%@",deviceIdentifierSignature]];
  795. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&userPublicKey=%@",publicKey]];
  796. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  797. [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  798. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  799. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  800. [operation resume];
  801. }
  802. #pragma mark - Get Activity
  803. - (void) getActivityServer:(NSString*)serverPath since:(NSInteger)since limit:(NSInteger)limit previews:(BOOL)previews link:(NSString *)link onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  804. _requestMethod = @"GET";
  805. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json&since=%ld&limit=%ld", (long)since, (long)limit]];
  806. if (previews) {
  807. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&previews=true"]];
  808. }
  809. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  810. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  811. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  812. [operation resume];
  813. }
  814. #pragma mark - Get External sites
  815. - (void) getExternalSitesServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  816. _requestMethod = @"GET";
  817. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  818. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  819. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  820. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  821. [operation resume];
  822. }
  823. #pragma mark - Get User Profile
  824. - (void) getUserProfileServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  825. _requestMethod = @"GET";
  826. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  827. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  828. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  829. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  830. [operation resume];
  831. }
  832. #pragma mark - End-to-End Encryption
  833. - (void)getEndToEndPublicKeys:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  834. _requestMethod = @"GET";
  835. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  836. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  837. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  838. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  839. [operation resume];
  840. }
  841. - (void)getEndToEndPrivateKeyCipher:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  842. _requestMethod = @"GET";
  843. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  844. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  845. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  846. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  847. [operation resume];
  848. }
  849. - (void)getEndToEndServerPublicKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  850. _requestMethod = @"GET";
  851. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  852. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  853. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  854. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  855. [operation resume];
  856. }
  857. - (void)signEndToEndPublicKey:(NSString*)serverPath key:(NSString *)key onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  858. NSParameterAssert(success);
  859. _requestMethod = @"POST";
  860. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  861. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  862. _postStringKey = [NSString stringWithFormat: @"csr=%@",key];
  863. [request setHTTPBody:[_postStringKey dataUsingEncoding:NSUTF8StringEncoding]];
  864. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  865. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  866. [operation resume];
  867. }
  868. - (void)storeEndToEndPrivateKeyCipher:(NSString*)serverPath key:(NSString *)key onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  869. NSParameterAssert(success);
  870. _requestMethod = @"POST";
  871. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  872. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  873. _postStringKey = [NSString stringWithFormat: @"privateKey=%@",key];
  874. [request setHTTPBody:[_postStringKey dataUsingEncoding:NSUTF8StringEncoding]];
  875. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  876. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  877. [operation resume];
  878. }
  879. - (void)deleteEndToEndPublicKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  880. _requestMethod = @"DELETE";
  881. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  882. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  883. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  884. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  885. [operation resume];
  886. }
  887. - (void)deleteEndToEndPrivateKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  888. _requestMethod = @"DELETE";
  889. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  890. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  891. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  892. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  893. [operation resume];
  894. }
  895. - (void)markEndToEndFolderEncrypted:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  896. _requestMethod = @"PUT";
  897. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  898. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  899. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  900. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  901. [operation resume];
  902. }
  903. - (void)deletemarkEndToEndFolderEncrypted:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  904. _requestMethod = @"DELETE";
  905. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  906. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  907. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  908. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  909. [operation resume];
  910. }
  911. - (void)lockEndToEndFolderEncrypted:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  912. _requestMethod = @"POST";
  913. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  914. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  915. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  916. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  917. [operation resume];
  918. }
  919. - (void)unlockEndToEndFolderEncrypted:(NSString*)serverPath token:(NSString *)token onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  920. _requestMethod = @"DELETE";
  921. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  922. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  923. // Add token
  924. [request setValue:token forHTTPHeaderField:@"token"];
  925. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  926. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  927. [operation resume];
  928. }
  929. - (void)getEndToEndMetadata:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  930. NSParameterAssert(success);
  931. _requestMethod = @"GET";
  932. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  933. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  934. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  935. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  936. [operation resume];
  937. }
  938. - (void)storeEndToEndMetadata:(NSString*)serverPath metadata:(NSString *)metadata onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  939. NSParameterAssert(success);
  940. _requestMethod = @"POST";
  941. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  942. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  943. _postStringMetadata = [NSString stringWithFormat: @"metaData=%@",metadata];
  944. [request setHTTPBody:[_postStringMetadata dataUsingEncoding:NSUTF8StringEncoding]];
  945. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  946. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  947. [operation resume];
  948. }
  949. - (void)updateEndToEndMetadata:(NSString*)serverPath metadata:(NSString *)metadata onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  950. NSParameterAssert(success);
  951. _requestMethod = @"PUT";
  952. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  953. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  954. _postStringMetadata = [NSString stringWithFormat: @"metaData=%@",metadata];
  955. [request setHTTPBody:[_postStringMetadata dataUsingEncoding:NSUTF8StringEncoding]];
  956. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  957. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  958. [operation resume];
  959. }
  960. - (void)deleteEndToEndMetadata:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  961. NSParameterAssert(success);
  962. _requestMethod = @"DELETE";
  963. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  964. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  965. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  966. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  967. [operation resume];
  968. }
  969. #pragma mark - Manage Mobile Editor OCS API
  970. - (void)createLinkRichdocuments:(NSString *)serverPath fileID:(NSString *)fileID onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  971. NSParameterAssert(success);
  972. _requestMethod = @"POST";
  973. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  974. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  975. [request setHTTPBody:[[NSString stringWithFormat: @"fileId=%@",fileID] dataUsingEncoding:NSUTF8StringEncoding]];
  976. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  977. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  978. [operation resume];
  979. }
  980. - (void)getTemplatesRichdocuments:(NSString *)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  981. NSParameterAssert(success);
  982. _requestMethod = @"GET";
  983. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  984. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  985. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  986. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  987. [operation resume];
  988. }
  989. - (void)createNewRichdocuments:(NSString *)serverPath path:(NSString *)path templateID:(NSString *)templateID onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  990. NSParameterAssert(success);
  991. _requestMethod = @"POST";
  992. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  993. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  994. [request setHTTPBody:[[NSString stringWithFormat: @"path=%@&template=%@", path, templateID] dataUsingEncoding:NSUTF8StringEncoding]];
  995. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  996. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  997. [operation resume];
  998. }
  999. - (void)createAssetRichdocuments:(NSString *)serverPath path:(NSString *)path onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  1000. NSParameterAssert(success);
  1001. _requestMethod = @"POST";
  1002. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  1003. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  1004. [request setHTTPBody:[[NSString stringWithFormat: @"path=%@",path] dataUsingEncoding:NSUTF8StringEncoding]];
  1005. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1006. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1007. [operation resume];
  1008. }
  1009. #pragma mark - Fulltextsearch
  1010. - (void)fullTextSearch:(NSString *)serverPath data:(NSString *)data onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  1011. NSParameterAssert(success);
  1012. _requestMethod = @"GET";
  1013. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  1014. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  1015. //[request setHTTPBody:[data dataUsingEncoding:NSUTF8StringEncoding]];
  1016. [request setHTTPBody:[[NSString stringWithFormat:@"request=%@",data] dataUsingEncoding:NSUTF8StringEncoding]];
  1017. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1018. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1019. [operation resume];
  1020. }
  1021. #pragma mark - Remore wipe
  1022. - (void)getSetRemoteWipe:(NSString *)serverPath token:(NSString *)token onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  1023. NSParameterAssert(success);
  1024. _requestMethod = @"POST";
  1025. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  1026. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  1027. [request setHTTPBody:[[NSString stringWithFormat:@"token=%@",token] dataUsingEncoding:NSUTF8StringEncoding]];
  1028. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1029. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1030. [operation resume];
  1031. }
  1032. #pragma mark - Trash
  1033. - (void)listTrash:(NSString *)path depth:(NSString *)depth onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *response, id _Nullable responseObject, NSError *error))failure
  1034. {
  1035. NSParameterAssert(success);
  1036. _requestMethod = @"PROPFIND";
  1037. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_webdav];
  1038. [request setValue: depth forHTTPHeaderField: @"Depth"];
  1039. NSString *body = [NSString stringWithFormat: @""
  1040. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  1041. "<D:propfind xmlns:D=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">"
  1042. "<D:prop>"
  1043. "<D:resourcetype/>"
  1044. "<D:getlastmodified/>"
  1045. "<id xmlns=\"http://owncloud.org/ns\"/>"
  1046. "<D:getcontentlength/>"
  1047. "<nc:trashbin-filename/>"
  1048. "<nc:trashbin-original-location/>"
  1049. "<nc:trashbin-deletion-time/>"
  1050. "<nc:has-preview/>"
  1051. "</D:prop>"
  1052. "</D:propfind>"];
  1053. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  1054. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  1055. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1056. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1057. [operation resume];
  1058. }
  1059. - (void)emptyTrash:(NSString*)path onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  1060. NSParameterAssert(success);
  1061. _requestMethod = @"DELETE";
  1062. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_webdav];
  1063. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  1064. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1065. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1066. [operation resume];
  1067. }
  1068. #pragma mark - Third Parts
  1069. - (void)getHCUserProfile:(NSString *)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id responseObject, NSError *error))failure {
  1070. _requestMethod = @"GET";
  1071. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  1072. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  1073. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1074. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1075. [operation resume];
  1076. }
  1077. - (void)putHCUserProfile:(NSString*)serverPath data:(NSString *)data onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id responseObject, NSError *error))failure {
  1078. NSParameterAssert(success);
  1079. _requestMethod = @"POST";
  1080. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  1081. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  1082. [request setHTTPBody:[[NSString stringWithFormat: @"data=%@",data] dataUsingEncoding:NSUTF8StringEncoding]];
  1083. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1084. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1085. [operation resume];
  1086. }
  1087. - (void)getHCFeatures:(NSString *)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id responseObject, NSError *error))failure {
  1088. _requestMethod = @"GET";
  1089. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  1090. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  1091. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1092. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1093. [operation resume];
  1094. }
  1095. #pragma mark - Manage Redirections
  1096. - (void)setRedirectionBlockOnDatataskWithOCCommunication: (OCCommunication *) sharedOCCommunication andSessionManager:(AFURLSessionManager *) sessionManager{
  1097. [sessionManager setTaskWillPerformHTTPRedirectionBlock:^NSURLRequest * _Nonnull(NSURLSession * _Nonnull session, NSURLSessionTask * _Nonnull task, NSURLResponse * _Nonnull response, NSURLRequest * _Nonnull request) {
  1098. if (response == nil) {
  1099. // needed to handle fake redirects to canonical addresses, as explained in https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/RequestChanges.html
  1100. return request;
  1101. }
  1102. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
  1103. NSDictionary *dict = [httpResponse allHeaderFields];
  1104. //Server path of redirected server
  1105. NSString *responseURLString = [dict objectForKey:@"Location"];
  1106. if (responseURLString) {
  1107. if ([UtilsFramework isURLWithSamlFragment:responseURLString] || httpResponse.statusCode == k_redirected_code_1) {
  1108. //We set the redirectedServer in case SAML or is a permanent redirection
  1109. self.redirectedServer = responseURLString;
  1110. if ([UtilsFramework isURLWithSamlFragment:responseURLString]) {
  1111. // if SAML request, we don't want to follow it; WebView takes care, not here -> nil to NO FOLLOW
  1112. return nil;
  1113. }
  1114. }
  1115. NSMutableURLRequest *requestRedirect = [request mutableCopy];
  1116. [requestRedirect setURL: [NSURL URLWithString:responseURLString]];
  1117. requestRedirect = [sharedOCCommunication getRequestWithCredentials:requestRedirect];
  1118. requestRedirect.HTTPMethod = _requestMethod;
  1119. if (_postStringForShare) {
  1120. //It is a request to share a file by link
  1121. requestRedirect = [self sharedRequestWithMethod:_requestMethod path:responseURLString parameters:nil timeout:k_timeout_webdav];
  1122. [requestRedirect setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  1123. }
  1124. return requestRedirect;
  1125. } else {
  1126. // no location to redirect -> nil to NO FOLLOW
  1127. return nil;
  1128. }
  1129. }];
  1130. }
  1131. @end