marinofaggiana 4 years ago
parent
commit
e44d95e545
2 changed files with 59 additions and 20 deletions
  1. 4 1
      iOSClient/AppDelegate.m
  2. 55 19
      iOSClient/Networking/NCNetworking.swift

+ 4 - 1
iOSClient/AppDelegate.m

@@ -1199,7 +1199,8 @@
                     metadataForUpload.status = k_metadataStatusInUpload;
                     tableMetadata *metadata = [[NCManageDatabase sharedInstance] addMetadata:metadataForUpload];
                     
-                    [[CCNetworking sharedNetworking] uploadFile:metadata taskStatus:k_taskStatusResume];
+                    //[[CCNetworking sharedNetworking] uploadFile:metadata taskStatus:k_taskStatusResume];
+                    [[NCNetworking shared] uploadWithMetadata:metadata e2eEncrypted:false];
                     
                     counterUpload++;
                     sizeUpload = sizeUpload + metadata.size;
@@ -1321,6 +1322,7 @@
     
     // Upload in pending
     //
+    /*
     NSString *sessionExtension = [[NCCommunicationCommon shared] sessionIdentifierExtension];
     NSArray *metadatasInUpload = [[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"session != %@ AND status == %d AND sessionTaskIdentifier == 0", sessionExtension, k_metadataStatusInUpload] sorted:nil ascending:true];
     for (tableMetadata *metadata in metadatasInUpload) {
@@ -1334,6 +1336,7 @@
     if (metadatasInUpload.count == 0) {
         [self.sessionPendingStatusInUpload removeAllObjects];
     }
+    */
     
     // Start Timer
     _timerProcessAutoUpload = [NSTimer scheduledTimerWithTimeInterval:k_timerProcessAutoUpload target:self selector:@selector(loadAutoUpload) userInfo:nil repeats:YES];

+ 55 - 19
iOSClient/Networking/NCNetworking.swift

@@ -27,8 +27,8 @@ import NCCommunication
 import Alamofire
 
 @objc public protocol NCNetworkingDelegate {
-    @objc optional func downloadProgress(_ progress: Double, fileName: String, ServerUrl: String, session: URLSession, task: URLSessionTask)
-    @objc optional func uploadProgress(_ progress: Double, fileName: String, ServerUrl: String, session: URLSession, task: URLSessionTask)
+    @objc optional func downloadProgress(_ progress: Double, totalBytes: Int64, totalBytesExpected: Int64, fileName: String, ServerUrl: String, session: URLSession, task: URLSessionTask)
+    @objc optional func uploadProgress(_ progress: Double, totalBytes: Int64, totalBytesExpected: Int64, fileName: String, ServerUrl: String, session: URLSession, task: URLSessionTask)
     @objc optional func downloadComplete(fileName: String, serverUrl: String, etag: String?, date: NSDate?, dateLastModified: NSDate?, length: Double, description: String?, error: Error?, statusCode: Int)
     @objc optional func uploadComplete(fileName: String, serverUrl: String, ocId: String?, etag: String?, date: NSDate?, size: Int64, description: String?, error: Error?, statusCode: Int)
 }
@@ -77,16 +77,8 @@ import Alamofire
         }
     }
     
-    func downloadProgress(_ progress: Double, fileName: String, ServerUrl: String, session: URLSession, task: URLSessionTask) {
-        delegate?.downloadProgress?(progress, fileName: fileName, ServerUrl: ServerUrl, session: session, task: task)
-    }
-    
-    func uploadProgress(_ progress: Double, fileName: String, ServerUrl: String, session: URLSession, task: URLSessionTask) {
-        delegate?.uploadProgress?(progress, fileName: fileName, ServerUrl: ServerUrl, session: session, task: task)
-    }
-    
-    func uploadComplete(fileName: String, serverUrl: String, ocId: String?, etag: String?, date: NSDate?, size: Int64, description: String?, error: Error?, statusCode: Int) {
-        delegate?.uploadComplete?(fileName: fileName, serverUrl: serverUrl, ocId: ocId, etag: etag, date: date, size:size, description: description, error: error, statusCode: statusCode)
+    func downloadProgress(_ progress: Double, totalBytes: Int64, totalBytesExpected: Int64, fileName: String, ServerUrl: String, session: URLSession, task: URLSessionTask) {
+        delegate?.downloadProgress?(progress, totalBytes: totalBytes, totalBytesExpected: totalBytesExpected, fileName: fileName, ServerUrl: ServerUrl, session: session, task: task)
     }
     
     func downloadComplete(fileName: String, serverUrl: String, etag: String?, date: NSDate?, dateLastModified: NSDate?, length: Double, description: String?, error: Error?, statusCode: Int) {
@@ -353,13 +345,15 @@ import Alamofire
             }
                
             metadataForUpload = NCManageDatabase.sharedInstance.addMetadata(metadata)
-            
-            #if !EXTENSION
+           
             if e2eEncrypted {
+                #if !EXTENSION
                 NCNetworkingE2EE.shared.upload(metadata: metadataForUpload!, account: account)
+                #endif
+            } else {
+                uploadFile(metadata: metadataForUpload!, account: account)
             }
-            #endif
-            
+           
         } else {
                
             CCUtility.extractImageVideoFromAssetLocalIdentifier(forUpload: metadata, notification: true) { (extractMetadata, fileNamePath) in
@@ -378,16 +372,58 @@ import Alamofire
                 }
                        
                 metadataForUpload = NCManageDatabase.sharedInstance.addMetadata(extractMetadata)
-                
-                #if !EXTENSION
+               
                 if e2eEncrypted {
+                    #if !EXTENSION
                     NCNetworkingE2EE.shared.upload(metadata: metadataForUpload!, account: account)
+                    #endif
+                } else {
+                    self.uploadFile(metadata: metadataForUpload!, account: account)
                 }
-                #endif
             }
         }
     }
     
