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

move to NextcloudKit

Signed-off-by: marinofaggiana <marino.faggiana@nextcloud.com>
marinofaggiana 2 жил өмнө
parent
commit
424d14da47

+ 21 - 17
iOSClient/Main/NCFunctionCenter.swift

@@ -422,25 +422,29 @@ import Photos
         }
     }
 
-    func upload(fileName: String, serverUrlFileName: String, fileNameLocalPath: String, serverUrl: String, completion: @escaping () -> Void) {
-        NextcloudKit.shared.upload(serverUrlFileName: serverUrlFileName, fileNameLocalPath: fileNameLocalPath) { _ in
-        } progressHandler: { progress in
-        } completionHandler: { account, ocId, etag, _, _, _, error in
-            if error == .success && etag != nil && ocId != nil {
-                let toPath = CCUtility.getDirectoryProviderStorageOcId(ocId!, fileNameView: fileName)!
-                NCUtilityFileSystem.shared.moveFile(atPath: fileNameLocalPath, toPath: toPath)
-                NCManageDatabase.shared.addLocalFile(account: account, etag: etag!, ocId: ocId!, fileName: fileName)
-                NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSourceNetworkForced, userInfo: ["serverUrl": serverUrl])
-            } else {
-                NCContentPresenter.shared.showError(error: error)
-            }
-            completion()
-        }
-    }
-
     func pastePasteboard(serverUrl: String) {
         let parallelizer = ParallelWorker(n: 5, titleKey: "_uploading_", totalTasks: nil, hudView: appDelegate.window?.rootViewController?.view)
 
+        func uploadPastePasteboard(fileName: String, serverUrlFileName: String, fileNameLocalPath: String, serverUrl: String, completion: @escaping () -> Void) {
+            NextcloudKit.shared.upload(serverUrlFileName: serverUrlFileName, fileNameLocalPath: fileNameLocalPath) { request in
+                NCNetworking.shared.uploadRequest[fileNameLocalPath] = request
+            } progressHandler: { progress in
+            } completionHandler: { account, ocId, etag, _, _, _, afError, error in
+                NCNetworking.shared.uploadRequest.removeValue(forKey: fileNameLocalPath)
+                if error == .success && etag != nil && ocId != nil {
+                    let toPath = CCUtility.getDirectoryProviderStorageOcId(ocId!, fileNameView: fileName)!
+                    NCUtilityFileSystem.shared.moveFile(atPath: fileNameLocalPath, toPath: toPath)
+                    NCManageDatabase.shared.addLocalFile(account: account, etag: etag!, ocId: ocId!, fileName: fileName)
+                    NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSourceNetworkForced, userInfo: ["serverUrl": serverUrl])
+                } else if afError?.isExplicitlyCancelledError ?? false {
+                    print("cancel")
+                } else {
+                    NCContentPresenter.shared.showError(error: error)
+                }
+                completion()
+            }
+        }
+
         for (index, items) in UIPasteboard.general.items.enumerated() {
             for item in items {
                 let results = NKCommon.shared.getFileProperties(inUTI: item.key as CFString)
@@ -453,7 +457,7 @@ import Photos
                 let fileNameLocalPath = CCUtility.getDirectoryProviderStorageOcId(ocIdUpload, fileNameView: fileName)!
                 do { try data.write(to: URL(fileURLWithPath: fileNameLocalPath)) } catch { continue }
                 parallelizer.execute { completion in
-                    self.upload(fileName: fileName, serverUrlFileName: serverUrlFileName, fileNameLocalPath: fileNameLocalPath, serverUrl: serverUrl, completion: completion)
+                    uploadPastePasteboard(fileName: fileName, serverUrlFileName: serverUrlFileName, fileNameLocalPath: fileNameLocalPath, serverUrl: serverUrl, completion: completion)
                 }
             }
         }

+ 0 - 1
iOSClient/Networking/NCNetworking.swift

@@ -329,7 +329,6 @@ import Photos
         NextcloudKit.shared.download(serverUrlFileName: serverUrlFileName, fileNameLocalPath: fileNameLocalPath, queue: NKCommon.shared.backgroundQueue, requestHandler: { request in
 
             self.downloadRequest[fileNameLocalPath] = request
-            self.downloadRequest.removeValue(forKey: fileNameLocalPath)
 
             NCManageDatabase.shared.setMetadataSession(ocId: metadata.ocId, status: NCGlobal.shared.metadataStatusDownloading)
             NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDownloadStartFile, userInfo: ["ocId":metadata.ocId, "serverUrl": metadata.serverUrl, "account": metadata.account])

+ 7 - 2
iOSClient/Utility/ParallelWorker.swift

@@ -56,8 +56,13 @@ class ParallelWorker {
             hud.detailTextLabel.text = NSLocalizedString("_tap_to_cancel_", comment: "")
             hud.tapOnHUDViewBlock = { hud in
                 self.isCancelled = true
-                // Cancel all download
-                NCNetworking.shared.cancelAllDownloadTransfer()
+                // Cancel all download / upload
+                for uploadRequest in NCNetworking.shared.uploadRequest {
+                    uploadRequest.value.cancel()
+                }
+                for downloadRequest in NCNetworking.shared.downloadRequest {
+                    downloadRequest.value.cancel()
+                }
                 hud.dismiss()
             }
             self.hud = hud