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>0</d:depth>"
  367. "</d:scope>"
  368. "</d:from>"
  369. "<d:orderby>"
  370. "<d:order>"
  371. "<d:prop><d:displayname/></d:prop>"
  372. "<d:descending/>"
  373. "</d:order>"
  374. "<d:order>"
  375. "<d:prop><d:getlastmodified/></d:prop>"
  376. "<d:descending/>"
  377. "</d:order>"
  378. "</d:orderby>"
  379. "<d:where><d:and><d:or>"
  380. "<d:gte>"
  381. "<d:prop><d:getlastmodified/></d:prop>"
  382. "<d:literal>%@</d:literal>"
  383. "</d:gte>"
  384. "</d:or></d:and></d:where>"
  385. "<d:limit>"
  386. "<d:nresults>%@</d:nresults>"
  387. "</d:limit>"
  388. "</d:basicsearch>"
  389. "</d:searchrequest>"
  390. ,userID, folderStartWith, dateLastModified, [@(numberOfItem) stringValue]];
  391. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  392. [request setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
  393. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  394. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  395. [operation resume];
  396. }
  397. - (void)settingFavorite:(NSString * _Nonnull)path favorite:(BOOL)favorite onCommunication:(OCCommunication *)sharedOCCommunication withUserSessionToken:(NSString *)token success:(void(^)(NSHTTPURLResponse *, id, NSString *token))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *, NSString *token))failure {
  398. NSParameterAssert(success);
  399. _requestMethod = @"PROPPATCH";
  400. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_webdav];
  401. NSString *body;
  402. body = [NSString stringWithFormat: @""
  403. "<?xml version=\"1.0\"?>"
  404. "<d:propertyupdate xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">"
  405. "<d:set>"
  406. "<d:prop>"
  407. "<oc:favorite>%i</oc:favorite>"
  408. "</d:prop>"
  409. "</d:set>"
  410. "</d:propertyupdate>", (favorite ? 1 : 0)];
  411. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  412. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  413. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  414. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  415. [operation resume];
  416. }
  417. - (void)listingFavorites:(NSString *)path folder:(NSString *)folder user:(NSString *)user userID:(NSString *)userID onCommunication:(OCCommunication *)sharedOCCommunication withUserSessionToken:(NSString *)token success:(void(^)(NSHTTPURLResponse *, id, NSString *token))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *, NSString *token))failure {
  418. NSString *body;
  419. NSParameterAssert(success);
  420. _requestMethod = @"REPORT";
  421. userID = [userID stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
  422. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:[NSString stringWithFormat:@"%@/files/%@%@", path, userID, folder] parameters:nil timeout:k_timeout_webdav];
  423. body = [NSString stringWithFormat: @""
  424. "<?xml version=\"1.0\"?>"
  425. "<oc:filter-files xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">"
  426. "<d:prop>"
  427. "<d:resourcetype/>"
  428. "<oc:fileid/>"
  429. "<d:getetag/>"
  430. "<d:getcontentlength/>"
  431. "<oc:size/>"
  432. "<d:getlastmodified/>"
  433. "<oc:id/>"
  434. "<oc:permissions/>"
  435. "<oc:favorite/>"
  436. "<nc:is-encrypted/>"
  437. "<nc:has-preview/>"
  438. "</d:prop>"
  439. "<oc:filter-rules>"
  440. "<oc:favorite>1</oc:favorite>"
  441. "</oc:filter-rules>"
  442. "</oc:filter-files>"];
  443. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  444. [request setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
  445. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  446. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  447. [operation resume];
  448. }
  449. - (NSURLSessionDownloadTask *)downloadWithSessionPath:(NSString *)remoteSource toPath:(NSString *)localDestination defaultPriority:(BOOL)defaultPriority onCommunication:(OCCommunication *)sharedOCCommunication progress:(void(^)(NSProgress *progress))downloadProgress success:(void(^)(NSURLResponse *response, NSURL *filePath))success failure:(void(^)(NSURLResponse *response, NSError *error))failure{
  450. NSMutableURLRequest *request = [self requestWithMethod:@"GET" path:remoteSource parameters:nil timeout:k_timeout_webdav];
  451. //If is not nil is a redirection so we keep the original url server
  452. if (!self.originalUrlServer) {
  453. self.originalUrlServer = [request.URL absoluteString];
  454. }
  455. //We add the cookies of that URL
  456. request = [UtilsFramework getRequestWithCookiesByRequest:request andOriginalUrlServer:self.originalUrlServer];
  457. NSURLSessionDownloadTask *downloadTask = [sharedOCCommunication.downloadSessionManager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull progress) {
  458. downloadProgress(progress);
  459. } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
  460. [[NSFileManager defaultManager] removeItemAtURL:[NSURL fileURLWithPath:localDestination] error:nil];
  461. return [NSURL fileURLWithPath:localDestination];
  462. } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
  463. if (error) {
  464. failure(response, error);
  465. } else {
  466. success(response,filePath);
  467. }
  468. }];
  469. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.downloadSessionManager];
  470. if (defaultPriority) {
  471. [downloadTask resume];
  472. }
  473. return downloadTask;
  474. }
  475. - (void)checkServer:(NSString *)path onCommunication:
  476. (OCCommunication *)sharedOCCommunication
  477. success:(void(^)(NSHTTPURLResponse *, id))success
  478. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  479. _requestMethod = @"HEAD";
  480. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_webdav];
  481. request.HTTPShouldHandleCookies = false;
  482. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  483. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  484. [operation resume];
  485. }
  486. - (void)makeCollection:(NSString *)path onCommunication:
  487. (OCCommunication *)sharedOCCommunication
  488. success:(void(^)(NSHTTPURLResponse *, id))success
  489. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  490. _requestMethod = @"MKCOL";
  491. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_webdav];
  492. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  493. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  494. [operation resume];
  495. }
  496. - (NSURLSessionUploadTask *)putWithSessionLocalPath:(NSString *)localSource atRemotePath:(NSString *)remoteDestination onCommunication:(OCCommunication *)sharedOCCommunication progress:(void(^)(NSProgress *progress))uploadProgress success:(void(^)(NSURLResponse *, NSString *))success failure:(void(^)(NSURLResponse *, id, NSError *))failure failureBeforeRequest:(void(^)(NSError *)) failureBeforeRequest {
  497. NSFileManager *fileManager = [NSFileManager defaultManager];
  498. if (localSource == nil || ![fileManager fileExistsAtPath:localSource]) {
  499. NSMutableDictionary* details = [NSMutableDictionary dictionary];
  500. [details setValue:@"You are trying upload a file that does not exist" forKey:NSLocalizedDescriptionKey];
  501. NSError *error = [NSError errorWithDomain:k_domain_error_code code:OCErrorFileToUploadDoesNotExist userInfo:details];
  502. failureBeforeRequest(error);
  503. return nil;
  504. } else {
  505. NSMutableURLRequest *request = [self requestWithMethod:@"PUT" path:remoteDestination parameters:nil timeout:k_timeout_webdav];
  506. [request setTimeoutInterval:k_timeout_upload];
  507. [request setValue:[NSString stringWithFormat:@"%lld", [UtilsFramework getSizeInBytesByPath:localSource]] forHTTPHeaderField:@"Content-Length"];
  508. [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
  509. //If is not nil is a redirection so we keep the original url server
  510. if (!self.originalUrlServer) {
  511. self.originalUrlServer = [request.URL absoluteString];
  512. }
  513. if (sharedOCCommunication.isCookiesAvailable) {
  514. //We add the cookies of that URL
  515. request = [UtilsFramework getRequestWithCookiesByRequest:request andOriginalUrlServer:self.originalUrlServer];
  516. } else {
  517. [UtilsFramework deleteAllCookies];
  518. }
  519. NSURL *file = [NSURL fileURLWithPath:localSource];
  520. sharedOCCommunication.uploadSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
  521. NSURLSessionUploadTask *uploadTask = [sharedOCCommunication.uploadSessionManager uploadTaskWithRequest:request fromFile:file progress:^(NSProgress * _Nonnull progress) {
  522. uploadProgress(progress);
  523. } completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
  524. if (error) {
  525. failure(response, responseObject, error);
  526. } else {
  527. success(response,responseObject);
  528. }
  529. }];
  530. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.uploadSessionManager];
  531. [uploadTask resume];
  532. return uploadTask;
  533. }
  534. }
  535. - (void) requestUserNameOfServer:(NSString * _Nonnull) path byCookie:(NSString * _Nonnull) cookieString onCommunication:
  536. (OCCommunication * _Nonnull)sharedOCCommunication success:(void(^ _Nonnull)(NSHTTPURLResponse * _Nonnull, id _Nonnull))success
  537. failure:(void(^ _Nonnull)(NSHTTPURLResponse * _Nonnull, id _Nullable responseObject, NSError * _Nonnull))failure {
  538. NSString *apiUserUrl = nil;
  539. apiUserUrl = [NSString stringWithFormat:@"%@%@", path, k_api_user_url_json];
  540. NSLog(@"api user name call: %@", apiUserUrl);
  541. _requestMethod = @"GET";
  542. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path: apiUserUrl parameters: nil timeout:k_timeout_webdav];
  543. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  544. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  545. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  546. [operation resume];
  547. }
  548. - (void) getStatusOfTheServer:(NSString *)serverPath onCommunication:
  549. (OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id responseObject))success
  550. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  551. NSString *urlString = [NSString stringWithFormat:@"%@%@", serverPath, k_server_information_json];
  552. _requestMethod = @"GET";
  553. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path: urlString parameters: nil timeout:k_timeout_webdav];
  554. request.HTTPShouldHandleCookies = false;
  555. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  556. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  557. [operation resume];
  558. }
  559. - (void)listSharedByServer:(NSString *)serverPath
  560. onCommunication:(OCCommunication *)sharedOCCommunication
  561. success:(void(^)(NSHTTPURLResponse *, id))success
  562. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  563. NSParameterAssert(success);
  564. _requestMethod = @"GET";
  565. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  566. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  567. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  568. [operation resume];
  569. }
  570. - (void)listSharedByServer:(NSString *)serverPath andPath:(NSString *) path
  571. onCommunication:(OCCommunication *)sharedOCCommunication
  572. success:(void(^)(NSHTTPURLResponse *, id))success
  573. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  574. NSParameterAssert(success);
  575. NSString *postString = [NSString stringWithFormat: @"?path=%@&reshares=true",path];
  576. serverPath = [serverPath stringByAppendingString:postString];
  577. _requestMethod = @"GET";
  578. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  579. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  580. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  581. [operation resume];
  582. }
  583. - (void)shareByLinkFileOrFolderByServer:(NSString *)serverPath andPath:(NSString *) filePath andPassword:(NSString *)password andPermission:(NSInteger)permission andHideDownload:(BOOL)hideDownload
  584. onCommunication:(OCCommunication *)sharedOCCommunication
  585. success:(void(^)(NSHTTPURLResponse *, id))success
  586. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  587. NSParameterAssert(success);
  588. _requestMethod = @"POST";
  589. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  590. _postStringForShare = [NSString stringWithFormat: @"path=%@&shareType=3&permissions=%ld&password=%@&hidedownload=%i", filePath, (long)permission, password, hideDownload];
  591. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  592. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  593. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  594. [operation resume];
  595. }
  596. - (void)shareByLinkFileOrFolderByServer:(NSString *)serverPath andPath:(NSString *) filePath
  597. onCommunication:(OCCommunication *)sharedOCCommunication
  598. success:(void(^)(NSHTTPURLResponse *, id))success
  599. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  600. NSParameterAssert(success);
  601. _requestMethod = @"POST";
  602. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  603. _postStringForShare = [NSString stringWithFormat: @"path=%@&shareType=3",filePath];
  604. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  605. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  606. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  607. [operation resume];
  608. }
  609. - (void)shareWith:(NSString *)userOrGroup shareeType:(NSInteger)shareeType inServer:(NSString *) serverPath andPath:(NSString *) filePath andPermissions:(NSInteger) permissions onCommunication:(OCCommunication *)sharedOCCommunication
  610. success:(void(^)(NSHTTPURLResponse *, id))success
  611. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  612. NSParameterAssert(success);
  613. _requestMethod = @"POST";
  614. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  615. self.postStringForShare = [NSString stringWithFormat: @"path=%@&shareType=%ld&shareWith=%@&permissions=%ld",filePath, (long)shareeType, userOrGroup, (long)permissions];
  616. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  617. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  618. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  619. [operation resume];
  620. }
  621. - (void)unShareFileOrFolderByServer:(NSString *)serverPath
  622. onCommunication:(OCCommunication *)sharedOCCommunication
  623. success:(void(^)(NSHTTPURLResponse *, id))success
  624. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  625. NSParameterAssert(success);
  626. _requestMethod = @"DELETE";
  627. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  628. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  629. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  630. [operation resume];
  631. }
  632. - (void)isShareFileOrFolderByServer:(NSString *)serverPath
  633. onCommunication:(OCCommunication *)sharedOCCommunication
  634. success:(void(^)(NSHTTPURLResponse *, id))success
  635. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  636. NSParameterAssert(success);
  637. _requestMethod = @"GET";
  638. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  639. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  640. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  641. [operation resume];
  642. }
  643. - (void) updateShareItem:(NSInteger)shareId ofServerPath:(NSString*)serverPath withPasswordProtect:(NSString*)password andExpirationTime:(NSString*)expirationTime andPermissions:(NSInteger)permissions andHideDownload:(BOOL)hideDownload
  644. onCommunication:(OCCommunication *)sharedOCCommunication
  645. success:(void(^)(NSHTTPURLResponse *, id response))success
  646. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *error))failure{
  647. NSParameterAssert(success);
  648. _requestMethod = @"PUT";
  649. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  650. if (password) {
  651. self.postStringForShare = [NSString stringWithFormat:@"password=%@&",password];
  652. } else if (expirationTime) {
  653. self.postStringForShare = [NSString stringWithFormat:@"expireDate=%@",expirationTime];
  654. } else if (permissions > 0) {
  655. self.postStringForShare = [NSString stringWithFormat:@"permissions=%ld",(long)permissions];
  656. } else {
  657. if (hideDownload) self.postStringForShare = [NSString stringWithFormat:@"hideDownload=true"];
  658. else self.postStringForShare = [NSString stringWithFormat:@"hideDownload=false"];
  659. }
  660. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  661. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  662. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  663. [operation resume];
  664. }
  665. - (void) searchUsersAndGroupsWith:(NSString *)searchString forPage:(NSInteger)page with:(NSInteger)resultsPerPage ofServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  666. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  667. NSParameterAssert(success);
  668. _requestMethod = @"GET";
  669. NSString *searchQuery = [NSString stringWithFormat: @"&search=%@",searchString];
  670. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  671. NSString *queryType = [NSString stringWithFormat:@"&itemType=file"];
  672. NSString *pagination = [NSString stringWithFormat:@"&page=%ld&perPage=%ld", (long)page, (long)resultsPerPage];
  673. serverPath = [serverPath stringByAppendingString:jsonQuery];
  674. serverPath = [serverPath stringByAppendingString:queryType];
  675. serverPath = [serverPath stringByAppendingString:searchQuery];
  676. serverPath = [serverPath stringByAppendingString:pagination];
  677. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  678. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  679. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  680. [operation resume];
  681. }
  682. - (void)getSharePermissionsFile:(NSString*)fileName onCommunication:(OCCommunication *)sharedOCCommunication
  683. success:(void(^)(NSHTTPURLResponse *, id))success
  684. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  685. NSParameterAssert(success);
  686. _requestMethod = @"PROPFIND";
  687. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:fileName parameters:nil timeout:k_timeout_webdav];
  688. [request setHTTPBody:[@"<?xml version=\"1.0\" encoding=\"UTF-8\"?><a:propfind xmlns:a=\"DAV:\" xmlns:b=\"http://open-collaboration-services.org/ns\"><a:prop><b:share-permissions/></a:prop></a:propfind>" dataUsingEncoding:NSUTF8StringEncoding]];
  689. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  690. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  691. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  692. [operation resume];
  693. }
  694. - (void) getCapabilitiesOfServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  695. _requestMethod = @"GET";
  696. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  697. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  698. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  699. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  700. [operation resume];
  701. }
  702. #pragma mark - Remote thumbnails
  703. - (OCHTTPRequestOperation *) getRemoteThumbnailByServer:(NSString*)serverPath ofFilePath:(NSString*)filePath withWidth:(NSInteger)fileWidth andHeight:(NSInteger)fileHeight onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  704. _requestMethod = @"GET";
  705. NSString *query = [NSString stringWithFormat:@"/index.php/apps/files/api/v1/thumbnail/%i/%i/%@", (int)fileWidth, (int)fileHeight, filePath];
  706. serverPath = [serverPath stringByAppendingString:query];
  707. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  708. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  709. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  710. return operation;
  711. }
  712. - (OCHTTPRequestOperation *) getRemotePreviewByServer:(NSString *)serverPath ofFilePath:(NSString *)filePath withWidth:(NSInteger)fileWidth andHeight:(NSInteger)fileHeight andA:(NSInteger)a andMode:(NSString *)mode path:(NSString *)path onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  713. _requestMethod = @"GET";
  714. if (path.length > 0) {
  715. serverPath = path;
  716. } else {
  717. NSString *query = [NSString stringWithFormat:@"/index.php/core/preview.png?file=%@&x=%d&y=%d&a=%d&mode=%@", filePath, (int)fileWidth, (int)fileHeight, (int)a, mode];
  718. serverPath = [serverPath stringByAppendingString:query];
  719. }
  720. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  721. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  722. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  723. return operation;
  724. }
  725. - (OCHTTPRequestOperation *) getRemotePreviewTrashByServer:(NSString *)serverPath ofFileID:(NSString*)fileID onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  726. _requestMethod = @"GET";
  727. NSString *query = [NSString stringWithFormat:@"/index.php/apps/files_trashbin/preview?fileId=%@&x=128&y=128", fileID];
  728. serverPath = [serverPath stringByAppendingString:query];
  729. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  730. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  731. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  732. return operation;
  733. }
  734. #pragma mark - Get Notification
  735. - (void)getNotificationServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  736. _requestMethod = @"GET";
  737. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  738. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  739. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  740. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  741. [operation resume];
  742. }
  743. - (void)setNotificationServer:(NSString *)serverPath type:(NSString *)type onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *, id))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  744. NSParameterAssert(success);
  745. _requestMethod = type;
  746. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  747. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  748. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  749. [operation resume];
  750. }
  751. - (void)subscribingNextcloudServerPush:(NSString *)serverPath pushTokenHash:(NSString *)pushTokenHash devicePublicKey:(NSString *)devicePublicKey proxyServerPath:(NSString *)proxyServerPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *, id))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  752. NSParameterAssert(success);
  753. _requestMethod = @"POST";
  754. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  755. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&pushTokenHash=%@",pushTokenHash]];
  756. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&devicePublicKey=%@",devicePublicKey]];
  757. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&proxyServer=%@",proxyServerPath]];
  758. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  759. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  760. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  761. [operation resume];
  762. }
  763. - (void)unsubscribingNextcloudServerPush:(NSString *)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *, id))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  764. NSParameterAssert(success);
  765. _requestMethod = @"DELETE";
  766. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  767. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  768. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  769. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  770. [operation resume];
  771. }
  772. - (void)subscribingPushProxy:(NSString *)serverPath pushToken:(NSString *)pushToken deviceIdentifier:(NSString *)deviceIdentifier deviceIdentifierSignature:(NSString *)deviceIdentifierSignature publicKey:(NSString *)publicKey onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *, id))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  773. NSParameterAssert(success);
  774. _requestMethod = @"POST";
  775. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  776. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&pushToken=%@",pushToken]];
  777. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&deviceIdentifier=%@",deviceIdentifier]];
  778. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&deviceIdentifierSignature=%@",deviceIdentifierSignature]];
  779. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&userPublicKey=%@",publicKey]];
  780. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  781. [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  782. NSString *userAgent = [request valueForHTTPHeaderField:@"User-Agent"];
  783. //[request setValue:[userAgent stringByAppendingString:@" (SilentPush)"] forHTTPHeaderField:@"User-Agent"];
  784. [request setValue:[userAgent stringByAppendingString:@" (PushKit)"] forHTTPHeaderField:@"User-Agent"];
  785. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  786. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  787. [operation resume];
  788. }
  789. - (void)unsubscribingPushProxy:(NSString *)serverPath deviceIdentifier:(NSString *)deviceIdentifier deviceIdentifierSignature:(NSString *)deviceIdentifierSignature publicKey:(NSString *)publicKey onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *, id))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  790. NSParameterAssert(success);
  791. _requestMethod = @"DELETE";
  792. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  793. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&deviceIdentifier=%@",deviceIdentifier]];
  794. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&deviceIdentifierSignature=%@",deviceIdentifierSignature]];
  795. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&userPublicKey=%@",publicKey]];
  796. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  797. [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  798. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  799. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  800. [operation resume];
  801. }
  802. #pragma mark - Get Activity
  803. - (void) getActivityServer:(NSString*)serverPath since:(NSInteger)since limit:(NSInteger)limit previews:(BOOL)previews link:(NSString *)link onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  804. _requestMethod = @"GET";
  805. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json&since=%ld&limit=%ld", (long)since, (long)limit]];
  806. if (previews) {
  807. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"&previews=true"]];
  808. }
  809. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  810. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  811. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  812. [operation resume];
  813. }
  814. #pragma mark - Get External sites
  815. - (void) getExternalSitesServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  816. _requestMethod = @"GET";
  817. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  818. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  819. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  820. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  821. [operation resume];
  822. }
  823. #pragma mark - Get User Profile
  824. - (void) getUserProfileServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  825. _requestMethod = @"GET";
  826. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  827. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  828. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  829. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  830. [operation resume];
  831. }
  832. #pragma mark - End-to-End Encryption
  833. - (void)getEndToEndPublicKeys:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  834. _requestMethod = @"GET";
  835. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  836. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  837. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  838. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  839. [operation resume];
  840. }
  841. - (void)getEndToEndPrivateKeyCipher:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  842. _requestMethod = @"GET";
  843. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  844. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  845. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  846. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  847. [operation resume];
  848. }
  849. - (void)getEndToEndServerPublicKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  850. _requestMethod = @"GET";
  851. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  852. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  853. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  854. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  855. [operation resume];
  856. }
  857. - (void)signEndToEndPublicKey:(NSString*)serverPath key:(NSString *)key onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  858. NSParameterAssert(success);
  859. _requestMethod = @"POST";
  860. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  861. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  862. _postStringKey = [NSString stringWithFormat: @"csr=%@",key];
  863. [request setHTTPBody:[_postStringKey dataUsingEncoding:NSUTF8StringEncoding]];
  864. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  865. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  866. [operation resume];
  867. }
  868. - (void)storeEndToEndPrivateKeyCipher:(NSString*)serverPath key:(NSString *)key onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  869. NSParameterAssert(success);
  870. _requestMethod = @"POST";
  871. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  872. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  873. _postStringKey = [NSString stringWithFormat: @"privateKey=%@",key];
  874. [request setHTTPBody:[_postStringKey dataUsingEncoding:NSUTF8StringEncoding]];
  875. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  876. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  877. [operation resume];
  878. }
  879. - (void)deleteEndToEndPublicKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  880. _requestMethod = @"DELETE";
  881. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  882. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  883. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  884. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  885. [operation resume];
  886. }
  887. - (void)deleteEndToEndPrivateKey:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  888. _requestMethod = @"DELETE";
  889. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  890. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  891. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  892. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  893. [operation resume];
  894. }
  895. - (void)markEndToEndFolderEncrypted:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  896. _requestMethod = @"PUT";
  897. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  898. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  899. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  900. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  901. [operation resume];
  902. }
  903. - (void)deletemarkEndToEndFolderEncrypted:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  904. _requestMethod = @"DELETE";
  905. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  906. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  907. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  908. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  909. [operation resume];
  910. }
  911. - (void)lockEndToEndFolderEncrypted:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  912. _requestMethod = @"POST";
  913. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  914. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  915. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  916. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  917. [operation resume];
  918. }
  919. - (void)unlockEndToEndFolderEncrypted:(NSString*)serverPath token:(NSString *)token onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  920. _requestMethod = @"DELETE";
  921. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  922. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  923. // Add token
  924. [request setValue:token forHTTPHeaderField:@"token"];
  925. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  926. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  927. [operation resume];
  928. }
  929. - (void)getEndToEndMetadata:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  930. NSParameterAssert(success);
  931. _requestMethod = @"GET";
  932. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  933. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  934. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  935. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  936. [operation resume];
  937. }
  938. - (void)storeEndToEndMetadata:(NSString*)serverPath metadata:(NSString *)metadata onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  939. NSParameterAssert(success);
  940. _requestMethod = @"POST";
  941. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  942. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  943. _postStringMetadata = [NSString stringWithFormat: @"metaData=%@",metadata];
  944. [request setHTTPBody:[_postStringMetadata dataUsingEncoding:NSUTF8StringEncoding]];
  945. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  946. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  947. [operation resume];
  948. }
  949. - (void)updateEndToEndMetadata:(NSString*)serverPath metadata:(NSString *)metadata onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  950. NSParameterAssert(success);
  951. _requestMethod = @"PUT";
  952. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  953. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  954. _postStringMetadata = [NSString stringWithFormat: @"metaData=%@",metadata];
  955. [request setHTTPBody:[_postStringMetadata dataUsingEncoding:NSUTF8StringEncoding]];
  956. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  957. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  958. [operation resume];
  959. }
  960. - (void)deleteEndToEndMetadata:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  961. NSParameterAssert(success);
  962. _requestMethod = @"DELETE";
  963. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  964. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  965. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  966. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  967. [operation resume];
  968. }
  969. #pragma mark - Manage Mobile Editor OCS API
  970. - (void)createLinkRichdocuments:(NSString *)serverPath fileID:(NSString *)fileID onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  971. NSParameterAssert(success);
  972. _requestMethod = @"POST";
  973. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  974. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  975. [request setHTTPBody:[[NSString stringWithFormat: @"fileId=%@",fileID] dataUsingEncoding:NSUTF8StringEncoding]];
  976. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  977. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  978. [operation resume];
  979. }
  980. - (void)getTemplatesRichdocuments:(NSString *)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  981. NSParameterAssert(success);
  982. _requestMethod = @"GET";
  983. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  984. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  985. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  986. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  987. [operation resume];
  988. }
  989. - (void)createNewRichdocuments:(NSString *)serverPath path:(NSString *)path templateID:(NSString *)templateID onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  990. NSParameterAssert(success);
  991. _requestMethod = @"POST";
  992. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  993. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  994. [request setHTTPBody:[[NSString stringWithFormat: @"path=%@&template=%@", path, templateID] dataUsingEncoding:NSUTF8StringEncoding]];
  995. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  996. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  997. [operation resume];
  998. }
  999. - (void)createAssetRichdocuments:(NSString *)serverPath path:(NSString *)path onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  1000. NSParameterAssert(success);
  1001. _requestMethod = @"POST";
  1002. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  1003. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  1004. [request setHTTPBody:[[NSString stringWithFormat: @"path=%@",path] dataUsingEncoding:NSUTF8StringEncoding]];
  1005. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1006. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1007. [operation resume];
  1008. }
  1009. #pragma mark - Fulltextsearch
  1010. - (void)fullTextSearch:(NSString *)serverPath data:(NSString *)data onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  1011. NSParameterAssert(success);
  1012. _requestMethod = @"GET";
  1013. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  1014. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  1015. //[request setHTTPBody:[data dataUsingEncoding:NSUTF8StringEncoding]];
  1016. [request setHTTPBody:[[NSString stringWithFormat:@"request=%@",data] dataUsingEncoding:NSUTF8StringEncoding]];
  1017. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1018. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1019. [operation resume];
  1020. }
  1021. #pragma mark - Remore wipe
  1022. - (void)getSetRemoteWipe:(NSString *)serverPath token:(NSString *)token onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  1023. NSParameterAssert(success);
  1024. _requestMethod = @"POST";
  1025. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  1026. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  1027. [request setHTTPBody:[[NSString stringWithFormat:@"token=%@",token] dataUsingEncoding:NSUTF8StringEncoding]];
  1028. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1029. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1030. [operation resume];
  1031. }
  1032. #pragma mark - Trash
  1033. - (void)listTrash:(NSString *)path depth:(NSString *)depth onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *response, id _Nullable responseObject, NSError *error))failure
  1034. {
  1035. NSParameterAssert(success);
  1036. _requestMethod = @"PROPFIND";
  1037. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_webdav];
  1038. [request setValue: depth forHTTPHeaderField: @"Depth"];
  1039. NSString *body = [NSString stringWithFormat: @""
  1040. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  1041. "<D:propfind xmlns:D=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\" xmlns:nc=\"http://nextcloud.org/ns\">"
  1042. "<D:prop>"
  1043. "<D:resourcetype/>"
  1044. "<D:getlastmodified/>"
  1045. "<id xmlns=\"http://owncloud.org/ns\"/>"
  1046. "<D:getcontentlength/>"
  1047. "<nc:trashbin-filename/>"
  1048. "<nc:trashbin-original-location/>"
  1049. "<nc:trashbin-deletion-time/>"
  1050. "<nc:has-preview/>"
  1051. "</D:prop>"
  1052. "</D:propfind>"];
  1053. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  1054. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  1055. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1056. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1057. [operation resume];
  1058. }
  1059. - (void)emptyTrash:(NSString*)path onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  1060. NSParameterAssert(success);
  1061. _requestMethod = @"DELETE";
  1062. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:path parameters:nil timeout:k_timeout_webdav];
  1063. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  1064. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1065. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1066. [operation resume];
  1067. }
  1068. #pragma mark - Third Parts
  1069. - (void)getHCUserProfile:(NSString *)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id responseObject, NSError *error))failure {
  1070. _requestMethod = @"GET";
  1071. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  1072. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  1073. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1074. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1075. [operation resume];
  1076. }
  1077. - (void)putHCUserProfile:(NSString*)serverPath data:(NSString *)data onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id responseObject, NSError *error))failure {
  1078. NSParameterAssert(success);
  1079. _requestMethod = @"POST";
  1080. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  1081. [request setValue:@"true" forHTTPHeaderField:@"OCS-APIRequest"];
  1082. [request setHTTPBody:[[NSString stringWithFormat: @"data=%@",data] dataUsingEncoding:NSUTF8StringEncoding]];
  1083. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1084. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1085. [operation resume];
  1086. }
  1087. - (void)getHCFeatures:(NSString *)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success failure:(void(^)(NSHTTPURLResponse *operation, id responseObject, NSError *error))failure {
  1088. _requestMethod = @"GET";
  1089. serverPath = [serverPath stringByAppendingString:[NSString stringWithFormat:@"?format=json"]];
  1090. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil timeout:k_timeout_webdav];
  1091. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  1092. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  1093. [operation resume];
  1094. }
  1095. #pragma mark - Manage Redirections
  1096. - (void)setRedirectionBlockOnDatataskWithOCCommunication: (OCCommunication *) sharedOCCommunication andSessionManager:(AFURLSessionManager *) sessionManager{
  1097. [sessionManager setTaskWillPerformHTTPRedirectionBlock:^NSURLRequest * _Nonnull(NSURLSession * _Nonnull session, NSURLSessionTask * _Nonnull task, NSURLResponse * _Nonnull response, NSURLRequest * _Nonnull request) {
  1098. if (response == nil) {
  1099. // needed to handle fake redirects to canonical addresses, as explained in https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/RequestChanges.html
  1100. return request;
  1101. }
  1102. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
  1103. NSDictionary *dict = [httpResponse allHeaderFields];
  1104. //Server path of redirected server
  1105. NSString *responseURLString = [dict objectForKey:@"Location"];
  1106. if (responseURLString) {
  1107. if ([UtilsFramework isURLWithSamlFragment:responseURLString] || httpResponse.statusCode == k_redirected_code_1) {
  1108. //We set the redirectedServer in case SAML or is a permanent redirection
  1109. self.redirectedServer = responseURLString;
  1110. if ([UtilsFramework isURLWithSamlFragment:responseURLString]) {
  1111. // if SAML request, we don't want to follow it; WebView takes care, not here -> nil to NO FOLLOW
  1112. return nil;
  1113. }
  1114. }
  1115. NSMutableURLRequest *requestRedirect = [request mutableCopy];
  1116. [requestRedirect setURL: [NSURL URLWithString:responseURLString]];
  1117. requestRedirect = [sharedOCCommunication getRequestWithCredentials:requestRedirect];
  1118. requestRedirect.HTTPMethod = _requestMethod;
  1119. if (_postStringForShare) {
  1120. //It is a request to share a file by link
  1121. requestRedirect = [self sharedRequestWithMethod:_requestMethod path:responseURLString parameters:nil timeout:k_timeout_webdav];
  1122. [requestRedirect setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  1123. }
  1124. return requestRedirect;
  1125. } else {
  1126. // no location to redirect -> nil to NO FOLLOW
  1127. return nil;
  1128. }
  1129. }];
  1130. }
  1131. @end