浏览代码

coding

Signed-off-by: marinofaggiana <ios@nextcloud.com>
marinofaggiana 2 年之前
父节点
当前提交
53385c5a99

+ 5 - 1
iOSClient/Data/NCManageDatabase+Metadata.swift

@@ -221,9 +221,11 @@ extension NCManageDatabase {
         return metadata
     }
 
-    @objc func addMetadata(_ metadata: tableMetadata) {
+    @discardableResult
+    @objc func addMetadata(_ metadata: tableMetadata) -> tableMetadata? {
 
         let realm = try! Realm()
+        let returnMetadata = tableMetadata.init(value: metadata)
 
         do {
             try realm.safeWrite {
@@ -231,7 +233,9 @@ extension NCManageDatabase {
             }
         } catch let error {
             NCCommunicationCommon.shared.writeLog("Could not write to database: \(error)")
+            return nil
         }
+        return returnMetadata
     }
 
     @objc func addMetadatas(_ metadatas: [tableMetadata]) {

+ 0 - 1
iOSClient/Main/Create cloud/NCCreateFormUploadConflict.swift

@@ -280,7 +280,6 @@ extension NCCreateFormUploadConflictDelegate {
             }
         }
 
-
         dismiss(animated: true) {
             self.delegate?.dismissCreateFormUploadConflict(metadatas: self.metadatasNOConflict)
         }

+ 1 - 0
iOSClient/Networking/NCAutoUpload.swift

@@ -168,6 +168,7 @@ class NCAutoUpload: NSObject {
                             NCCommunicationCommon.shared.writeLog("Automatic upload added \(metadata.fileNameView) with Identifier \(metadata.assetLocalIdentifier)")
                             NCManageDatabase.shared.addPhotoLibrary([asset], account: account.account)
                         }
+                        metadatas.append(metadata)
                     }
                 }
 

+ 2 - 2
iOSClient/Networking/NCNetworking.swift

@@ -705,7 +705,7 @@ import Photos
         }
     }
 
-    func getOcIdInBackgroundSession(completion: @escaping (_ listOcId: [String]) -> Void) {
+    func getOcIdInBackgroundSession(queue: DispatchQueue = .main, completion: @escaping (_ listOcId: [String]) -> Void) {
 
         var listOcId: [String] = []
 
@@ -717,7 +717,7 @@ import Photos
                 for task in tasks {
                     listOcId.append(task.description)
                 }
-                completion(listOcId)
+                queue.async { completion(listOcId) }
             })
         })
     }

+ 63 - 7
iOSClient/Networking/NCNetworkingProcessUpload.swift

