浏览代码

coding

Signed-off-by: marinofaggiana <ios@nextcloud.com>
marinofaggiana 2 年之前
父节点
当前提交
2e5cdd7cba
共有 2 个文件被更改,包括 26 次插入26 次删除
  1. 3 21
      iOSClient/Networking/NCNetworkingProcessUpload.swift
  2. 23 5
      iOSClient/Utility/NCUtility.swift

+ 3 - 21
iOSClient/Networking/NCNetworkingProcessUpload.swift

@@ -221,27 +221,9 @@ class NCNetworkingProcessUpload: NSObject {
 
         let fetchAssets = PHAsset.fetchAssets(withLocalIdentifiers: [metadata.assetLocalIdentifier], options: nil)
         if metadata.livePhoto, fetchAssets.count > 0  {
-            let ocId = NSUUID().uuidString
-            let fileName = (metadata.fileName as NSString).deletingPathExtension + ".mov"
-            let fileNamePath = CCUtility.getDirectoryProviderStorageOcId(ocId, fileNameView: fileName)!
-            NCUtility.shared.extractLivePhoto(asset: fetchAssets.firstObject,  fileNamePath: fileNamePath, queue: queue) { error in
-                if !error {
-                    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.isExtractFile = true
-                    metadataLivePhoto.session = metadata.session
-                    metadataLivePhoto.sessionSelector = metadata.sessionSelector
-                    metadataLivePhoto.size = NCUtilityFileSystem.shared.getFileSize(filePath: fileNamePath)
-                    metadataLivePhoto.status = metadata.status
-                    if chunckSize > 0 && metadataLivePhoto.size > chunckSize {
-                        metadataLivePhoto.chunk = true
-                        metadataLivePhoto.session = NCCommunicationCommon.shared.sessionIdentifierUpload
-                    }
-                    if let metadata = NCManageDatabase.shared.addMetadata(metadataLivePhoto) {
-                        metadatas.append(metadata)
-                    }
+            NCUtility.shared.createMetadataLivePhotoFromMetadata(metadata, asset: fetchAssets.firstObject, queue: queue) { metadata in
+                if let metadata = metadata, let metadata = NCManageDatabase.shared.addMetadata(metadata) {
+                    metadatas.append(metadata)
                 }
                 semaphore.continue()
             }

+ 23 - 5
iOSClient/Utility/NCUtility.swift

@@ -513,15 +513,19 @@ class NCUtility: NSObject {
         }
     }
 
-    func extractLivePhoto(asset: PHAsset?, fileNamePath: String, queue: DispatchQueue, completion: @escaping (_ error: Bool) -> ()) {
+    func createMetadataLivePhotoFromMetadata(_ metadata: tableMetadata, asset: PHAsset?, queue: DispatchQueue, completion: @escaping (_ metadata: tableMetadata?) -> ()) {
 
-        guard let asset = asset else { return queue.async { completion(true) }}
+        guard let asset = asset else { return queue.async { completion(nil) }}
         let options = PHLivePhotoRequestOptions()
         options.deliveryMode = PHImageRequestOptionsDeliveryMode.fastFormat
         options.isNetworkAccessAllowed = true
+        let chunckSize = CCUtility.getChunkSize() * 1000000
+        let ocId = NSUUID().uuidString
+        let fileName = (metadata.fileName as NSString).deletingPathExtension + ".mov"
+        let fileNamePath = CCUtility.getDirectoryProviderStorageOcId(ocId, fileNameView: fileName)!
 
         PHImageManager.default().requestLivePhoto(for: asset, targetSize: UIScreen.main.bounds.size, contentMode: PHImageContentMode.default, options: options) { livePhoto, info in
-            guard let livePhoto = livePhoto else { return queue.async { completion(true) }}
+            guard let livePhoto = livePhoto else { return queue.async { completion(nil) }}
             var videoResource: PHAssetResource?
             for resource in PHAssetResource.assetResources(for: livePhoto) {
                 if resource.type == PHAssetResourceType.pairedVideo {
@@ -529,10 +533,24 @@ class NCUtility: NSObject {
                     break
                 }
             }
-            guard let videoResource = videoResource else { return queue.async { completion(true) }}
+            guard let videoResource = videoResource else { return queue.async { completion(nil) }}
             NCUtilityFileSystem.shared.deleteFile(filePath: fileNamePath)
             PHAssetResourceManager.default().writeData(for: videoResource, toFile: URL(fileURLWithPath: fileNamePath), options: nil) { error in
-                queue.async { completion(error != nil) }
+                if error != nil { return queue.async { completion(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.isExtractFile = true
+                metadataLivePhoto.session = metadata.session
+                metadataLivePhoto.sessionSelector = metadata.sessionSelector
+                metadataLivePhoto.size = NCUtilityFileSystem.shared.getFileSize(filePath: fileNamePath)
+                metadataLivePhoto.status = metadata.status
+                if chunckSize > 0 && metadataLivePhoto.size > chunckSize {
+                    metadataLivePhoto.chunk = true
+                    metadataLivePhoto.session = NCCommunicationCommon.shared.sessionIdentifierUpload
+                }
+                return queue.async { completion(NCManageDatabase.shared.addMetadata(metadataLivePhoto)) }
             }
         }
     }