OCWebDAVClient.m 88 KB

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