OCWebDAVClient.m 92 KB

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