OCWebDAVClient.m 77 KB

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