OCWebDAVClient.m 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  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. //
  29. // Add : Support for Favorite <favorite xmlns=\"http://owncloud.org/ns\"/>
  30. //
  31. // Author Marino Faggiana <m.faggiana@twsweb.it>
  32. //
  33. #import "OCWebDAVClient.h"
  34. #import "OCFrameworkConstants.h"
  35. #import "OCCommunication.h"
  36. #import "OCChunkDto.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:(NSUInteger)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 {
  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: k_timeout_webdav];
  126. return request;
  127. }
  128. - (NSMutableURLRequest *)sharedRequestWithMethod:(NSString *)method path:(NSString *)path parameters:(NSDictionary *)parameters {
  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];
  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];
  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:(NSUInteger)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];
  170. NSString *depthHeader = nil;
  171. if (depth <= 0)
  172. depthHeader = @"0";
  173. else if (depth == 1)
  174. depthHeader = @"1";
  175. else
  176. depthHeader = @"infinity";
  177. [request setValue: depthHeader forHTTPHeaderField: @"Depth"];
  178. [request setHTTPBody:[@"<?xml version=\"1.0\" encoding=\"UTF-8\"?><D:propfind xmlns:D=\"DAV:\"><D:prop><D:resourcetype/><D:getlastmodified/><size xmlns=\"http://owncloud.org/ns\"/><favorite xmlns=\"http://owncloud.org/ns\"/><D:creationdate/><id xmlns=\"http://owncloud.org/ns\"/><D:getcontentlength/><D:displayname/><D:quota-available-bytes/><D:getetag/><permissions xmlns=\"http://owncloud.org/ns\"/><D:quota-used-bytes/><D:getcontenttype/></D:prop></D:propfind>" dataUsingEncoding:NSUTF8StringEncoding]];
  179. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  180. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  181. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  182. [operation resume];
  183. }
  184. - (void)mr_listPath:(NSString *)path depth:(NSUInteger)depth withUserSessionToken:(NSString*)token onCommunication:
  185. (OCCommunication *)sharedOCCommunication
  186. success:(void(^)(NSHTTPURLResponse *operation, id response, NSString *token))success
  187. failure:(void(^)(NSHTTPURLResponse *response, id _Nullable responseObject, NSError *, NSString *token))failure {
  188. NSParameterAssert(success);
  189. _requestMethod = @"PROPFIND";
  190. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil];
  191. NSString *depthHeader = nil;
  192. if (depth <= 0)
  193. depthHeader = @"0";
  194. else if (depth == 1)
  195. depthHeader = @"1";
  196. else
  197. depthHeader = @"infinity";
  198. [request setValue: depthHeader forHTTPHeaderField: @"Depth"];
  199. [request setHTTPBody:[@"<?xml version=\"1.0\" encoding=\"UTF-8\"?><D:propfind xmlns:D=\"DAV:\"><D:prop><D:resourcetype/><D:getlastmodified/><size xmlns=\"http://owncloud.org/ns\"/><favorite xmlns=\"http://owncloud.org/ns\"/><D:creationdate/><id xmlns=\"http://owncloud.org/ns\"/><D:getcontentlength/><D:displayname/><D:quota-available-bytes/><D:getetag/><permissions xmlns=\"http://owncloud.org/ns\"/><D:quota-used-bytes/><D:getcontenttype/></D:prop></D:propfind>" dataUsingEncoding:NSUTF8StringEncoding]];
  200. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  201. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  202. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  203. [operation resume];
  204. }
  205. - (void)propertiesOfPath:(NSString *)path
  206. onCommunication: (OCCommunication *)sharedOCCommunication
  207. success:(void(^)(NSHTTPURLResponse *, id ))success
  208. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  209. [self mr_listPath:path depth:0 onCommunication:sharedOCCommunication success:success failure:failure];
  210. }
  211. - (void)listPath:(NSString *)path
  212. onCommunication:(OCCommunication *)sharedOCCommunication
  213. success:(void(^)(NSHTTPURLResponse *, id))success
  214. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  215. [self mr_listPath:path depth:1 onCommunication:sharedOCCommunication success:success failure:failure];
  216. }
  217. - (void)listPath:(NSString *)path
  218. onCommunication:(OCCommunication *)sharedOCCommunication withUserSessionToken:(NSString *)token
  219. success:(void(^)(NSHTTPURLResponse *, id, NSString *token))success
  220. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *, NSString *token))failure {
  221. [self mr_listPath:path depth:1 withUserSessionToken:token onCommunication:sharedOCCommunication success:success failure:failure];
  222. }
  223. - (void)search:(NSString *)path fileName:(NSString *)fileName depth:(NSString *)depth user:(NSString *)user onCommunication:(OCCommunication *)sharedOCCommunication withUserSessionToken:(NSString *)token success:(void(^)(NSHTTPURLResponse *, id, NSString *token))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *, NSString *token))failure {
  224. NSParameterAssert(success);
  225. _requestMethod = @"SEARCH";
  226. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil];
  227. NSString *body = [NSString stringWithFormat:@"<?xml version=\"1.0\"?><d:searchrequest xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\"><d:basicsearch><d:select><d:prop><oc:fileid/><d:getcontenttype/><d:getetag/><oc:size/></d:prop></d:select><d:from><d:scope><d:href>/files/%@</d:href><d:depth>infinity</d:depth></d:scope></d:from><d:where><d:like><d:prop><d:displayname/></d:prop><d:literal>%%%@%%</d:literal></d:like></d:where><d:orderby/></d:basicsearch></d:searchrequest>", user, fileName];
  228. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  229. [request setValue:@"text/xml" forHTTPHeaderField:@"Content-Type"];
  230. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  231. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  232. [operation resume];
  233. }
  234. - (void)settingFavorite:(NSString * _Nonnull)serverFilePath 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 {
  235. NSParameterAssert(success);
  236. _requestMethod = @"PROPPATCH";
  237. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:serverFilePath parameters:nil];
  238. NSString *body = [NSString stringWithFormat:@"<?xml version=\"1.0\"?><d:propertyupdate xmlns:d=\"DAV:\" xmlns:oc=\"http://owncloud.org/ns\"><d:set><d:prop><oc:favorite>%i</oc:favorite></d:prop></d:set></d:propertyupdate>", (favorite ? 1 : 0)];
  239. [request setHTTPBody:[body dataUsingEncoding:NSUTF8StringEncoding]];
  240. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  241. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication withUserSessionToken:token success:success failure:failure];
  242. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  243. [operation resume];
  244. }
  245. - (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{
  246. NSMutableURLRequest *request = [self requestWithMethod:@"GET" path:remoteSource parameters:nil];
  247. //If is not nil is a redirection so we keep the original url server
  248. if (!self.originalUrlServer) {
  249. self.originalUrlServer = [request.URL absoluteString];
  250. }
  251. //We add the cookies of that URL
  252. request = [UtilsFramework getRequestWithCookiesByRequest:request andOriginalUrlServer:self.originalUrlServer];
  253. NSURL *localDestinationUrl = [NSURL fileURLWithPath:localDestination];
  254. NSURLSessionDownloadTask *downloadTask = [sharedOCCommunication.downloadSessionManager downloadTaskWithRequest:request progress:^(NSProgress * _Nonnull progress) {
  255. downloadProgress(progress);
  256. } destination:^NSURL * _Nonnull(NSURL * _Nonnull targetPath, NSURLResponse * _Nonnull response) {
  257. return localDestinationUrl;
  258. } completionHandler:^(NSURLResponse * _Nonnull response, NSURL * _Nullable filePath, NSError * _Nullable error) {
  259. if (error) {
  260. failure(response, error);
  261. } else {
  262. success(response,filePath);
  263. }
  264. }];
  265. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.downloadSessionManager];
  266. if (defaultPriority) {
  267. [downloadTask resume];
  268. }
  269. return downloadTask;
  270. }
  271. - (void)checkServer:(NSString *)path onCommunication:
  272. (OCCommunication *)sharedOCCommunication
  273. success:(void(^)(NSHTTPURLResponse *, id))success
  274. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  275. _requestMethod = @"HEAD";
  276. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil];
  277. request.HTTPShouldHandleCookies = false;
  278. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  279. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  280. [operation resume];
  281. }
  282. - (void)makeCollection:(NSString *)path onCommunication:
  283. (OCCommunication *)sharedOCCommunication
  284. success:(void(^)(NSHTTPURLResponse *, id))success
  285. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  286. _requestMethod = @"MKCOL";
  287. NSMutableURLRequest *request = [self requestWithMethod:_requestMethod path:path parameters:nil];
  288. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  289. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  290. [operation resume];
  291. }
  292. - (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 {
  293. NSFileManager *fileManager = [NSFileManager defaultManager];
  294. if (localSource == nil || ![fileManager fileExistsAtPath:localSource]) {
  295. NSMutableDictionary* details = [NSMutableDictionary dictionary];
  296. [details setValue:@"You are trying upload a file that does not exist" forKey:NSLocalizedDescriptionKey];
  297. NSError *error = [NSError errorWithDomain:k_domain_error_code code:OCErrorFileToUploadDoesNotExist userInfo:details];
  298. failureBeforeRequest(error);
  299. return nil;
  300. } else {
  301. NSMutableURLRequest *request = [self requestWithMethod:@"PUT" path:remoteDestination parameters:nil];
  302. [request setTimeoutInterval:k_timeout_upload];
  303. [request setValue:[NSString stringWithFormat:@"%lld", [UtilsFramework getSizeInBytesByPath:localSource]] forHTTPHeaderField:@"Content-Length"];
  304. [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
  305. //If is not nil is a redirection so we keep the original url server
  306. if (!self.originalUrlServer) {
  307. self.originalUrlServer = [request.URL absoluteString];
  308. }
  309. if (sharedOCCommunication.isCookiesAvailable) {
  310. //We add the cookies of that URL
  311. request = [UtilsFramework getRequestWithCookiesByRequest:request andOriginalUrlServer:self.originalUrlServer];
  312. } else {
  313. [UtilsFramework deleteAllCookies];
  314. }
  315. NSURL *file = [NSURL fileURLWithPath:localSource];
  316. sharedOCCommunication.uploadSessionManager.responseSerializer = [AFHTTPResponseSerializer serializer];
  317. NSURLSessionUploadTask *uploadTask = [sharedOCCommunication.uploadSessionManager uploadTaskWithRequest:request fromFile:file progress:^(NSProgress * _Nonnull progress) {
  318. uploadProgress(progress);
  319. } completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
  320. if (error) {
  321. failure(response, responseObject, error);
  322. } else {
  323. success(response,responseObject);
  324. }
  325. }];
  326. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.uploadSessionManager];
  327. [uploadTask resume];
  328. return uploadTask;
  329. }
  330. }
  331. - (void) requestUserNameOfServer:(NSString * _Nonnull) path byCookie:(NSString * _Nonnull) cookieString onCommunication:
  332. (OCCommunication * _Nonnull)sharedOCCommunication success:(void(^ _Nonnull)(NSHTTPURLResponse * _Nonnull, id _Nonnull))success
  333. failure:(void(^ _Nonnull)(NSHTTPURLResponse * _Nonnull, id _Nullable responseObject, NSError * _Nonnull))failure {
  334. NSString *apiUserUrl = nil;
  335. apiUserUrl = [NSString stringWithFormat:@"%@%@", path, k_api_user_url_json];
  336. NSLog(@"api user name call: %@", apiUserUrl);
  337. _requestMethod = @"GET";
  338. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path: apiUserUrl parameters: nil];
  339. [request setValue:@"application/xml" forHTTPHeaderField:@"Content-Type"];
  340. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  341. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  342. [operation resume];
  343. }
  344. - (void) getStatusOfTheServer:(NSString *)serverPath onCommunication:
  345. (OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id responseObject))success
  346. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  347. NSString *urlString = [NSString stringWithFormat:@"%@%@", serverPath, k_server_information_json];
  348. _requestMethod = @"GET";
  349. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path: urlString parameters: nil];
  350. request.HTTPShouldHandleCookies = false;
  351. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  352. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  353. [operation resume];
  354. }
  355. - (void)listSharedByServer:(NSString *)serverPath
  356. onCommunication:(OCCommunication *)sharedOCCommunication
  357. success:(void(^)(NSHTTPURLResponse *, id))success
  358. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  359. NSParameterAssert(success);
  360. _requestMethod = @"GET";
  361. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  362. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  363. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  364. [operation resume];
  365. }
  366. - (void)listSharedByServer:(NSString *)serverPath andPath:(NSString *) path
  367. onCommunication:(OCCommunication *)sharedOCCommunication
  368. success:(void(^)(NSHTTPURLResponse *, id))success
  369. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  370. NSParameterAssert(success);
  371. NSString *postString = [NSString stringWithFormat: @"?path=%@&subfiles=true",path];
  372. serverPath = [serverPath stringByAppendingString:postString];
  373. _requestMethod = @"GET";
  374. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  375. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  376. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  377. [operation resume];
  378. }
  379. - (void)shareByLinkFileOrFolderByServer:(NSString *)serverPath andPath:(NSString *) filePath andPassword:(NSString *)password
  380. onCommunication:(OCCommunication *)sharedOCCommunication
  381. success:(void(^)(NSHTTPURLResponse *, id))success
  382. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  383. NSParameterAssert(success);
  384. _requestMethod = @"POST";
  385. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  386. _postStringForShare = [NSString stringWithFormat: @"path=%@&shareType=3&password=%@",filePath,password];
  387. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  388. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  389. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  390. [operation resume];
  391. }
  392. - (void)shareByLinkFileOrFolderByServer:(NSString *)serverPath andPath:(NSString *) filePath
  393. onCommunication:(OCCommunication *)sharedOCCommunication
  394. success:(void(^)(NSHTTPURLResponse *, id))success
  395. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  396. NSParameterAssert(success);
  397. _requestMethod = @"POST";
  398. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  399. _postStringForShare = [NSString stringWithFormat: @"path=%@&shareType=3",filePath];
  400. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  401. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  402. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  403. [operation resume];
  404. }
  405. - (void)shareWith:(NSString *)userOrGroup shareeType:(NSInteger)shareeType inServer:(NSString *) serverPath andPath:(NSString *) filePath andPermissions:(NSInteger) permissions onCommunication:(OCCommunication *)sharedOCCommunication
  406. success:(void(^)(NSHTTPURLResponse *, id))success
  407. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  408. NSParameterAssert(success);
  409. _requestMethod = @"POST";
  410. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  411. self.postStringForShare = [NSString stringWithFormat: @"path=%@&shareType=%ld&shareWith=%@&permissions=%ld",filePath, (long)shareeType, userOrGroup, (long)permissions];
  412. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  413. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  414. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  415. [operation resume];
  416. }
  417. - (void)unShareFileOrFolderByServer:(NSString *)serverPath
  418. onCommunication:(OCCommunication *)sharedOCCommunication
  419. success:(void(^)(NSHTTPURLResponse *, id))success
  420. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  421. NSParameterAssert(success);
  422. _requestMethod = @"DELETE";
  423. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  424. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  425. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  426. [operation resume];
  427. }
  428. - (void)isShareFileOrFolderByServer:(NSString *)serverPath
  429. onCommunication:(OCCommunication *)sharedOCCommunication
  430. success:(void(^)(NSHTTPURLResponse *, id))success
  431. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  432. NSParameterAssert(success);
  433. _requestMethod = @"GET";
  434. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  435. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  436. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  437. [operation resume];
  438. }
  439. - (void) updateShareItem:(NSInteger)shareId ofServerPath:(NSString*)serverPath withPasswordProtect:(NSString*)password andExpirationTime:(NSString*)expirationTime andPermissions:(NSInteger)permissions
  440. onCommunication:(OCCommunication *)sharedOCCommunication
  441. success:(void(^)(NSHTTPURLResponse *, id response))success
  442. failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *error))failure{
  443. NSParameterAssert(success);
  444. _requestMethod = @"PUT";
  445. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  446. if (password) {
  447. self.postStringForShare = [NSString stringWithFormat:@"password=%@",password];
  448. } else if (expirationTime) {
  449. self.postStringForShare = [NSString stringWithFormat:@"expireDate=%@",expirationTime];
  450. }else if (permissions > 0) {
  451. self.postStringForShare = [NSString stringWithFormat:@"permissions=%ld",(long)permissions];
  452. }
  453. [request setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  454. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  455. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  456. [operation resume];
  457. }
  458. - (void) searchUsersAndGroupsWith:(NSString *)searchString forPage:(NSInteger)page with:(NSInteger)resultsPerPage ofServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  459. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure {
  460. NSParameterAssert(success);
  461. _requestMethod = @"GET";
  462. NSString *searchQuery = [NSString stringWithFormat: @"&search=%@",searchString];
  463. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  464. NSString *queryType = [NSString stringWithFormat:@"&itemType=file"];
  465. NSString *pagination = [NSString stringWithFormat:@"&page=%ld&perPage=%ld", (long)page, (long)resultsPerPage];
  466. serverPath = [serverPath stringByAppendingString:jsonQuery];
  467. serverPath = [serverPath stringByAppendingString:queryType];
  468. serverPath = [serverPath stringByAppendingString:searchQuery];
  469. serverPath = [serverPath stringByAppendingString:pagination];
  470. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  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) getCapabilitiesOfServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  476. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  477. _requestMethod = @"GET";
  478. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  479. serverPath = [serverPath stringByAppendingString:jsonQuery];
  480. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  481. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  482. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  483. [operation resume];
  484. }
  485. #pragma mark - Remote thumbnails
  486. - (OCHTTPRequestOperation *) getRemoteThumbnailByServer:(NSString*)serverPath ofFilePath:(NSString*)filePath withWidth:(NSInteger)fileWidth andHeight:(NSInteger)fileHeight onCommunication:(OCCommunication *)sharedOCCommunication
  487. success:(void(^)(NSHTTPURLResponse *operation, id response))success
  488. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  489. _requestMethod = @"GET";
  490. NSString *query = [NSString stringWithFormat:@"/%i/%i/%@", (int)fileWidth, (int)fileHeight, filePath];
  491. serverPath = [serverPath stringByAppendingString:query];
  492. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  493. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  494. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  495. return operation;
  496. }
  497. #pragma mark - Get Notification
  498. - (void) getNotificationServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  499. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  500. _requestMethod = @"GET";
  501. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  502. serverPath = [serverPath stringByAppendingString:jsonQuery];
  503. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  504. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  505. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  506. [operation resume];
  507. }
  508. - (void) setNotificationServer:(NSString *)serverPath type:(NSString *)type onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *, id))success failure:(void(^)(NSHTTPURLResponse *, id _Nullable responseObject, NSError *))failure {
  509. NSParameterAssert(success);
  510. _requestMethod = type;
  511. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  512. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  513. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  514. [operation resume];
  515. }
  516. #pragma mark - Get User Profile
  517. - (void) getUserProfileServer:(NSString*)serverPath onCommunication:(OCCommunication *)sharedOCCommunication success:(void(^)(NSHTTPURLResponse *operation, id response))success
  518. failure:(void(^)(NSHTTPURLResponse *operation, id _Nullable responseObject, NSError *error))failure{
  519. _requestMethod = @"GET";
  520. NSString *jsonQuery = [NSString stringWithFormat:@"?format=json"];
  521. serverPath = [serverPath stringByAppendingString:jsonQuery];
  522. NSMutableURLRequest *request = [self sharedRequestWithMethod:_requestMethod path:serverPath parameters:nil];
  523. OCHTTPRequestOperation *operation = [self mr_operationWithRequest:request onCommunication:sharedOCCommunication success:success failure:failure];
  524. [self setRedirectionBlockOnDatataskWithOCCommunication:sharedOCCommunication andSessionManager:sharedOCCommunication.networkSessionManager];
  525. [operation resume];
  526. }
  527. #pragma mark - Manage Redirections
  528. - (void) setRedirectionBlockOnDatataskWithOCCommunication: (OCCommunication *) sharedOCCommunication andSessionManager:(AFURLSessionManager *) sessionManager{
  529. [sessionManager setTaskWillPerformHTTPRedirectionBlock:^NSURLRequest * _Nonnull(NSURLSession * _Nonnull session, NSURLSessionTask * _Nonnull task, NSURLResponse * _Nonnull response, NSURLRequest * _Nonnull request) {
  530. if (response == nil) {
  531. // needed to handle fake redirects to canonical addresses, as explained in https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/URLLoadingSystem/Articles/RequestChanges.html
  532. return request;
  533. }
  534. NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response;
  535. NSDictionary *dict = [httpResponse allHeaderFields];
  536. //Server path of redirected server
  537. NSString *responseURLString = [dict objectForKey:@"Location"];
  538. if (responseURLString) {
  539. if ([UtilsFramework isURLWithSamlFragment:responseURLString] || httpResponse.statusCode == k_redirected_code_1) {
  540. //We set the redirectedServer in case SAML or is a permanent redirection
  541. self.redirectedServer = responseURLString;
  542. if ([UtilsFramework isURLWithSamlFragment:responseURLString]) {
  543. // if SAML request, we don't want to follow it; WebView takes care, not here -> nil to NO FOLLOW
  544. return nil;
  545. }
  546. }
  547. NSMutableURLRequest *requestRedirect = [request mutableCopy];
  548. [requestRedirect setURL: [NSURL URLWithString:responseURLString]];
  549. requestRedirect = [sharedOCCommunication getRequestWithCredentials:requestRedirect];
  550. requestRedirect.HTTPMethod = _requestMethod;
  551. if (_postStringForShare) {
  552. //It is a request to share a file by link
  553. requestRedirect = [self sharedRequestWithMethod:_requestMethod path:responseURLString parameters:nil];
  554. [requestRedirect setHTTPBody:[_postStringForShare dataUsingEncoding:NSUTF8StringEncoding]];
  555. }
  556. return requestRedirect;
  557. } else {
  558. // no location to redirect -> nil to NO FOLLOW
  559. return nil;
  560. }
  561. }];
  562. }
  563. @end