OCWebDAVClient.m 89 KB

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