OCWebDAVClient.m 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549
  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/><nc:is-encrypted/>"
  182. "<nc:has-preview/>"
  183. "</D:prop>"
  184. "</D:propfind>" dataUsingEncoding:NSUTF8StringEncoding]];
  185. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  186. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  187. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  188. [operation resume];
  189. }
  190. - (void)mr_listPath:(NSString *)path depth:(NSString *)depth withUserSessionToken:(NSString*)token onCommunication:
  191. (OCCommunication *)sharedOCCommunication
  192. success:(void(^)(NSHTTPURLResponse *operation, id response, NSString *token))success
  193. failure:(void(^)(NSHTTPURLResponse *response, id _Nullable responseObject, NSError *, NSString *token))failure {
  194. NSParameterAssert(success);
  195. _requestMethod = @"PROPFIND";
  196. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_webdav];
  197. [request setValue: depth forHTTPHeaderField: @"Depth"];
  198. [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\"><D:prop><D:resourcetype/><D:getlastmodified/><size xmlns=\"http://owncloud.org/ns\"/><favorite xmlns=\"http://owncloud.org/ns\"/><id xmlns=\"http://owncloud.org/ns\"/><D:getcontentlength/><D:getetag/><oc:fileid/><permissions xmlns=\"http://owncloud.org/ns\"/><D:getcontenttype/><nc:is-encrypted/><nc:has-preview/></D:prop></D:propfind>" dataUsingEncoding:NSUTF8StringEncoding]];
  199. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  200. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  201. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  202. [operation resume];
  203. }
  204. - (void)propertiesOfPath:(NSString *)path
  205. onCommunication: (OCCommunication *)sharedOCCommunication
  206. success:(void(^)(NSHTTPURLResponse *, id ))success
  207. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  208. [self mr_listPath:path depth:@"0" onCommunication:sharedOCCommunication success:success failure:failure];
  209. }
  210. - (void)listPath:(NSString *)path depth:(NSString *)depth
  211. onCommunication:(OCCommunication *)sharedOCCommunication
  212. success:(void(^)(NSHTTPURLResponse *, id))success
  213. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  214. [self mr_listPath:path depth:depth onCommunication:sharedOCCommunication success:success failure:failure];
  215. }
  216. - (void)listPath:(NSString *)path depth:(NSString *)depth
  217. onCommunication:(OCCommunication *)sharedOCCommunication withUserSessionToken:(NSString *)token
  218. success:(void(^)(NSHTTPURLResponse *, id, NSString *token))success
  219. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *, NSString *token))failure {
  220. [self mr_listPath:path depth:depth withUserSessionToken:token onCommunication:sharedOCCommunication success:success failure:failure];
  221. }
  222. - (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 {
  223. NSString *body = @"";
  224. NSString *whereType = @"";
  225. NSString *whereDate = @"";
  226. NSParameterAssert(success);
  227. _requestMethod = @"SEARCH";
  228. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_search];
  229. if (contentType && lteDateLastModified && gteDateLastModified) {
  230. body = [NSString stringWithFormat: @""
  231. "<?xml version=\"1.0\"?>"
  232. "<d:searchrequest xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">"
  233. "<d:basicsearch>"
  234. "<d:select>"
  235. "<d:prop>"
  236. "<d:getlastmodified />"
  237. "<d:getetag />"
  238. "<d:getcontenttype />"
  239. "<d:resourcetype/>"
  240. "<d:getcontentlength />"
  241. "<oc:fileid/>"
  242. "<oc:id/>"
  243. "<oc:permissions />"
  244. "<oc:size />"
  245. "<oc:favorite/>"
  246. "<nc:is-encrypted/>"
  247. "<nc:has-preview/>"
  248. "</d:prop>"
  249. "</d:select>"
  250. "<d:from>"
  251. "<d:scope>"
  252. "<d:href>/files/%@%@</d:href>"
  253. "<d:depth>infinity</d:depth>"
  254. "</d:scope>"
  255. "</d:from>"
  256. "<d:orderby>"
  257. "<d:order>"
  258. "<d:prop><d:getlastmodified/></d:prop>"
  259. "<d:descending/>"
  260. "</d:order>"
  261. "<d:order>"
  262. "<d:prop><d:displayname/></d:prop>"
  263. "<d:descending/>"
  264. "</d:order>"
  265. "</d:orderby>"
  266. "<d:where><d:and><d:or>", userID, folder];
  267. for (NSString *type in contentType) {
  268. whereType = [NSString stringWithFormat: @"%@<d:like><d:prop><d:getcontenttype/></d:prop><d:literal>%@</d:literal></d:like>", whereType, type];
  269. }
  270. body = [NSString stringWithFormat: @"%@%@</d:or><d:or>", body, whereType];
  271. 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];
  272. /*
  273. if (gteDateLastModified != nil && lteDateLastModified == nil) {
  274. whereDate = [NSString stringWithFormat: @"%@<d:gte><d:prop><d:getlastmodified/></d:prop><d:literal>%@</d:literal></d:gte>", whereDate, gteDateLastModified];
  275. } else if (gteDateLastModified != nil && lteDateLastModified != nil) {
  276. 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];
  277. }
  278. */
  279. body = [NSString stringWithFormat: @"%@%@</d:or></d:and></d:where></d:basicsearch></d:searchrequest>", body, whereDate];
  280. } else {
  281. body = [NSString stringWithFormat: @""
  282. "<?xml version=\"1.0\"?>"
  283. "<d:searchrequest xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">"
  284. "<d:basicsearch>"
  285. "<d:select>"
  286. "<d:prop>"
  287. "<d:getlastmodified/>"
  288. "<d:getetag/>"
  289. "<d:getcontenttype/>"
  290. "<d:resourcetype/>"
  291. "<d:getcontentlength/>"
  292. "<oc:fileid/>"
  293. "<oc:id/>"
  294. "<oc:permissions/>"
  295. "<oc:size/>"
  296. "<oc:favorite/>"
  297. "<nc:is-encrypted/>"
  298. "<nc:has-preview/>"
  299. "</d:prop>"
  300. "</d:select>"
  301. "<d:from>"
  302. "<d:scope>"
  303. "<d:href>/files/%@%@</d:href>"
  304. "<d:depth>infinity</d:depth>"
  305. "</d:scope>"
  306. "</d:from>"
  307. "<d:where>"
  308. "<d:like>"
  309. "<d:prop><d:displayname/></d:prop>"
  310. "<d:literal>%@</d:literal>"
  311. "</d:like>"
  312. "</d:where>"
  313. "</d:basicsearch>"
  314. "</d:searchrequest>", userID, folder, fileName];
  315. }
  316. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  317. [request setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
  318. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  319. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  320. [operation resume];
  321. }
  322. - (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 {
  323. NSString *body = @"";
  324. NSParameterAssert(success);
  325. _requestMethod = @"SEARCH";
  326. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_search];
  327. body = [NSString stringWithFormat: @""
  328. "<?xml version=\"1.0\"?>"
  329. "<d:searchrequest xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">"
  330. "<d:limit>"
  331. "<d:nresults>10</d:nresults>"
  332. "</d:limit>"
  333. "<d:basicsearch>"
  334. "<d:select>"
  335. "<d:prop>"
  336. "<d:getlastmodified />"
  337. "<d:getetag />"
  338. "<d:getcontenttype />"
  339. "<d:resourcetype/>"
  340. "<d:getcontentlength />"
  341. "<oc:fileid/>"
  342. "<oc:id/>"
  343. "<oc:permissions />"
  344. "<oc:size />"
  345. "<oc:favorite/>"
  346. "<nc:is-encrypted/>"
  347. "<nc:has-preview/>"
  348. "</d:prop>"
  349. "</d:select>"
  350. "<d:from>"
  351. "<d:scope>"
  352. "<d:href>/files/%@%@</d:href>"
  353. "<d:depth>1</d:depth>"
  354. "</d:scope>"
  355. "</d:from>"
  356. "<d:orderby>"
  357. "<d:order>"
  358. "<d:prop><d:displayname/></d:prop>"
  359. "<d:descending/>"
  360. "</d:order>"
  361. "<d:order>"
  362. "<d:prop><d:getlastmodified/></d:prop>"
  363. "<d:descending/>"
  364. "</d:order>"
  365. "</d:orderby>"
  366. "<d:where><d:and><d:or>"
  367. "<d:gte>"
  368. "<d:prop><d:getlastmodified/></d:prop>"
  369. "<d:literal>%@</d:literal>"
  370. "</d:gte>"
  371. "</d:or></d:and></d:where>"
  372. "</d:basicsearch>"
  373. "</d:searchrequest>"
  374. ,userID, folderStartWith, dateLastModified];
  375. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  376. [request setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
  377. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  378. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  379. [operation resume];
  380. }
  381. - (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 {
  382. NSParameterAssert(success);
  383. _requestMethod = @"PROPPATCH";
  384. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_webdav];
  385. NSString *body;
  386. body = [NSString stringWithFormat: @""
  387. "<?xml version=\"1.0\"?>"
  388. "<d:propertyupdate xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">"
  389. "<d:set>"
  390. "<d:prop>"
  391. "<oc:favorite>%i</oc:favorite>"
  392. "</d:prop>"
  393. "</d:set>"
  394. "</d:propertyupdate>", (favorite ? 1 : 0)];
  395. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  396. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  397. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  398. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  399. [operation resume];
  400. }
  401. - (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 {
  402. NSString *body;
  403. NSParameterAssert(success);
  404. _requestMethod = @"REPORT";
  405. userID = [userID stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
  406. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:[NSString stringWithFormat:@"%@/files/%@%@", path, userID, folder] parameters:nil timeout:k_timeout_webdav];
  407. body = [NSString stringWithFormat: @""
  408. "<?xml version=\"1.0\"?>"
  409. "<oc:filter-files xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">"
  410. "<d:prop>"
  411. "<d:resourcetype/>"
  412. "<oc:fileid/>"
  413. "<d:getetag/>"
  414. "<d:getcontentlength/>"
  415. "<oc:size/>"
  416. "<d:getlastmodified/>"
  417. "<oc:id/>"
  418. "<oc:permissions/>"
  419. "<oc:favorite/>"
  420. "<nc:is-encrypted/>"
  421. "<nc:has-preview/>"
  422. "</d:prop>"
  423. "<oc:filter-rules>"
  424. "<oc:favorite>1</oc:favorite>"
  425. "</oc:filter-rules>"
  426. "</oc:filter-files>"];
  427. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  428. [request setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
  429. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  430. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  431. [operation resume];
  432. }
  433. - (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{
  434. NSMutableURLRequest *request = [self requestWithMethod:@"GET" path:remoteSource parameters:nil timeout:k_timeout_webdav];
  435. //If is not nil is a redirection so we keep the original url server
  436. if (!self.originalUrlServer) {
  437. self.originalUrlServer = [request.URL absoluteString];
  438. }
  439. //We add the cookies of that URL
  440. request = [UtilsFramework getRequestWithCookiesByRequest:request andOriginalUrlServer:self.originalUrlServer];
  441. NSURLSessionDownloadTask *downloadTask = [sharedOCCommunication.downloadSessionManager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull progress) {
  442. downloadProgress(progress);
  443. } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
  444. [[NSFileManager defaultManager] removeItemAtURL:[NSURL fileURLWithPath:localDestination] error:nil];
  445. return [NSURL fileURLWithPath:localDestination];
  446. } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
  447. if (error) {
  448. failure(response, error);
  449. } else {
  450. success(response,filePath);
  451. }
  452. }];
  453. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.downloadSessionManager];
  454. if (defaultPriority) {
  455. [downloadTask resume];
  456. }
  457. return downloadTask;
  458. }
  459. - (void)checkServer:(NSString *)path onCommunication:
  460. (OCCommunication *)sharedOCCommunication
  461. success:(void(^)(NSHTTPURLResponse *, id))success
  462. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  463. _requestMethod = @"HEAD";
  464. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_webdav];
  465. request.HTTPShouldHandleCookies = false;
  466. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  467. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  468. [operation resume];
  469. }
  470. - (void)makeCollection:(NSString *)path onCommunication:
  471. (OCCommunication *)sharedOCCommunication
  472. success:(void(^)(NSHTTPURLResponse *, id))success
  473. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  474. _requestMethod = @"MKCOL";
  475. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_webdav];
  476. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  477. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  478. [operation resume];
  479. }
  480. - (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 {
  481. NSFileManager *fileManager = [NSFileManager defaultManager];
  482. if (localSource == nil || ![fileManager fileExistsAtPath:localSource]) {
  483. NSMutableDictionary* details = [NSMutableDictionary dictionary];
  484. [details setValue:@"You are trying upload a file that does not exist" forKey:NSLocalizedDescriptionKey];
  485. NSError *error = [NSError errorWithDomain:k_domain_error_code code:OCErrorFileToUploadDoesNotExist userInfo:details];
  486. failureBeforeRequest(error);
  487. return nil;
  488. } else {
  489. NSMutableURLRequest *request = [self requestWithMethod:@"PUT" path:remoteDestination parameters:nil timeout:k_timeout_webdav];
  490. [request setTimeoutInterval:k_timeout_upload];
  491. [request setValue:[NSString stringWithFormat:@"%lld", [UtilsFramework getSizeInBytesByPath:localSource]] forHTTPHeaderField:@"Content-Length"];
  492. [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
  493. //If is not nil is a redirection so we keep the original url server
  494. if (!self.originalUrlServer) {
  495. self.originalUrlServer = [request.URL absoluteString];
  496. }
  497. if (sharedOCCommunication.isCookiesAvailable) {
  498. //We add the cookies of that URL
  499. request = [UtilsFramework getRequestWithCookiesByRequest:request andOriginalUrlServer:self.originalUrlServer];
  500. } else {
  501. [UtilsFramework deleteAllCookies];
  502. }
  503. NSURL *file = [NSURL fileURLWithPath:localSource];
  504. sharedOCCommunication.uploadSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
  505. NSURLSessionUploadTask *uploadTask = [sharedOCCommunication.uploadSessionManager uploadTaskWithRequest:request fromFile:file progress:^(NSProgress * _Nonnull progress) {
  506. uploadProgress(progress);
  507. } completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
  508. if (error) {
  509. failure(response, responseObject, error);
  510. } else {
  511. success(response,responseObject);
  512. }
  513. }];
  514. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.uploadSessionManager];
  515. [uploadTask resume];
  516. return uploadTask;
  517. }
  518. }
  519. - (void) requestUserNameOfServer:(NSString * _Nonnull) path byCookie:(NSString * _Nonnull) cookieString onCommunication:
  520. (OCCommunication * _Nonnull)sharedOCCommunication success:(void(^ _Nonnull)(NSHTTPURLResponse * _Nonnull, id _Nonnull))success
  521. failure:(void(^ _Nonnull)(NSHTTPURLResponse * _Nonnull, id _Nullable responseObject, NSError * _Nonnull))failure {
  522. NSString *apiUserUrl = nil;
  523. apiUserUrl = [NSString stringWithFormat:@"%@%@", path, k_api_user_url_json];
  524. NSLog(@"api user name call: %@", apiUserUrl);
  525. _requestMethod = @"GET";
  526. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path: apiUserUrl parameters: nil timeout:k_timeout_webdav];
  527. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  528. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  529. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  530. [operation resume];
  531. }
  532. - (void) getStatusOfTheServer:(NSString *)serverPath onCommunication:
  533. (OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id responseObject))success
  534. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  535. NSString *urlString = [NSString stringWithFormat:@"%@%@", serverPath, k_server_information_json];
  536. _requestMethod = @"GET";
  537. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path: urlString parameters: nil timeout:k_timeout_webdav];
  538. request.HTTPShouldHandleCookies = false;
  539. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  540. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  541. [operation resume];
  542. }
  543. - (void)listSharedByServer:(NSString *)serverPath
  544. onCommunication:(OCCommunication *)sharedOCCommunication
  545. success:(void(^)(NSHTTPURLResponse *, id))success
  546. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  547. NSParameterAssert(success);
  548. _requestMethod = @"GET";
  549. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  550. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  551. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  552. [operation resume];
  553. }
  554. - (void)listSharedByServer:(NSString *)serverPath andPath:(NSString *) path
  555. onCommunication:(OCCommunication *)sharedOCCommunication
  556. success:(void(^)(NSHTTPURLResponse *, id))success
  557. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  558. NSParameterAssert(success);
  559. NSString *postString = [NSString stringWithFormat: @"?path=%@&reshares=true",path];
  560. serverPath = [serverPath stringByAppendingString:postString];
  561. _requestMethod = @"GET";
  562. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  563. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  564. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  565. [operation resume];
  566. }
  567. - (void)shareByLinkFileOrFolderByServer:(NSString *)serverPath andPath:(NSString *) filePath andPassword:(NSString *)password andPermission:(NSInteger)permission andHideDownload:(BOOL)hideDownload
  568. onCommunication:(OCCommunication *)sharedOCCommunication
  569. success:(void(^)(NSHTTPURLResponse *, id))success
  570. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  571. NSParameterAssert(success);
  572. _requestMethod = @"POST";
  573. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  574. _postStringForShare = [NSString stringWithFormat: @"path=%@&shareType=3&permissions=%ld&password=%@&hidedownload=%i", filePath, (long)permission, password, hideDownload];
  575. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  576. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  577. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  578. [operation resume];
  579. }
  580. - (void)shareByLinkFileOrFolderByServer:(NSString *)serverPath andPath:(NSString *) filePath
  581. onCommunication:(OCCommunication *)sharedOCCommunication
  582. success:(void(^)(NSHTTPURLResponse *, id))success
  583. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  584. NSParameterAssert(success);
  585. _requestMethod = @"POST";
  586. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  587. _postStringForShare = [NSString stringWithFormat: @"path=%@&shareType=3",filePath];
  588. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  589. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  590. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  591. [operation resume];
  592. }
  593. - (void)shareWith:(NSString *)userOrGroup shareeType:(NSInteger)shareeType inServer:(NSString *) serverPath andPath:(NSString *) filePath andPermissions:(NSInteger) permissions onCommunication:(OCCommunication *)sharedOCCommunication
  594. success:(void(^)(NSHTTPURLResponse *, id))success
  595. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  596. NSParameterAssert(success);
  597. _requestMethod = @"POST";
  598. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  599. self.postStringForShare = [NSString stringWithFormat: @"path=%@&shareType=%ld&shareWith=%@&permissions=%ld",filePath, (long)shareeType, userOrGroup, (long)permissions];
  600. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  601. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  602. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  603. [operation resume];
  604. }
  605. - (void)unShareFileOrFolderByServer:(NSString *)serverPath
  606. onCommunication:(OCCommunication *)sharedOCCommunication
  607. success:(void(^)(NSHTTPURLResponse *, id))success
  608. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  609. NSParameterAssert(success);
  610. _requestMethod = @"DELETE";
  611. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  612. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  613. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  614. [operation resume];
  615. }
  616. - (void)isShareFileOrFolderByServer:(NSString *)serverPath
  617. onCommunication:(OCCommunication *)sharedOCCommunication
  618. success:(void(^)(NSHTTPURLResponse *, id))success
  619. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  620. NSParameterAssert(success);
  621. _requestMethod = @"GET";
  622. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  623. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  624. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  625. [operation resume];
  626. }
  627. - (void) updateShareItem:(NSInteger)shareId ofServerPath:(NSString*)serverPath withPasswordProtect:(NSString*)password andExpirationTime:(NSString*)expirationTime andPermissions:(NSInteger)permissions andHideDownload:(BOOL)hideDownload
  628. onCommunication:(OCCommunication *)sharedOCCommunication
  629. success:(void(^)(NSHTTPURLResponse *, id response))success
  630. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *error))failure{
  631. NSParameterAssert(success);
  632. _requestMethod = @"PUT";
  633. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  634. if (password) {
  635. self.postStringForShare = [NSString stringWithFormat:@"password=%@&",password];
  636. } else if (expirationTime) {
  637. self.postStringForShare = [NSString stringWithFormat:@"expireDate=%@",expirationTime];
  638. } else if (permissions > 0) {
  639. self.postStringForShare = [NSString stringWithFormat:@"permissions=%ld",(long)permissions];
  640. } else {
  641. if (hideDownload) self.postStringForShare = [NSString stringWithFormat:@"hideDownload=true"];
  642. else self.postStringForShare = [NSString stringWithFormat:@"hideDownload=false"];
  643. }
  644. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  645. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  646. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  647. [operation resume];
  648. }
  649. - (void) searchUsersAndGroupsWith:(NSString *)searchString forPage:(NSInteger)page with:(NSInteger)resultsPerPage ofServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  650. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  651. NSParameterAssert(success);
  652. _requestMethod = @"GET";
  653. NSString *searchQuery = [NSString stringWithFormat: @"&search=%@",searchString];
  654. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  655. NSString *queryType = [NSString stringWithFormat:@"&itemType=file"];
  656. NSString *pagination = [NSString stringWithFormat:@"&page=%ld&perPage=%ld", (long)page, (long)resultsPerPage];
  657. serverPath = [serverPath stringByAppendingString:jsonQuery];
  658. serverPath = [serverPath stringByAppendingString:queryType];
  659. serverPath = [serverPath stringByAppendingString:searchQuery];
  660. serverPath = [serverPath stringByAppendingString:pagination];
  661. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  662. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  663. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  664. [operation resume];
  665. }
  666. - (void)getSharePermissionsFile:(NSString*)fileName onCommunication:(OCCommunication *)sharedOCCommunication
  667. success:(void(^)(NSHTTPURLResponse *, id))success
  668. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  669. NSParameterAssert(success);
  670. _requestMethod = @"PROPFIND";
  671. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:fileName parameters:nil timeout:k_timeout_webdav];
  672. [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]];
  673. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  674. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  675. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  676. [operation resume];
  677. }
  678. - (void) getCapabilitiesOfServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  679. _requestMethod = @"GET";
  680. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  681. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  682. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  683. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  684. [operation resume];
  685. }
  686. #pragma mark - Remote thumbnails
  687. - (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{
  688. _requestMethod = @"GET";
  689. NSString *query = [NSString stringWithFormat:@"/index.php/apps/files/api/v1/thumbnail/%i/%i/%@", (int)fileWidth, (int)fileHeight, filePath];
  690. serverPath = [serverPath stringByAppendingString:query];
  691. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  692. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  693. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  694. return operation;
  695. }
  696. - (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 {
  697. _requestMethod = @"GET";
  698. if (path.length > 0) {
  699. serverPath = path;
  700. } else {
  701. 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];
  702. serverPath = [serverPath stringByAppendingString:query];
  703. }
  704. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  705. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  706. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  707. return operation;
  708. }
  709. - (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 {
  710. _requestMethod = @"GET";
  711. NSString *query = [NSString stringWithFormat:@"/index.php/apps/files_trashbin/preview?fileId=%@&x=128&y=128", fileID];
  712. serverPath = [serverPath stringByAppendingString:query];
  713. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  714. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  715. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  716. return operation;
  717. }
  718. #pragma mark - Get Notification
  719. - (void)getNotificationServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  720. _requestMethod = @"GET";
  721. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  722. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  723. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  724. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  725. [operation resume];
  726. }
  727. - (void)setNotificationServer:(NSString *)serverPath type:(NSString *)type onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *, id))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  728. NSParameterAssert(success);
  729. _requestMethod = type;
  730. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  731. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  732. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  733. [operation resume];
  734. }
  735. - (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 {
  736. NSParameterAssert(success);
  737. _requestMethod = @"POST";
  738. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  739. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&pushTokenHash=%@",pushTokenHash]];
  740. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&devicePublicKey=%@",devicePublicKey]];
  741. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&proxyServer=%@",proxyServerPath]];
  742. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  743. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  744. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  745. [operation resume];
  746. }
  747. - (void)unsubscribingNextcloudServerPush:(NSString *)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *, id))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  748. NSParameterAssert(success);
  749. _requestMethod = @"DELETE";
  750. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  751. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  752. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  753. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  754. [operation resume];
  755. }
  756. - (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 {
  757. NSParameterAssert(success);
  758. _requestMethod = @"POST";
  759. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  760. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&pushToken=%@",pushToken]];
  761. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&deviceIdentifier=%@",deviceIdentifier]];
  762. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&deviceIdentifierSignature=%@",deviceIdentifierSignature]];
  763. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&userPublicKey=%@",publicKey]];
  764. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  765. [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  766. NSString *userAgent = [request valueForHTTPHeaderField:@"User-Agent"];
  767. //[request setValue:[userAgent stringByAppendingString:@" (SilentPush)"] forHTTPHeaderField:@"User-Agent"];
  768. [request setValue:[userAgent stringByAppendingString:@" (PushKit)"] forHTTPHeaderField:@"User-Agent"];
  769. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  770. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  771. [operation resume];
  772. }
  773. - (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 {
  774. NSParameterAssert(success);
  775. _requestMethod = @"DELETE";
  776. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  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. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  783. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  784. [operation resume];
  785. }
  786. #pragma mark - Get Activity
  787. - (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{
  788. _requestMethod = @"GET";
  789. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json&since=%ld&limit=%ld", (long)since, (long)limit]];
  790. if (previews) {
  791. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&previews=true"]];
  792. }
  793. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  794. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  795. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  796. [operation resume];
  797. }
  798. #pragma mark - Get External sites
  799. - (void) getExternalSitesServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  800. _requestMethod = @"GET";
  801. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  802. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  803. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  804. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  805. [operation resume];
  806. }
  807. #pragma mark - Get User Profile
  808. - (void) getUserProfileServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  809. _requestMethod = @"GET";
  810. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  811. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  812. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  813. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  814. [operation resume];
  815. }
  816. #pragma mark - End-to-End Encryption
  817. - (void)getEndToEndPublicKeys:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  818. _requestMethod = @"GET";
  819. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  820. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  821. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  822. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  823. [operation resume];
  824. }
  825. - (void)getEndToEndPrivateKeyCipher:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  826. _requestMethod = @"GET";
  827. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  828. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  829. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  830. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  831. [operation resume];
  832. }
  833. - (void)getEndToEndServerPublicKey:(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)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{
  842. NSParameterAssert(success);
  843. _requestMethod = @"POST";
  844. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  845. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  846. _postStringKey = [NSString stringWithFormat: @"csr=%@",key];
  847. [request setHTTPBody:[_postStringKey dataUsingEncoding:NSUTF8StringEncoding]];
  848. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  849. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  850. [operation resume];
  851. }
  852. - (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{
  853. NSParameterAssert(success);
  854. _requestMethod = @"POST";
  855. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  856. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  857. _postStringKey = [NSString stringWithFormat: @"privateKey=%@",key];
  858. [request setHTTPBody:[_postStringKey dataUsingEncoding:NSUTF8StringEncoding]];
  859. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  860. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  861. [operation resume];
  862. }
  863. - (void)deleteEndToEndPublicKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  864. _requestMethod = @"DELETE";
  865. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  866. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  867. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  868. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  869. [operation resume];
  870. }
  871. - (void)deleteEndToEndPrivateKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  872. _requestMethod = @"DELETE";
  873. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  874. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  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)markEndToEndFolderEncrypted:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  880. _requestMethod = @"PUT";
  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)deletemarkEndToEndFolderEncrypted:(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)lockEndToEndFolderEncrypted:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  896. _requestMethod = @"POST";
  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)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{
  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. // Add token
  908. [request setValue:token forHTTPHeaderField:@"token"];
  909. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  910. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  911. [operation resume];
  912. }
  913. - (void)getEndToEndMetadata:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  914. NSParameterAssert(success);
  915. _requestMethod = @"GET";
  916. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  917. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  918. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  919. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  920. [operation resume];
  921. }
  922. - (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{
  923. NSParameterAssert(success);
  924. _requestMethod = @"POST";
  925. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  926. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  927. _postStringMetadata = [NSString stringWithFormat: @"metaData=%@",metadata];
  928. [request setHTTPBody:[_postStringMetadata dataUsingEncoding:NSUTF8StringEncoding]];
  929. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  930. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  931. [operation resume];
  932. }
  933. - (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{
  934. NSParameterAssert(success);
  935. _requestMethod = @"PUT";
  936. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  937. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  938. _postStringMetadata = [NSString stringWithFormat: @"metaData=%@",metadata];
  939. [request setHTTPBody:[_postStringMetadata dataUsingEncoding:NSUTF8StringEncoding]];
  940. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  941. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  942. [operation resume];
  943. }
  944. - (void)deleteEndToEndMetadata:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  945. NSParameterAssert(success);
  946. _requestMethod = @"DELETE";
  947. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  948. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  949. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  950. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  951. [operation resume];
  952. }
  953. #pragma mark - Manage Mobile Editor OCS API
  954. - (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 {
  955. NSParameterAssert(success);
  956. _requestMethod = @"POST";
  957. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  958. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  959. [request setHTTPBody:[[NSString stringWithFormat: @"fileId=%@",fileID] dataUsingEncoding:NSUTF8StringEncoding]];
  960. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  961. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  962. [operation resume];
  963. }
  964. - (void)getTemplatesRichdocuments:(NSString *)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  965. NSParameterAssert(success);
  966. _requestMethod = @"GET";
  967. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  968. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  969. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  970. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  971. [operation resume];
  972. }
  973. - (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 {
  974. NSParameterAssert(success);
  975. _requestMethod = @"POST";
  976. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  977. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  978. [request setHTTPBody:[[NSString stringWithFormat: @"path=%@&template=%@", path, templateID] dataUsingEncoding:NSUTF8StringEncoding]];
  979. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  980. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  981. [operation resume];
  982. }
  983. - (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 {
  984. NSParameterAssert(success);
  985. _requestMethod = @"POST";
  986. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  987. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  988. [request setHTTPBody:[[NSString stringWithFormat: @"path=%@",path] dataUsingEncoding:NSUTF8StringEncoding]];
  989. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  990. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  991. [operation resume];
  992. }
  993. #pragma mark - Fulltextsearch
  994. - (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 {
  995. NSParameterAssert(success);
  996. _requestMethod = @"GET";
  997. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  998. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  999. //[request setHTTPBody:[data dataUsingEncoding:NSUTF8StringEncoding]];
  1000. [request setHTTPBody:[[NSString stringWithFormat:@"request=%@",data] dataUsingEncoding:NSUTF8StringEncoding]];
  1001. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1002. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1003. [operation resume];
  1004. }
  1005. #pragma mark - Remore wipe
  1006. - (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 {
  1007. NSParameterAssert(success);
  1008. _requestMethod = @"POST";
  1009. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  1010. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  1011. [request setHTTPBody:[[NSString stringWithFormat:@"token=%@",token] dataUsingEncoding:NSUTF8StringEncoding]];
  1012. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1013. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1014. [operation resume];
  1015. }
  1016. #pragma mark - Trash
  1017. - (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
  1018. {
  1019. NSParameterAssert(success);
  1020. _requestMethod = @"PROPFIND";
  1021. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_webdav];
  1022. [request setValue: depth forHTTPHeaderField: @"Depth"];
  1023. NSString *body = [NSString stringWithFormat: @""
  1024. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  1025. "<D:propfind xmlns:D=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">"
  1026. "<D:prop>"
  1027. "<D:resourcetype/>"
  1028. "<D:getlastmodified/>"
  1029. "<id xmlns=\"http://owncloud.org/ns\"/>"
  1030. "<D:getcontentlength/>"
  1031. "<nc:trashbin-filename/>"
  1032. "<nc:trashbin-original-location/>"
  1033. "<nc:trashbin-deletion-time/>"
  1034. "<nc:has-preview/>"
  1035. "</D:prop>"
  1036. "</D:propfind>"];
  1037. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  1038. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  1039. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1040. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1041. [operation resume];
  1042. }
  1043. - (void)emptyTrash:(NSString*)path onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  1044. NSParameterAssert(success);
  1045. _requestMethod = @"DELETE";
  1046. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_webdav];
  1047. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  1048. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1049. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1050. [operation resume];
  1051. }
  1052. #pragma mark - Third Parts
  1053. - (void)getHCUserProfile:(NSString *)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id responseObject, NSError *error))failure {
  1054. _requestMethod = @"GET";
  1055. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  1056. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  1057. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1058. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1059. [operation resume];
  1060. }
  1061. - (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 {
  1062. NSParameterAssert(success);
  1063. _requestMethod = @"POST";
  1064. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  1065. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  1066. [request setHTTPBody:[[NSString stringWithFormat: @"data=%@",data] dataUsingEncoding:NSUTF8StringEncoding]];
  1067. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1068. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1069. [operation resume];
  1070. }
  1071. - (void)getHCFeatures:(NSString *)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id responseObject, NSError *error))failure {
  1072. _requestMethod = @"GET";
  1073. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  1074. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  1075. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1076. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1077. [operation resume];
  1078. }
  1079. #pragma mark - Manage Redirections
  1080. - (void)setRedirectionBlockOnDatataskWithOCCommunication: (OCCommunication *) sharedOCCommunication andSessionManager:(AFURLSessionManager *) sessionManager{
  1081. [sessionManager setTaskWillPerformHTTPRedirectionBlock:^NSURLRequest * _Nonnull(NSURLSession * _Nonnull session, NSURLSessionTask * _Nonnull task, NSURLResponse * _Nonnull response, NSURLRequest * _Nonnull request) {
  1082. if (response == nil) {
  1083. // needed to handle fake redirects to canonical addresses, as explained in https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/RequestChanges.html
  1084. return request;
  1085. }
  1086. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
  1087. NSDictionary *dict = [httpResponse allHeaderFields];
  1088. //Server path of redirected server
  1089. NSString *responseURLString = [dict objectForKey:@"Location"];
  1090. if (responseURLString) {
  1091. if ([UtilsFramework isURLWithSamlFragment:responseURLString] || httpResponse.statusCode == k_redirected_code_1) {
  1092. //We set the redirectedServer in case SAML or is a permanent redirection
  1093. self.redirectedServer = responseURLString;
  1094. if ([UtilsFramework isURLWithSamlFragment:responseURLString]) {
  1095. // if SAML request, we don't want to follow it; WebView takes care, not here -> nil to NO FOLLOW
  1096. return nil;
  1097. }
  1098. }
  1099. NSMutableURLRequest *requestRedirect = [request mutableCopy];
  1100. [requestRedirect setURL: [NSURL URLWithString:responseURLString]];
  1101. requestRedirect = [sharedOCCommunication getRequestWithCredentials:requestRedirect];
  1102. requestRedirect.HTTPMethod = _requestMethod;
  1103. if (_postStringForShare) {
  1104. //It is a request to share a file by link
  1105. requestRedirect = [self sharedRequestWithMethod:_requestMethod path:responseURLString parameters:nil timeout:k_timeout_webdav];
  1106. [requestRedirect setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  1107. }
  1108. return requestRedirect;
  1109. } else {
  1110. // no location to redirect -> nil to NO FOLLOW
  1111. return nil;
  1112. }
  1113. }];
  1114. }
  1115. @end