Эх сурвалжийг харах

Update library NCCommunication

Signed-off-by: marinofaggiana <marino@marinofaggiana.com>
marinofaggiana 3 жил өмнө
parent
commit
5f43e01ab6

+ 86 - 89
iOSClient/Networking/NCNetworkingChunkedUpload.swift

@@ -65,113 +65,110 @@ extension NCNetworking {
                     
                 NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterUploadStartFile, userInfo: ["ocId": metadata.ocId])
                 
-                DispatchQueue.global(qos: .background).async {
+                for fileName in filesNames {
                         
-                    for fileName in filesNames {
-                            
-                        let serverUrlFileName = chunkFolderPath + "/" + fileName
-                        let fileNameChunkLocalPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: fileName)!
-                        
-                        var size: Int64?
-                        if let tableChunk = NCManageDatabase.shared.getChunk(account: metadata.account, fileName: fileName) {
-                            size = tableChunk.size - NCUtilityFileSystem.shared.getFileSize(filePath: fileNameChunkLocalPath)
-                        }
+                    let serverUrlFileName = chunkFolderPath + "/" + fileName
+                    let fileNameChunkLocalPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: fileName)!
+                    
+                    var size: Int64?
+                    if let tableChunk = NCManageDatabase.shared.getChunk(account: metadata.account, fileName: fileName) {
+                        size = tableChunk.size - NCUtilityFileSystem.shared.getFileSize(filePath: fileNameChunkLocalPath)
+                    }
+                                            
+                    let semaphore = Semaphore()
                                                 
-                        let semaphore = Semaphore()
-                                                    
-                        NCCommunication.shared.upload(serverUrlFileName: serverUrlFileName, fileNameLocalPath: fileNameChunkLocalPath, requestHandler: { (request) in
-                                
-                            self.uploadRequest[fileNameLocalPath] = request
+                    NCCommunication.shared.upload(serverUrlFileName: serverUrlFileName, fileNameLocalPath: fileNameChunkLocalPath, requestHandler: { (request) in
                             
-                        }, taskHandler: { (task) in
-                            
-                            NCManageDatabase.shared.setMetadataSession(ocId: metadata.ocId, sessionError: "", sessionTaskIdentifier: task.taskIdentifier, status: NCGlobal.shared.metadataStatusUploading)
-                            
-                            NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterUploadStartFile, userInfo: ["ocId":metadata.ocId])
-                            
-                            NCCommunicationCommon.shared.writeLog("Upload chunk: " + fileName)
-                           
-                        }, progressHandler: { (progress) in
+                        self.uploadRequest[fileNameLocalPath] = request
+                        
+                    }, taskHandler: { (task) in
+                        
+                        NCManageDatabase.shared.setMetadataSession(ocId: metadata.ocId, sessionError: "", sessionTaskIdentifier: task.taskIdentifier, status: NCGlobal.shared.metadataStatusUploading)
+                        
+                        NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterUploadStartFile, userInfo: ["ocId":metadata.ocId])
+                        
+                        NCCommunicationCommon.shared.writeLog("Upload chunk: " + fileName)
+                       
+                    }, progressHandler: { (progress) in
+                        
+                        if let size = size {
                             
-                            if let size = size {
+                            let totalBytesExpected = size + progress.completedUnitCount
+                            let totalBytes = metadata.size
+                            let fractionCompleted = Float(totalBytesExpected) / Float(totalBytes)
                                 
-                                let totalBytesExpected = size + progress.completedUnitCount
-                                let totalBytes = metadata.size
-                                let fractionCompleted = Float(totalBytesExpected) / Float(totalBytes)
-                                    
-                                NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterProgressTask, object: nil, userInfo: ["account":metadata.account, "ocId":metadata.ocId, "fileName":metadata.fileName, "serverUrl":metadata.serverUrl, "status":NSNumber(value: NCGlobal.shared.metadataStatusInUpload), "progress":NSNumber(value: fractionCompleted), "totalBytes":NSNumber(value: totalBytes), "totalBytesExpected":NSNumber(value: totalBytesExpected)])
-                            }
-                            
-                        }) { (_, _, _, _, _, _, _, errorCode, errorDescription) in
-                               
-                            self.uploadRequest[fileNameLocalPath] = nil
-                            uploadErrorCode = errorCode
-                            uploadErrorDescription = errorDescription
-                            semaphore.continue()
-                        }
-                            
-                        semaphore.wait()
-                            
-                        if uploadErrorCode == 0 {
-                            NCManageDatabase.shared.deleteChunk(account: metadata.account, ocId: metadata.ocId, fileName: fileName)
-                        } else {
-                            break
+                            NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterProgressTask, object: nil, userInfo: ["account":metadata.account, "ocId":metadata.ocId, "fileName":metadata.fileName, "serverUrl":metadata.serverUrl, "status":NSNumber(value: NCGlobal.shared.metadataStatusInUpload), "progress":NSNumber(value: fractionCompleted), "totalBytes":NSNumber(value: totalBytes), "totalBytesExpected":NSNumber(value: totalBytesExpected)])
                         }