@@ -57,15 +57,15 @@ class NCNetworkingProcessUpload: NSObject {
 
         var counterUpload: Int = 0
         let sessionSelectors = [NCGlobal.shared.selectorUploadFile, NCGlobal.shared.selectorUploadAutoUpload, NCGlobal.shared.selectorUploadAutoUploadAll]
-
         let metadatasUpload = NCManageDatabase.shared.getMetadatas(predicate: NSPredicate(format: "status == %d OR status == %d", NCGlobal.shared.metadataStatusInUpload, NCGlobal.shared.metadataStatusUploading))
+        let chunckSize = CCUtility.getChunkSize() * 1000000
         counterUpload = metadatasUpload.count
 
         stopTimer()
 
         print("[LOG] PROCESS-UPLOAD \(counterUpload)")
 
-        NCNetworking.shared.getOcIdInBackgroundSession { listOcId in
+        NCNetworking.shared.getOcIdInBackgroundSession(queue: DispatchQueue.global(qos: .background), completion: { listOcId in
 
             for sessionSelector in sessionSelectors {
                 if counterUpload < self.maxConcurrentOperationUpload {
@@ -78,6 +78,9 @@ class NCNetworkingProcessUpload: NSObject {
 
                     for metadata in metadatas {
 
+                        var metadataForUpload: tableMetadata?
+                        var metadataLivePhotoForUpload: tableMetadata?
+
                         // Different account
                         if self.appDelegate.account != metadata.account {
                             NCCommunicationCommon.shared.writeLog("Process auto upload skipped file: \(metadata.serverUrl)/\(metadata.fileNameView) on account: \(metadata.account), because the actual account is \(self.appDelegate.account).")
@@ -103,6 +106,57 @@ class NCNetworkingProcessUpload: NSObject {
                             }
                         }
 
+                        // OK EXTRACT
+                        if !metadata.assetLocalIdentifier.isEmpty {
+                            CCUtility.extractImageVideoFromAssetLocalIdentifier(forUpload: metadata) { extractMetadata, fileNamePath in
+                                if let metadata = extractMetadata {
+                                    let toPath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
+                                    NCUtilityFileSystem.shared.moveFileInBackground(atPath: fileNamePath!, toPath: toPath)
+                                    metadata.size = NCUtilityFileSystem.shared.getFileSize(filePath: toPath)
+                                    // DETECT IF CHUNCK
+                                    if chunckSize > 0 && metadata.size > chunckSize {
+                                        metadata.chunk = true
+                                        metadata.session = NCCommunicationCommon.shared.sessionIdentifierUpload
+                                    }
+                                    // DETECT IF E2EE
+                                    if CCUtility.isFolderEncrypted(metadata.serverUrl, e2eEncrypted: metadata.e2eEncrypted, account: metadata.account, urlBase: metadata.urlBase) {
+                                        metadata.e2eEncrypted = true
+                                    }
+                                    // update
+                                    metadataForUpload = NCManageDatabase.shared.addMetadata(metadata)
+                                    if metadata.livePhoto {
+                                        let fetchAssets = PHAsset.fetchAssets(withLocalIdentifiers: [metadata.assetLocalIdentifier], options: nil)
+                                        if fetchAssets.count > 0 {
+                                            let ocId = NSUUID().uuidString
+                                            let fileName = (metadata.fileName as NSString).deletingPathExtension + ".mov"
+                                            let filePath = CCUtility.getDirectoryProviderStorageOcId(ocId, fileNameView: fileName)!
+                                            CCUtility.extractLivePhotoAsset(fetchAssets.firstObject, filePath: filePath) { url in
+                                                if url != nil {
+                                                    let metadataLivePhoto = NCManageDatabase.shared.createMetadata(account: metadata.account, user: metadata.user, userId: metadata.userId, fileName: fileName, fileNameView: fileName, ocId: ocId, serverUrl: metadata.serverUrl, urlBase: metadata.urlBase, url: "", contentType: "", isLivePhoto: true)
+                                                    metadataLivePhoto.classFile = NCCommunicationCommon.typeClassFile.video.rawValue
+                                                    metadataLivePhoto.e2eEncrypted = metadata.e2eEncrypted
+                                                    metadataLivePhoto.isAutoupload = metadata.isAutoupload
+                                                    metadataLivePhoto.session = metadata.session
+                                                    metadataLivePhoto.sessionSelector = metadata.sessionSelector
+                                                    metadataLivePhoto.size = NCUtilityFileSystem.shared.getFileSize(filePath: filePath)
+                                                    metadataLivePhoto.status = metadata.status
+                                                    if chunckSize > 0 && metadataLivePhoto.size > chunckSize {
+                                                        metadataLivePhoto.chunk = true
+                                                        metadataLivePhoto.session = NCCommunicationCommon.shared.sessionIdentifierUpload
+                                                    }
+                                                    metadataLivePhotoForUpload = NCManageDatabase.shared.addMetadata(metadataLivePhoto)
+                                                }
+                                            }
+                                        }
+                                    }
+                                } else {
+                                    NCManageDatabase.shared.deleteMetadata(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
+                                }
+                            }
+                        } else {
+
+                        }
+
                         // Chunk 
                         if metadata.chunk && UIApplication.shared.applicationState == .active {
 //                            if metadata.session == NCNetworking.shared.sessionIdentifierBackgroundWWan && metadata.isAutoupload && NCNetworking.shared.networkReachability != NCCommunicationCommon.typeReachability.reachableEthernetOrWiFi {
@@ -164,14 +218,16 @@ class NCNetworkingProcessUpload: NSObject {
             }
              
             // verify delete Asset Local Identifiers in auto upload (DELETE Photos album)
-            if (counterUpload == 0 && !self.appDelegate.isPasscodePresented()) {
-                self.deleteAssetLocalIdentifiers(account: self.appDelegate.account) {
+            DispatchQueue.main.async {
+                if (counterUpload == 0 && !self.appDelegate.isPasscodePresented()) {
+                    self.deleteAssetLocalIdentifiers(account: self.appDelegate.account) {
+                        self.startTimer()
+                    }
+                } else {
                     self.startTimer()
                 }
-            } else {
-                self.startTimer()
             }
-        }
+        })
     }
 
     private func deleteAssetLocalIdentifiers(account: String, completition: @escaping () -> Void) {