CCNetworking.m 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. //
  2. // CCNetworking.m
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 01/06/15.
  6. // Copyright (c) 2017 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. #import "CCNetworking.h"
  24. #import "NCEndToEndEncryption.h"
  25. #import "AppDelegate.h"
  26. #import "NSDate+ISO8601.h"
  27. #import "NSString+Encode.h"
  28. #import "NCBridgeSwift.h"
  29. @interface CCNetworking ()
  30. {
  31. }
  32. @end
  33. @implementation CCNetworking
  34. + (CCNetworking *)sharedNetworking {
  35. static CCNetworking *sharedNetworking;
  36. @synchronized(self)
  37. {
  38. if (!sharedNetworking) {
  39. sharedNetworking = [[CCNetworking alloc] init];
  40. }
  41. return sharedNetworking;
  42. }
  43. }
  44. - (id)init
  45. {
  46. self = [super init];
  47. // Initialization Sessions
  48. [self sessionUpload];
  49. [self sessionWWanUpload];
  50. [self sessionUploadForeground];
  51. return self;
  52. }
  53. #pragma --------------------------------------------------------------------------------------------
  54. #pragma mark ===== Session =====
  55. #pragma --------------------------------------------------------------------------------------------
  56. - (NSURLSession *)sessionUpload
  57. {
  58. static NSURLSession *sessionUpload = nil;
  59. if (sessionUpload == nil) {
  60. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:k_upload_session];
  61. configuration.allowsCellularAccess = YES;
  62. configuration.sessionSendsLaunchEvents = YES;
  63. configuration.discretionary = NO;
  64. configuration.HTTPMaximumConnectionsPerHost = k_maxHTTPConnectionsPerHost;
  65. configuration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
  66. sessionUpload = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  67. sessionUpload.sessionDescription = k_upload_session;
  68. }
  69. return sessionUpload;
  70. }
  71. - (NSURLSession *)sessionWWanUpload
  72. {
  73. static NSURLSession *sessionWWanUpload = nil;
  74. if (sessionWWanUpload == nil) {
  75. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:k_upload_session_wwan];
  76. configuration.allowsCellularAccess = NO;
  77. configuration.sessionSendsLaunchEvents = YES;
  78. configuration.discretionary = NO;
  79. configuration.HTTPMaximumConnectionsPerHost = k_maxHTTPConnectionsPerHost;
  80. configuration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
  81. sessionWWanUpload = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  82. sessionWWanUpload.sessionDescription = k_upload_session_wwan;
  83. }
  84. return sessionWWanUpload;
  85. }
  86. - (NSURLSession *)sessionUploadForeground
  87. {
  88. static NSURLSession *sessionUploadForeground;
  89. if (sessionUploadForeground == nil) {
  90. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  91. configuration.allowsCellularAccess = YES;
  92. configuration.discretionary = NO;
  93. configuration.HTTPMaximumConnectionsPerHost = k_maxHTTPConnectionsPerHost;
  94. configuration.requestCachePolicy = NSURLRequestReloadIgnoringLocalCacheData;
  95. sessionUploadForeground = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
  96. sessionUploadForeground.sessionDescription = k_upload_session_foreground;
  97. }
  98. return sessionUploadForeground;
  99. }
  100. - (NSURLSession *)getSessionfromSessionDescription:(NSString *)sessionDescription
  101. {
  102. if ([sessionDescription isEqualToString:k_upload_session]) return [self sessionUpload];
  103. if ([sessionDescription isEqualToString:k_upload_session_wwan]) return [self sessionWWanUpload];
  104. if ([sessionDescription isEqualToString:k_upload_session_foreground]) return [self sessionUploadForeground];
  105. return nil;
  106. }
  107. - (void)invalidateAndCancelAllSession
  108. {
  109. [[self sessionUpload] invalidateAndCancel];
  110. [[self sessionWWanUpload] invalidateAndCancel];
  111. [[self sessionUploadForeground] invalidateAndCancel];
  112. }
  113. #pragma --------------------------------------------------------------------------------------------
  114. #pragma mark ===== URLSession download/upload =====
  115. #pragma --------------------------------------------------------------------------------------------
  116. - (void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
  117. {
  118. // The pinnning check
  119. if ([[NCNetworking shared] checkTrustedChallengeWithChallenge:challenge directoryCertificate:[CCUtility getDirectoryCerificates]]) {
  120. completionHandler(NSURLSessionAuthChallengeUseCredential, [NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust]);
  121. } else {
  122. completionHandler(NSURLSessionAuthChallengePerformDefaultHandling, nil);
  123. }
  124. }
  125. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error
  126. {
  127. NSString *url = [[[task currentRequest].URL absoluteString] stringByRemovingPercentEncoding];
  128. if (!url)
  129. return;
  130. NSString *fileName = [url lastPathComponent];
  131. NSString *serverUrl = [self getServerUrlFromUrl:url];
  132. if (!serverUrl) return;
  133. NSInteger errorCode;
  134. NSDate *date = [NSDate date];
  135. NSDateFormatter *dateFormatter = [NSDateFormatter new];
  136. NSLocale *enUSPOSIXLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
  137. [dateFormatter setLocale:enUSPOSIXLocale];
  138. [dateFormatter setDateFormat:@"EEE, dd MMM y HH:mm:ss zzz"];
  139. NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)task.response;
  140. if (httpResponse.statusCode >= 200 && httpResponse.statusCode < 300) {
  141. errorCode = error.code;
  142. } else {
  143. if (httpResponse.statusCode > 0)
  144. errorCode = httpResponse.statusCode;
  145. else
  146. errorCode = error.code;
  147. }
  148. // ------------------------ UPLOAD -----------------------
  149. if ([task isKindOfClass:[NSURLSessionUploadTask class]]) {
  150. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataInSessionFromFileName:fileName serverUrl:serverUrl taskIdentifier:task.taskIdentifier];
  151. if (metadata) {
  152. NSDictionary *fields = [httpResponse allHeaderFields];
  153. NSString *ocId = metadata.ocId;
  154. NSString *etag = metadata.etag;
  155. if (errorCode == 0) {
  156. if ([CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-FileId"]] != nil) {
  157. ocId = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-FileId"]];
  158. } else if ([CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"FileId"]] != nil) {
  159. ocId = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"FileId"]];
  160. }
  161. if ([CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-ETag"]] != nil) {
  162. etag = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"OC-ETag"]];
  163. } else if ([CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"ETag"]] != nil) {
  164. etag = [CCUtility removeForbiddenCharactersFileSystem:[fields objectForKey:@"ETag"]];
  165. }
  166. NSString *dateString = [fields objectForKey:@"Date"];
  167. if (dateString) {
  168. if (![dateFormatter getObjectValue:&date forString:dateString range:nil error:&error]) {
  169. NSLog(@"[LOG] Date '%@' could not be parsed: %@", dateString, error);
  170. date = [NSDate date];
  171. }
  172. } else {
  173. date = [NSDate date];
  174. }
  175. }
  176. if (fileName.length > 0 && ocId.length > 0 && serverUrl.length > 0) {
  177. dispatch_async(dispatch_get_main_queue(), ^{
  178. [self uploadFileSuccessFailure:metadata fileName:fileName ocId:ocId etag:etag date:date serverUrl:serverUrl errorCode:errorCode];
  179. });
  180. }
  181. }
  182. }
  183. }
  184. #pragma --------------------------------------------------------------------------------------------
  185. #pragma mark ===== Upload =====
  186. #pragma --------------------------------------------------------------------------------------------
  187. - (void)uploadFile:(tableMetadata *)metadata taskStatus:(NSInteger)taskStatus
  188. {
  189. // Password nil
  190. if ([CCUtility getPassword:metadata.account].length == 0) {
  191. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_uploadedFile object:nil userInfo:@{@"metadata": metadata, @"errorCode": @(kOCErrorServerUnauthorized), @"errorDescription": @"_bad_username_password_"}];
  192. return;
  193. } else if ([CCUtility getCertificateError:metadata.account]) {
  194. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_uploadedFile object:nil userInfo:@{@"metadata": metadata, @"errorCode": @(NSURLErrorServerCertificateUntrusted), @"errorDescription": @"_ssl_certificate_untrusted_"}];
  195. return;
  196. }
  197. if ([CCUtility fileProviderStorageExists:metadata.ocId fileNameView:metadata.fileNameView] == NO) {
  198. [CCUtility extractImageVideoFromAssetLocalIdentifierForUpload:metadata notification:true completion:^(tableMetadata *newMetadata, NSString *fileNamePath) {
  199. if (newMetadata == nil) {
  200. [[NCManageDatabase sharedInstance] deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"ocId == %@", metadata.ocId]];
  201. } else {
  202. NSString *toPath = [CCUtility getDirectoryProviderStorageOcId:newMetadata.ocId fileNameView:newMetadata.fileNameView];
  203. [CCUtility moveFileAtPath:fileNamePath toPath:toPath];
  204. tableMetadata *metadataForUpload = [[NCManageDatabase sharedInstance] addMetadata:newMetadata];
  205. if ([CCUtility isFolderEncrypted:metadataForUpload.serverUrl e2eEncrypted:metadataForUpload.e2eEncrypted account:metadataForUpload.account] && [CCUtility isEndToEndEnabled:metadataForUpload.account]) {
  206. [self e2eEncryptedFile:metadataForUpload taskStatus:taskStatus];
  207. } else {
  208. [self uploadURLSessionMetadata:metadataForUpload taskStatus:taskStatus];
  209. }
  210. }
  211. }];
  212. } else {
  213. NSDictionary *results = [[NCCommunicationCommon shared] objcGetInternalContenTypeWithFileName:metadata.fileNameView contentType:metadata.contentType directory:metadata.directory];
  214. metadata.contentType = results[@"contentType"];
  215. metadata.iconName = results[@"iconName"];
  216. metadata.typeFile = results[@"typeFile"];
  217. NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[CCUtility getDirectoryProviderStorageOcId:metadata.ocId fileNameView:metadata.fileName] error:nil];
  218. if (attributes[NSFileModificationDate]) {
  219. metadata.date = attributes[NSFileModificationDate];
  220. } else {
  221. metadata.date = [NSDate date];
  222. }
  223. metadata.size = [attributes[NSFileSize] longValue];
  224. tableMetadata *metadataForUpload = [[NCManageDatabase sharedInstance] addMetadata:metadata];
  225. if ([CCUtility isFolderEncrypted:metadataForUpload.serverUrl e2eEncrypted:metadataForUpload.e2eEncrypted account:metadataForUpload.account] && [CCUtility isEndToEndEnabled:metadataForUpload.account]) {
  226. [self e2eEncryptedFile:metadataForUpload taskStatus:taskStatus];
  227. } else {
  228. [self uploadURLSessionMetadata:metadataForUpload taskStatus:taskStatus];
  229. }
  230. }
  231. }
  232. - (void)e2eEncryptedFile:(tableMetadata *)metadata taskStatus:(NSInteger)taskStatus
  233. {
  234. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", metadata.account]];
  235. if (tableAccount == nil) {
  236. [[NCManageDatabase sharedInstance] deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"ocId == %@", metadata.ocId]];
  237. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_uploadedFile object:nil userInfo:@{@"metadata": metadata, @"errorCode": @(k_CCErrorInternalError), @"errorDescription": @"Upload error, account not found"}];
  238. return;
  239. }
  240. NSString *fileNameIdentifier;
  241. NSString *key;
  242. NSString *initializationVector;
  243. NSString *authenticationTag;
  244. NSString *metadataKey;
  245. NSInteger metadataKeyIndex;
  246. // Verify File Size
  247. NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[CCUtility getDirectoryProviderStorageOcId:metadata.ocId fileNameView:metadata.fileNameView] error:nil];
  248. NSNumber *fileSizeNumber = [fileAttributes objectForKey:NSFileSize];
  249. long long fileSize = [fileSizeNumber longLongValue];
  250. if (fileSize > k_max_filesize_E2EE) {
  251. // Error for uploadFileFailure
  252. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_uploadedFile object:nil userInfo:@{@"metadata": metadata, @"errorCode": @(k_CCErrorInternalError), @"errorDescription": @"E2E Error file too big"}];
  253. return;
  254. }
  255. // if new file upload create a new encrypted filename
  256. fileNameIdentifier = [CCUtility generateRandomIdentifier];
  257. /*
  258. if ([metadata.ocId isEqualToString:[CCUtility createMetadataIDFromAccount:metadata.account serverUrl:metadata.serverUrl fileNameView:metadata.fileNameView directory:false]]) {
  259. fileNameIdentifier = [CCUtility generateRandomIdentifier];
  260. } else {
  261. fileNameIdentifier = metadata.fileName;
  262. }
  263. */
  264. [[NCEndToEndEncryption sharedManager] encryptFileName:metadata.fileNameView fileNameIdentifier:fileNameIdentifier directory:[CCUtility getDirectoryProviderStorageOcId:metadata.ocId] key:&key initializationVector:&initializationVector authenticationTag:&authenticationTag];
  265. tableE2eEncryption *object = [[NCManageDatabase sharedInstance] getE2eEncryptionWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", tableAccount.account, metadata.serverUrl]];
  266. if (object) {
  267. metadataKey = object.metadataKey;
  268. metadataKeyIndex = object.metadataKeyIndex;
  269. } else {
  270. metadataKey = [[[NCEndToEndEncryption sharedManager] generateKey:16] base64EncodedStringWithOptions:0]; // AES_KEY_128_LENGTH
  271. metadataKeyIndex = 0;
  272. }
  273. tableE2eEncryption *addObject = [tableE2eEncryption new];
  274. addObject.account = tableAccount.account;
  275. addObject.authenticationTag = authenticationTag;
  276. addObject.fileName = metadata.fileNameView;
  277. addObject.fileNameIdentifier = fileNameIdentifier;
  278. addObject.fileNamePath = [CCUtility returnFileNamePathFromFileName:metadata.fileNameView serverUrl:metadata.serverUrl activeUrl:tableAccount.url];
  279. addObject.key = key;
  280. addObject.initializationVector = initializationVector;
  281. addObject.metadataKey = metadataKey;
  282. addObject.metadataKeyIndex = metadataKeyIndex;
  283. CFStringRef UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)[metadata.fileNameView pathExtension], NULL);
  284. CFStringRef mimeTypeRef = UTTypeCopyPreferredTagWithClass (UTI, kUTTagClassMIMEType);
  285. if (mimeTypeRef) {
  286. addObject.mimeType = (__bridge NSString *)mimeTypeRef;
  287. } else {
  288. addObject.mimeType = @"application/octet-stream";
  289. }
  290. addObject.serverUrl = metadata.serverUrl;
  291. NSString *e2eeApiVersion = [[NCManageDatabase sharedInstance] getCapabilitiesServerStringWithAccount:tableAccount.account elements:NCElementsJSON.shared.capabilitiesE2EEApiVersion];
  292. addObject.version = [e2eeApiVersion intValue];
  293. // Get the last metadata
  294. tableDirectory *directory = [[NCManageDatabase sharedInstance] getTableDirectoryWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@", tableAccount.account, metadata.serverUrl]];
  295. [[NCCommunication shared] getE2EEMetadataWithFileId:directory.fileId e2eToken:nil customUserAgent:nil addCustomHeaders:nil completionHandler:^(NSString *account, NSString *e2eMetadata, NSInteger errorCode, NSString *errorDescription) {
  296. if (errorCode == 0 && e2eMetadata != nil) {
  297. if ([[NCEndToEndMetadata sharedInstance] decoderMetadata:e2eMetadata privateKey:[CCUtility getEndToEndPrivateKey:tableAccount.account] serverUrl:directory.serverUrl account:tableAccount.account url:tableAccount.url] == false) {
  298. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_uploadedFile object:nil userInfo:@{@"metadata": metadata, @"errorCode": @(k_CCErrorInternalError), @"errorDescription": @"_e2e_error_decode_metadata_"}];
  299. return;
  300. }
  301. }
  302. if([[NCManageDatabase sharedInstance] addE2eEncryption:addObject] == NO) {
  303. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_uploadedFile object:nil userInfo:@{@"metadata": metadata, @"errorCode": @(k_CCErrorInternalError), @"errorDescription": @"_e2e_error_create_encrypted_"}];
  304. return;
  305. }
  306. // Now the fileName is fileNameIdentifier && flag e2eEncrypted
  307. metadata.fileName = fileNameIdentifier;
  308. metadata.e2eEncrypted = YES;
  309. // Update Metadata
  310. tableMetadata *metadataEncrypted = [[NCManageDatabase sharedInstance] addMetadata:metadata];
  311. [self uploadURLSessionMetadata:metadataEncrypted taskStatus:taskStatus];
  312. }];
  313. }
  314. - (void)uploadURLSessionMetadata:(tableMetadata *)metadata taskStatus:(NSInteger)taskStatus
  315. {
  316. NSURL *url;
  317. NSMutableURLRequest *request;
  318. PHAsset *asset;
  319. NSError *error;
  320. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", metadata.account]];
  321. if (tableAccount == nil) {
  322. [[NCManageDatabase sharedInstance] deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"ocId == %@", metadata.ocId]];
  323. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_uploadedFile object:nil userInfo:@{@"metadata": metadata, @"errorCode": @(k_CCErrorInternalError), @"errorDescription": @"Upload error, account not found"}];
  324. return;
  325. }
  326. // calculate and store file size
  327. NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:[CCUtility getDirectoryProviderStorageOcId:metadata.ocId fileNameView:metadata.fileName] error:&error];
  328. long long fileSize = [[fileAttributes objectForKey:NSFileSize] longLongValue];
  329. metadata.size = fileSize;
  330. [[NCManageDatabase sharedInstance] addMetadata:metadata];
  331. url = [NSURL URLWithString:[[NSString stringWithFormat:@"%@/%@", metadata.serverUrl, metadata.fileName] encodeString:NSUTF8StringEncoding]];
  332. request = [NSMutableURLRequest requestWithURL:url];
  333. NSData *authData = [[NSString stringWithFormat:@"%@:%@", tableAccount.user, [CCUtility getPassword:tableAccount.account]] dataUsingEncoding:NSUTF8StringEncoding];
  334. NSString *authValue = [NSString stringWithFormat: @"Basic %@",[authData base64EncodedStringWithOptions:0]];
  335. [request setHTTPMethod:@"PUT"];
  336. [request setValue:authValue forHTTPHeaderField:@"Authorization"];
  337. [request setValue:[CCUtility getUserAgent] forHTTPHeaderField:@"User-Agent"];
  338. // Create Image for Upload (gray scale)
  339. #ifndef EXTENSION
  340. [CCGraphics createNewImageFrom:metadata.fileNameView ocId:metadata.ocId filterGrayScale:YES typeFile:metadata.typeFile writeImage:YES];
  341. #endif
  342. // Change date file upload with header : X-OC-Mtime (ctime assetLocalIdentifier) image/video
  343. if (metadata.assetLocalIdentifier) {
  344. PHFetchResult *result = [PHAsset fetchAssetsWithLocalIdentifiers:@[metadata.assetLocalIdentifier] options:nil];
  345. if (result.count) {
  346. asset = result[0];
  347. long dateFileCreation = [asset.creationDate timeIntervalSince1970];
  348. [request setValue:[NSString stringWithFormat:@"%ld", dateFileCreation] forHTTPHeaderField:@"X-OC-Mtime"];
  349. }
  350. }
  351. // NSURLSession
  352. NSURLSession *sessionUpload;
  353. if ([metadata.session isEqualToString:k_upload_session]) sessionUpload = [self sessionUpload];
  354. else if ([metadata.session isEqualToString:k_upload_session_wwan]) sessionUpload = [self sessionWWanUpload];
  355. else if ([metadata.session isEqualToString:k_upload_session_foreground]) sessionUpload = [self sessionUploadForeground];
  356. NSURLSessionUploadTask *uploadTask = [sessionUpload uploadTaskWithRequest:request fromFile:[NSURL fileURLWithPath:[CCUtility getDirectoryProviderStorageOcId:metadata.ocId fileNameView:metadata.fileName]]];
  357. // Manage uploadTask cancel,suspend,resume
  358. if (taskStatus == k_taskStatusCancel) [uploadTask cancel];
  359. else if (taskStatus == k_taskStatusSuspend) [uploadTask suspend];
  360. else if (taskStatus == k_taskStatusResume) [uploadTask resume];
  361. // *** PLAIN ***
  362. [[NCManageDatabase sharedInstance] setMetadataSession:metadata.session sessionError:@"" sessionSelector:nil sessionTaskIdentifier:uploadTask.taskIdentifier status:k_metadataStatusUploading predicate:[NSPredicate predicateWithFormat:@"ocId == %@", metadata.ocId]];
  363. NSLog(@"[LOG] Upload file %@ TaskIdentifier %lu", metadata.fileName, (unsigned long)uploadTask.taskIdentifier);
  364. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_uploadFileStart object:nil userInfo:@{@"ocId": metadata.ocId, @"task": uploadTask, @"serverUrl": metadata.serverUrl, @"account": metadata.account}];
  365. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_reloadDataSource object:nil userInfo:@{@"ocId": metadata.ocId,@"serverUrl": metadata.serverUrl}];
  366. }
  367. - (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveData:(NSData *)data
  368. {
  369. }
  370. - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didSendBodyData:(int64_t)bytesSent totalBytesSent:(int64_t)totalBytesSent totalBytesExpectedToSend:(int64_t)totalBytesExpectedToSend
  371. {
  372. NSString *url = [[[task currentRequest].URL absoluteString] stringByRemovingPercentEncoding];
  373. NSString *fileName = [url lastPathComponent];
  374. NSString *serverUrl = [self getServerUrlFromUrl:url];
  375. if (!serverUrl) return;
  376. if (totalBytesExpectedToSend < 1) {
  377. totalBytesExpectedToSend = totalBytesSent;
  378. }
  379. float progress = (float) totalBytesSent / (float)totalBytesExpectedToSend;
  380. tableMetadata *metadata = [[NCManageDatabase sharedInstance] getMetadataInSessionFromFileName:fileName serverUrl:serverUrl taskIdentifier:task.taskIdentifier];
  381. if (metadata) {
  382. NSDictionary *userInfo = @{@"account": (metadata.account), @"ocId": (metadata.ocId), @"serverUrl": (serverUrl), @"status": ([NSNumber numberWithLong:k_metadataStatusInUpload]), @"progress": ([NSNumber numberWithFloat:progress]), @"totalBytes": ([NSNumber numberWithLongLong:totalBytesSent]), @"totalBytesExpected": ([NSNumber numberWithLongLong:totalBytesExpectedToSend])};
  383. if (userInfo)
  384. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_progressTask object:nil userInfo:userInfo];
  385. }
  386. }
  387. - (void)uploadFileSuccessFailure:(tableMetadata *)metadata fileName:(NSString *)fileName ocId:(NSString *)ocId etag:(NSString *)etag date:(NSDate *)date serverUrl:(NSString *)serverUrl errorCode:(NSInteger)errorCode
  388. {
  389. NSString *tempocId = metadata.ocId;
  390. NSString *errorMessage = @"";
  391. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", metadata.account]];
  392. if (tableAccount == nil) {
  393. [[NCManageDatabase sharedInstance] deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"ocId == %@", tempocId]];
  394. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_uploadedFile object:nil userInfo:@{@"metadata": metadata, @"errorCode": @(errorCode), @"errorDescription": errorMessage}];
  395. return;
  396. }
  397. // ERRORE
  398. if (errorCode != 0) {
  399. #ifndef EXTENSION
  400. AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  401. [appDelegate.listProgressMetadata removeObjectForKey:metadata.ocId];
  402. #endif
  403. // Mark error only if not Cancelled Task
  404. if (errorCode == kCFURLErrorCancelled) {
  405. if (metadata.status == k_metadataStatusUploadForcedStart) {
  406. errorCode = 0;
  407. metadata.session = k_upload_session;
  408. metadata.sessionError = @"";
  409. metadata.sessionTaskIdentifier = 0;
  410. metadata.status = k_metadataStatusInUpload;
  411. metadata = [[NCManageDatabase sharedInstance] addMetadata:metadata];
  412. [[CCNetworking sharedNetworking] uploadFile:metadata taskStatus:k_taskStatusResume];
  413. } else {
  414. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryProviderStorageOcId:tempocId] error:nil];
  415. [[NCManageDatabase sharedInstance] deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"ocId == %@", tempocId]];
  416. errorMessage = [CCError manageErrorKCF:errorCode withNumberError:YES];
  417. }
  418. } else {
  419. if (metadata && (errorCode == kOCErrorServerUnauthorized || errorCode == kOCErrorServerForbidden)) {
  420. #ifndef EXTENSION
  421. [[NCNetworkingCheckRemoteUser shared] checkRemoteUserWithAccount:metadata.account];
  422. #endif
  423. } else if (metadata && errorCode == NSURLErrorServerCertificateUntrusted) {
  424. [CCUtility setCertificateError:metadata.account error:YES];
  425. }
  426. [[NCManageDatabase sharedInstance] setMetadataSession:nil sessionError:[CCError manageErrorKCF:errorCode withNumberError:NO] sessionSelector:nil sessionTaskIdentifier:k_taskIdentifierDone status:k_metadataStatusUploadError predicate:[NSPredicate predicateWithFormat:@"ocId == %@", tempocId]];
  427. errorMessage = [CCError manageErrorKCF:errorCode withNumberError:YES];
  428. }
  429. } else {
  430. // Delete Asset
  431. if (tableAccount.autoUploadDeleteAssetLocalIdentifier && ![metadata.assetLocalIdentifier isEqualToString:@""] && [metadata.sessionSelector isEqualToString:selectorUploadAutoUpload]) {
  432. metadata.deleteAssetLocalIdentifier = true;
  433. }
  434. // Edited file, remove tempocId and adjust the directory provider storage
  435. if (metadata.edited) {
  436. // Update metadata tempocId
  437. [[NCManageDatabase sharedInstance] setMetadataSession:@"" sessionError:@"" sessionSelector:@"" sessionTaskIdentifier:k_taskIdentifierDone status:k_metadataStatusNormal predicate:[NSPredicate predicateWithFormat:@"ocId == %@", tempocId]];
  438. // Add metadata ocId
  439. metadata.date = date;
  440. metadata.etag = etag;
  441. metadata.ocId = ocId;
  442. metadata.session = @"";
  443. metadata.sessionError = @"";
  444. metadata.sessionTaskIdentifier = k_taskIdentifierDone;
  445. metadata.status = k_metadataStatusNormal;
  446. metadata = [[NCManageDatabase sharedInstance] addMetadata:metadata];
  447. // Copy new version on old version
  448. if (![tempocId isEqualToString:metadata.ocId]) {
  449. [CCUtility copyFileAtPath:[NSString stringWithFormat:@"%@/%@", [CCUtility getDirectoryProviderStorage], tempocId] toPath:[NSString stringWithFormat:@"%@/%@", [CCUtility getDirectoryProviderStorage], metadata.ocId]];
  450. [[NCManageDatabase sharedInstance] deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"ocId == %@", tempocId]];
  451. // IMI -> Unzip
  452. #if HC
  453. if ([metadata.typeFile isEqualToString:k_metadataTypeFile_imagemeter]) {
  454. (void)[[IMUtility shared] IMUnzipWithMetadata:metadata];
  455. }
  456. #endif
  457. }
  458. } else {
  459. // Replace Metadata
  460. metadata.date = date;
  461. metadata.etag = etag;
  462. metadata.ocId = ocId;
  463. metadata.session = @"";
  464. metadata.sessionError = @"";
  465. metadata.sessionTaskIdentifier = k_taskIdentifierDone;
  466. metadata.status = k_metadataStatusNormal;
  467. [CCUtility moveFileAtPath:[NSString stringWithFormat:@"%@/%@", [CCUtility getDirectoryProviderStorage], tempocId] toPath:[NSString stringWithFormat:@"%@/%@", [CCUtility getDirectoryProviderStorage], metadata.ocId]];
  468. [[NCManageDatabase sharedInstance] deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND serverUrl == %@ AND fileName == %@", metadata.account, metadata.serverUrl, metadata.fileName]];
  469. metadata = [[NCManageDatabase sharedInstance] addMetadata:metadata];
  470. NSLog(@"[LOG] Insert new upload : %@ - ocId : %@", metadata.fileName, ocId);
  471. }
  472. #ifndef EXTENSION
  473. // EXIF
  474. if ([metadata.typeFile isEqualToString: k_metadataTypeFile_image])
  475. [[CCExifGeo sharedInstance] setExifLocalTableEtag:metadata];
  476. // Create preview
  477. [CCGraphics createNewImageFrom:metadata.fileNameView ocId:metadata.ocId filterGrayScale:NO typeFile:metadata.typeFile writeImage:YES];
  478. // Copy photo or video in the photo album for auto upload
  479. if ([metadata.assetLocalIdentifier length] > 0 && ([metadata.sessionSelector isEqualToString:selectorUploadAutoUpload] || [metadata.sessionSelector isEqualToString:selectorUploadFile])) {
  480. PHAsset *asset;
  481. PHFetchResult *result = [PHAsset fetchAssetsWithLocalIdentifiers:@[metadata.assetLocalIdentifier] options:nil];
  482. if(result.count){
  483. asset = result[0];
  484. [asset saveToAlbum:[NCBrandOptions sharedInstance].brand completionBlock:^(BOOL success) {
  485. if (success) NSLog(@"[LOG] Insert file %@ in %@", metadata.fileName, [NCBrandOptions sharedInstance].brand);
  486. else NSLog(@"[LOG] File %@ do not insert in %@", metadata.fileName, [NCBrandOptions sharedInstance].brand);
  487. }];
  488. }
  489. }
  490. #endif
  491. // Add Local or Remove from cache
  492. if ([CCUtility getDisableLocalCacheAfterUpload] && !metadata.edited) {
  493. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryProviderStorageOcId:metadata.ocId] error:nil];
  494. } else {
  495. // Add Local
  496. (void)[[NCManageDatabase sharedInstance] addLocalFileWithMetadata:metadata];
  497. }
  498. }
  499. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_uploadedFile object:nil userInfo:@{@"metadata": metadata, @"errorCode": @(errorCode), @"errorDescription": errorMessage}];
  500. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:k_notificationCenter_reloadDataSource object:nil userInfo:@{@"ocId": metadata.ocId,@"serverUrl": metadata.serverUrl}];
  501. }
  502. #pragma --------------------------------------------------------------------------------------------
  503. #pragma mark ===== Utility =====
  504. #pragma --------------------------------------------------------------------------------------------
  505. - (NSString *)getServerUrlFromUrl:(NSString *)url
  506. {
  507. NSString *fileName = [url lastPathComponent];
  508. url = [url stringByReplacingOccurrencesOfString:[@"/" stringByAppendingString:fileName] withString:@""];
  509. return url;
  510. }
  511. @end