+                        
+                    }) { (_, _, _, _, _, _, _, errorCode, errorDescription) in
+                           
+                        self.uploadRequest[fileNameLocalPath] = nil
+                        uploadErrorCode = errorCode
+                        uploadErrorDescription = errorDescription
+                        semaphore.continue()
                     }
                         
+                    semaphore.wait()
+                        
                     if uploadErrorCode == 0 {
-                            
-                        // Assembling the chunks
-                        let serverUrlFileNameSource = chunkFolderPath + "/.file"
-                        let pathServerUrl = CCUtility.returnPathfromServerUrl(metadata.serverUrl, urlBase: metadata.urlBase, account: metadata.account)!
-                        let serverUrlFileNameDestination = metadata.urlBase + "/" + NCUtilityFileSystem.shared.getWebDAV(account: metadata.account) + "/files/" + metadata.userId + pathServerUrl + "/" + metadata.fileName
+                        NCManageDatabase.shared.deleteChunk(account: metadata.account, ocId: metadata.ocId, fileName: fileName)
+                    } else {
+                        break
+                    }
+                }
+                    
+                if uploadErrorCode == 0 {
                         
-                        var addCustomHeaders: [String:String] = [:]
-                        let creationDate = "\(metadata.creationDate.timeIntervalSince1970)"
-                        let modificationDate = "\(metadata.date.timeIntervalSince1970)"
-                            
-                        addCustomHeaders["X-OC-CTime"] = creationDate
-                        addCustomHeaders["X-OC-MTime"] = modificationDate
+                    // Assembling the chunks
+                    let serverUrlFileNameSource = chunkFolderPath + "/.file"
+                    let pathServerUrl = CCUtility.returnPathfromServerUrl(metadata.serverUrl, urlBase: metadata.urlBase, account: metadata.account)!
+                    let serverUrlFileNameDestination = metadata.urlBase + "/" + NCUtilityFileSystem.shared.getWebDAV(account: metadata.account) + "/files/" + metadata.userId + pathServerUrl + "/" + metadata.fileName
+                    
+                    var addCustomHeaders: [String:String] = [:]
+                    let creationDate = "\(metadata.creationDate.timeIntervalSince1970)"
+                    let modificationDate = "\(metadata.date.timeIntervalSince1970)"
+                        
+                    addCustomHeaders["X-OC-CTime"] = creationDate
+                    addCustomHeaders["X-OC-MTime"] = modificationDate
 
-                        NCCommunication.shared.moveFileOrFolder(serverUrlFileNameSource: serverUrlFileNameSource, serverUrlFileNameDestination: serverUrlFileNameDestination, overwrite: true, addCustomHeaders: addCustomHeaders) { (_, errorCode, errorDescription) in
-                                       
-                            NCCommunicationCommon.shared.writeLog("Assembling chunk with error code: \(errorCode)")
-                            
-                            if errorCode == 0 {
-                                                                
-                                let serverUrl = metadata.serverUrl
+                    NCCommunication.shared.moveFileOrFolder(serverUrlFileNameSource: serverUrlFileNameSource, serverUrlFileNameDestination: serverUrlFileNameDestination, overwrite: true, addCustomHeaders: addCustomHeaders) { (_, errorCode, errorDescription) in
+                                   
+                        NCCommunicationCommon.shared.writeLog("Assembling chunk with error code: \(errorCode)")
+                        
+                        if errorCode == 0 {
+                                                            
+                            let serverUrl = metadata.serverUrl
 
-                                NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
-                                NCManageDatabase.shared.deleteChunks(account: metadata.account, ocId: metadata.ocId)
-                                NCUtilityFileSystem.shared.deleteFile(filePath: directoryProviderStorageOcId)
+                            NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
+                            NCManageDatabase.shared.deleteChunks(account: metadata.account, ocId: metadata.ocId)
+                            NCUtilityFileSystem.shared.deleteFile(filePath: directoryProviderStorageOcId)
 
-                                self.readFile(serverUrlFileName: serverUrlFileNameDestination, account: metadata.account) { (_, metadata, _, _) in
-                                        
-                                    if errorCode == 0, let metadata = metadata {
-                                        
-                                        NCManageDatabase.shared.addMetadata(metadata)
-                                        NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource, userInfo: ["serverUrl":serverUrl])
-                                        
-                                    } else {
-                                        
-                                        NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSourceNetworkForced, userInfo: ["serverUrl": serverUrl])
-                                    }
+                            self.readFile(serverUrlFileName: serverUrlFileNameDestination, account: metadata.account) { (_, metadata, _, _) in
+                                    
+                                if errorCode == 0, let metadata = metadata {
                                     
-                                    completion(errorCode, errorDescription)
+                                    NCManageDatabase.shared.addMetadata(metadata)
+                                    NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource, userInfo: ["serverUrl":serverUrl])
+                                    
+                                } else {
+                                    
+                                    NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSourceNetworkForced, userInfo: ["serverUrl": serverUrl])
                                 }
                                 
-                            } else {
-                                
-                                self.uploadChunkFileError(metadata: metadata, chunkFolderPath: chunkFolderPath, directoryProviderStorageOcId: directoryProviderStorageOcId, errorCode: errorCode, errorDescription: errorDescription)
                                 completion(errorCode, errorDescription)
                             }
+                            
+                        } else {
+                            
+                            self.uploadChunkFileError(metadata: metadata, chunkFolderPath: chunkFolderPath, directoryProviderStorageOcId: directoryProviderStorageOcId, errorCode: errorCode, errorDescription: errorDescription)
+                            completion(errorCode, errorDescription)
                         }
-                                                        
-                    } else {
-                                                            
-                        self.uploadChunkFileError(metadata: metadata, chunkFolderPath: chunkFolderPath, directoryProviderStorageOcId: directoryProviderStorageOcId, errorCode: uploadErrorCode, errorDescription: uploadErrorDescription)
-                        completion(errorCode, errorDescription)
                     }
