CCNetworking.m 31 KB

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