CCNetworking.m 32 KB

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