+                                                    
+                } else {
+                                                        
+                    self.uploadChunkFileError(metadata: metadata, chunkFolderPath: chunkFolderPath, directoryProviderStorageOcId: directoryProviderStorageOcId, errorCode: uploadErrorCode, errorDescription: uploadErrorDescription)
+                    completion(errorCode, errorDescription)
                 }
                 
             } else {
@@ -184,12 +181,12 @@ extension NCNetworking {
     
     private func createChunkedFolder(chunkFolderPath: String, account: String, completion: @escaping (_ errorCode: Int, _ errorDescription: String)->()) {
         
-        NCCommunication.shared.readFileOrFolder(serverUrlFileName: chunkFolderPath, depth: "0", showHiddenFiles: CCUtility.getShowHiddenFiles()) { (_, _, _, errorCode, errorDescription) in
+        NCCommunication.shared.readFileOrFolder(serverUrlFileName: chunkFolderPath, depth: "0", showHiddenFiles: CCUtility.getShowHiddenFiles(), queue: NCGlobal.shared.backgroundQueue) { (_, _, _, errorCode, errorDescription) in
         
             if errorCode == 0 {
                 completion(0, "")
             } else if errorCode == NCGlobal.shared.errorResourceNotFound {
-                NCCommunication.shared.createFolder(chunkFolderPath) { (_, _, _, errorCode, errorDescription) in
+                NCCommunication.shared.createFolder(chunkFolderPath, queue: NCGlobal.shared.backgroundQueue) { (_, _, _, errorCode, errorDescription) in
                     completion(errorCode, errorDescription)
                 }
             } else {

+ 10 - 7
iOSClient/Shares/NCShares.swift

@@ -74,11 +74,13 @@ class NCShares: NCCollectionViewCommon  {
         collectionView?.reloadData()
                     
         // Shares network
-        NCCommunication.shared.readShares { (account, shares, errorCode, ErrorDescription) in
-                
-            self.refreshControl.endRefreshing()
-            self.isReloadDataSourceNetworkInProgress = false
+        NCCommunication.shared.readShares(queue: NCGlobal.shared.backgroundQueue) { (account, shares, errorCode, ErrorDescription) in
                 
+            DispatchQueue.main.async {
+                self.refreshControl.endRefreshing()
+                self.isReloadDataSourceNetworkInProgress = false
+            }
+            
             if errorCode == 0 {
                     
                 NCManageDatabase.shared.deleteTableShare(account: account)
@@ -86,13 +88,14 @@ class NCShares: NCCollectionViewCommon  {
                     NCManageDatabase.shared.addShare(urlBase: self.appDelegate.urlBase, account: account, shares: shares!)
                 }
                 self.appDelegate.shares = NCManageDatabase.shared.getTableShares(account: account)
-                    
                 self.reloadDataSource()
                     
             } else {
                     
-                self.collectionView?.reloadData()
-                NCContentPresenter.shared.messageNotification("_share_", description: ErrorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
+                DispatchQueue.main.async {
+                    self.collectionView?.reloadData()
+                    NCContentPresenter.shared.messageNotification("_share_", description: ErrorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
+                }
             }
         }
     }

+ 5 - 3
iOSClient/Trash/NCTrash.swift

@@ -416,8 +416,7 @@ extension NCTrash {
     
     @objc func loadListingTrash() {
         
-        NCCommunication.shared.listingTrash(showHiddenFiles: false) { (account, items, errorCode, errorDescription) in
-            self.refreshControl.endRefreshing()
+        NCCommunication.shared.listingTrash(showHiddenFiles: false, queue: NCGlobal.shared.backgroundQueue) { (account, items, errorCode, errorDescription) in
          
             if errorCode == 0 && account == self.appDelegate.account {
                 NCManageDatabase.shared.deleteTrash(filePath: self.trashPath, account: self.appDelegate.account)
@@ -428,7 +427,10 @@ extension NCTrash {
                 print("[LOG] It has been changed user during networking process, error.")
             }
             
-            self.reloadDataSource()
+            DispatchQueue.main.async {
+                self.refreshControl.endRefreshing()
+                self.reloadDataSource()
+            }
         }
     }