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

improved chunks

Signed-off-by: Marino Faggiana <8616947+marinofaggiana@users.noreply.github.com>
Marino Faggiana 1 жил өмнө
parent
commit
53e57a6777

+ 1 - 1
Share/NCShareExtension.swift

@@ -351,7 +351,7 @@ extension NCShareExtension {
         hud.textLabel.text = NSLocalizedString("_upload_file_", comment: "") + " \(counterUploaded + 1) " + NSLocalizedString("_of_", comment: "") + " \(filesName.count)"
         hud.show(in: self.view)
 
-        NCNetworking.shared.upload(metadata: metadata, uploadE2EEDelegate: self) {
+        NCNetworking.shared.upload(metadata: metadata, uploadE2EEDelegate: self, hudView: self.view) {
             self.hud.progress = 0
         } progressHandler: { _, _, fractionCompleted in
             self.hud.progress = Float(fractionCompleted)

+ 7 - 6
iOSClient/Networking/E2EE/NCNetworkingE2EEUpload.swift

@@ -201,14 +201,17 @@ class NCNetworkingE2EEUpload: NSObject {
 
         if metadata.chunk > 0 {
             return await withCheckedContinuation({ continuation in
-                NCNetworking.shared.uploadChunkFile(metadata: metadata, withUploadComplete: false, customHeaders: ["e2e-token": e2eToken]) {
-#if !EXTENSION
+                NCNetworking.shared.uploadChunkFile(metadata: metadata, withUploadComplete: false, customHeaders: ["e2e-token": e2eToken]) { num in
+
+                } counterChunk: { counter in
+
+                } start: {
                     DispatchQueue.main.async { self.hud.dismiss() }
-#endif
                     uploadE2EEDelegate?.start()
-                } progressHandler: { totalBytesExpected, totalBytes, fractionCompleted in
+                } progressHandler: {totalBytesExpected, totalBytes, fractionCompleted in
                     uploadE2EEDelegate?.uploadE2EEProgress(totalBytesExpected, totalBytes, fractionCompleted)
                 } completion: { _, file, afError, error in
+                    DispatchQueue.main.async { self.hud.dismiss() }
                     continuation.resume(returning: (ocId: file?.ocId, etag: file?.etag, date: file?.date, afError: afError, error: error))
                 }
             })
@@ -218,9 +221,7 @@ class NCNetworkingE2EEUpload: NSObject {
             let fileNameLocalPath = utilityFileSystem.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileName)
             return await withCheckedContinuation({ continuation in
                 NCNetworking.shared.uploadFile(metadata: metadata, fileNameLocalPath: fileNameLocalPath, withUploadComplete: false, customHeaders: ["e2e-token": e2eToken]) {
-#if !EXTENSION
                     DispatchQueue.main.async { self.hud.dismiss() }
-#endif
                     uploadE2EEDelegate?.start()
                 } progressHandler: { totalBytesExpected, totalBytes, fractionCompleted in
                     uploadE2EEDelegate?.uploadE2EEProgress(totalBytesExpected, totalBytes, fractionCompleted)

+ 39 - 22
iOSClient/Networking/NCNetworking.swift

@@ -386,12 +386,14 @@ class NCNetworking: NSObject, NKCommonDelegate {
 
     func upload(metadata: tableMetadata,
                 uploadE2EEDelegate: uploadE2EEDelegate? = nil,
+                hudView: UIView?,
                 start: @escaping () -> Void = { },
                 progressHandler: @escaping (_ totalBytesExpected: Int64, _ totalBytes: Int64, _ fractionCompleted: Double) -> Void = { _, _, _ in },
                 completion: @escaping (_ error: NKError) -> Void = { _ in }) {
 
         let hud = JGProgressHUD()
         let metadata = tableMetadata.init(value: metadata)
+        var numChunks: Int = 0
         NextcloudKit.shared.nkCommonInstance.writeLog("[INFO] Upload file \(metadata.fileNameView) with Identifier \(metadata.assetLocalIdentifier) with size \(metadata.size) [CHUNK \(metadata.chunk), E2EE \(metadata.isDirectoryE2EE)]")
 
         if metadata.isDirectoryE2EE {
@@ -402,20 +404,30 @@ class NCNetworking: NSObject, NKCommonDelegate {
             }
 #endif
         } else if metadata.chunk > 0 {
-#if !EXTENSION
-            DispatchQueue.main.async {
-                guard let appDelegate = UIApplication.shared.delegate as? AppDelegate, let hudView = appDelegate.window?.rootViewController?.view else {
-                    return completion(.success)
+                if let hudView {
+                    DispatchQueue.main.async {
+                        hud.indicatorView = JGProgressHUDRingIndicatorView()
+                        if let indicatorView = hud.indicatorView as? JGProgressHUDRingIndicatorView {
+                            indicatorView.ringWidth = 1.5
+                        }
+                        hud.tapOnHUDViewBlock = { _ in
+                            NotificationCenter.default.postOnMainThread(name: "NextcloudKit.chunkedFile.stop")
+                        }
+                        hud.textLabel.text = NSLocalizedString("_wait_file_preparation_", comment: "")
+                        hud.detailTextLabel.text = NSLocalizedString("_tap_to_cancel_", comment: "")
+                        hud.show(in: hudView)
+                    }
                 }
-                hud.textLabel.text = NSLocalizedString("_wait_file_preparation_", comment: "")
-                hud.show(in: hudView)
-            }
-#endif
-            uploadChunkFile(metadata: metadata) {
-#if !EXTENSION
+            uploadChunkFile(metadata: metadata) { num in
+                numChunks = num
+            } counterChunk: { counter in
+                DispatchQueue.main.async {
+                    hud.progress = Float(counter) / Float(numChunks)
+                }
+            } start: {
                 DispatchQueue.main.async { hud.dismiss() }
-#endif
             } completion: { _, _, _, error in
+                DispatchQueue.main.async { hud.dismiss() }
                 completion(error)
             }
         } else if metadata.session == NextcloudKit.shared.nkCommonInstance.sessionIdentifierUpload {
@@ -483,6 +495,8 @@ class NCNetworking: NSObject, NKCommonDelegate {
     func uploadChunkFile(metadata: tableMetadata,
                          withUploadComplete: Bool = true,
                          customHeaders: [String: String]? = nil,
+                         numChunks: @escaping (_ num: Int) -> Void = { _ in },
+                         counterChunk: @escaping (_ counter: Int) -> Void = { _ in },
                          start: @escaping () -> Void = { },
                          progressHandler: @escaping (_ totalBytesExpected: Int64, _ totalBytes: Int64, _ fractionCompleted: Double) -> Void = { _, _, _ in },
                          completion: @escaping (_ account: String, _ file: NKFile?, _ afError: AFError?, _ error: NKError) -> Void) {
@@ -499,15 +513,15 @@ class NCNetworking: NSObject, NKCommonDelegate {
         }
         let options = NKRequestOptions(customHeader: customHeaders, queue: NextcloudKit.shared.nkCommonInstance.backgroundQueue)
 
-        NextcloudKit.shared.uploadChunk(directory: directory,
-                                        fileName: metadata.fileName,
-                                        date: metadata.date as Date,
-                                        creationDate: metadata.creationDate as Date,
-                                        serverUrl: metadata.serverUrl,
-                                        chunkFolder: chunkFolder,
-                                        filesChunk: filesChunk,
-                                        chunkSize: chunkSize,
-                                        options: options) { filesChunk in
+        NextcloudKit.shared.uploadChunk(directory: directory, fileName: metadata.fileName, date: metadata.date as Date, creationDate: metadata.creationDate as Date, serverUrl: metadata.serverUrl, chunkFolder: chunkFolder, filesChunk: filesChunk, chunkSize: chunkSize, options: options) { num in
+
+            numChunks(num)
+
+        } counterChunk: { counter in
+
+            counterChunk(counter)
+
+        } start: { filesChunk in
 
             start()
             NCManageDatabase.shared.addChunks(account: metadata.account, ocId: metadata.ocId, chunkFolder: chunkFolder, filesChunk: filesChunk)
@@ -558,17 +572,20 @@ class NCNetworking: NSObject, NKCommonDelegate {
             }
 
             switch error.errorCode {
-            case NKError.chunkResourceNotFound:
-                NCManageDatabase.shared.deleteChunks(account: account, ocId: metadata.ocId, directory: directory)
             case NKError.chunkNoEnoughMemory:
+                NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
                 NCManageDatabase.shared.deleteChunks(account: account, ocId: metadata.ocId, directory: directory)
                 NCContentPresenter().messageNotification("_chunk_enough_memory_", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: .error)
             case NKError.chunkCreateFolder:
+                NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
+                NCManageDatabase.shared.deleteChunks(account: account, ocId: metadata.ocId, directory: directory)
                 NCContentPresenter().messageNotification("_chunk_create_folder_", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: .error)
             case NKError.chunkFilesNull:
+                NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
                 NCManageDatabase.shared.deleteChunks(account: account, ocId: metadata.ocId, directory: directory)
                 NCContentPresenter().messageNotification("_chunk_files_null_", error: error, delay: NCGlobal.shared.dismissAfterSecond, type: .error)
             case NKError.chunkFileNull: // (fileSize == 0)
+                NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
                 NCManageDatabase.shared.deleteChunks(account: account, ocId: metadata.ocId, directory: directory)
             case NKError.chunkFileUpload:
                 if let afError, (afError.isExplicitlyCancelledError || sessionTaskFailedCode == NCGlobal.shared.errorExplicitlyCancelled ) {

+ 1 - 1
iOSClient/Networking/NCNetworkingProcessUpload.swift

@@ -174,7 +174,7 @@ class NCNetworkingProcessUpload: NSObject {
                                 }
 
                                 if let metadata = NCManageDatabase.shared.setMetadataStatus(ocId: metadata.ocId, status: NCGlobal.shared.metadataStatusInUpload) {
-                                    NCNetworking.shared.upload(metadata: metadata)
+                                    NCNetworking.shared.upload(metadata: metadata, hudView: self.appDelegate.window?.rootViewController?.view)
                                     if isInDirectoryE2EE || metadata.chunk > 0 {
                                         maxConcurrentOperationUpload = 1
                                     }

+ 1 - 1
iOSClient/Supporting Files/en.lproj/Localizable.strings

@@ -215,7 +215,7 @@
 "_replace_all_action_title_" = "Replace all";
 "_keep_both_for_all_action_title_" = "Keep both for all";
 "_more_action_title_" = "More Details";
-"_wait_file_preparation_" = "Please wait, upload file in preparation";
+"_wait_file_preparation_" = "Please wait, upload file in preparation";
 
 /* Background of the file listing view */
 "_use_as_background_"       = "Use it as a background";

+ 1 - 1
iOSClient/Transfers/NCTransfers.swift

@@ -142,7 +142,7 @@ class NCTransfers: NCCollectionViewCommon, NCTransferCellDelegate {
         cameraRoll.extractCameraRoll(from: metadata, viewController: self, hud: JGProgressHUD()) { metadatas in
             for metadata in metadatas {
                 if let metadata = NCManageDatabase.shared.setMetadataStatus(ocId: metadata.ocId, status: NCGlobal.shared.metadataStatusInUpload) {
-                    NCNetworking.shared.upload(metadata: metadata)
+                    NCNetworking.shared.upload(metadata: metadata, hudView: self.appDelegate.window?.rootViewController?.view)
                 }
             }
         }