OCWebDAVClient.m 91 KB

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