+    private func uploadFile(metadata: tableMetadata, account: tableAccount) {
+        
+        let serverUrlFileName = metadata.serverUrl + "/" + metadata.fileName
+        let fileNameLocalPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
+        
+        if let task = NCCommunicationBackground.shared.upload(serverUrlFileName: serverUrlFileName, fileNameLocalPath: fileNameLocalPath, dateCreationFile: nil, dateModificationFile: nil, description: "", session: NCCommunicationBackground.shared.sessionManagerTransferExtension) {
+         
+            metadata.status = Int(k_metadataStatusUploading)
+            metadata.sessionError = ""
+            metadata.sessionTaskIdentifier = task.taskIdentifier
+            NCManageDatabase.sharedInstance.addMetadata(metadata)
+            
+            NotificationCenter.default.post(name: Notification.Name.init(rawValue: k_notificationCenter_uploadFileStart), object: nil, userInfo: ["ocId":metadata.ocId, "task":task, "serverUrl":metadata.serverUrl, "account":metadata.account])
+            NotificationCenter.default.post(name: Notification.Name.init(rawValue: k_notificationCenter_reloadDataSource), object: nil, userInfo: ["ocId":metadata.ocId,"serverUrl":metadata.serverUrl])
+        }
+    }
+    
+    func uploadProgress(_ progress: Double, totalBytes: Int64, totalBytesExpected: Int64, fileName: String, ServerUrl: String, session: URLSession, task: URLSessionTask) {
+        delegate?.uploadProgress?(progress, totalBytes: totalBytes, totalBytesExpected: totalBytesExpected, fileName: fileName, ServerUrl: ServerUrl, session: session, task: task)
+        
+        if let metadata = NCManageDatabase.sharedInstance.getMetadataInSessionFromFileName(fileName, serverUrl: ServerUrl, taskIdentifier: task.taskIdentifier) {
+                        
+            NotificationCenter.default.post(name: Notification.Name.init(rawValue: k_notificationCenter_progressTask), object: nil, userInfo: ["account":metadata.account, "ocId":metadata.ocId, "serverUrl":ServerUrl, "status":NSNumber(value: k_metadataStatusInDownload), "progress":NSNumber(value: progress), "totalBytes":NSNumber(value: totalBytes), "totalBytesExpected":NSNumber(value: totalBytesExpected)])
+        }
+    }
+    
+    func uploadComplete(fileName: String, serverUrl: String, ocId: String?, etag: String?, date: NSDate?, size: Int64, description: String?, error: Error?, statusCode: Int) {
+        if delegate != nil {
+            delegate?.uploadComplete?(fileName: fileName, serverUrl: serverUrl, ocId: ocId, etag: etag, date: date, size:size, description: description, error: error, statusCode: statusCode)
+        } else {
+            
+            if error == nil && statusCode >= 200 && statusCode < 300 {
+                
+            } else if error != nil && (error! as NSError).code == -999 {
+                
+            }
+        }
+    }
+    
+    
     //MARK: - WebDav Read file, folder
     
     @objc func readFolder(serverUrl: String, account: String, completion: @escaping (_ account: String, _ metadataFolder: tableMetadata?, _ metadatas: [tableMetadata]?, _ errorCode: Int, _ errorDescription: String)->()) {