Marino Faggiana 1 tahun lalu
induk
melakukan
880637eaca

+ 36 - 23
iOSClient/Main/NCPickerViewController.swift

@@ -150,7 +150,10 @@ class NCDocumentPickerViewController: NSObject, UIDocumentPickerDelegate {
 
         let ocId = NSUUID().uuidString
 
-        if isViewerMedia, let url = urls.first, let viewController = self.viewController {
+        if isViewerMedia,
+            let urlIn = urls.first,
+            let url = self.copySecurityScopedResource(url: urlIn, urlOut: FileManager.default.temporaryDirectory.appendingPathComponent(urlIn.lastPathComponent)),
+            let viewController = self.viewController {
 
             let fileName = url.lastPathComponent
             let metadata = NCManageDatabase.shared.createMetadata(account: appDelegate.account, user: appDelegate.user, userId: appDelegate.userId, fileName: fileName, fileNameView: fileName, ocId: ocId, serverUrl: "", urlBase: appDelegate.urlBase, url: url.path, contentType: "")
@@ -158,42 +161,52 @@ class NCDocumentPickerViewController: NSObject, UIDocumentPickerDelegate {
             NCViewer.shared.view(viewController: viewController, metadata: metadata, metadatas: [metadata], imageIcon: nil)
 
         } else {
-            for url in urls {
 
-                let serverUrl = appDelegate.activeServerUrl
-                let fileName = url.lastPathComponent
-                let atPath = url.path
-                let toPath = CCUtility.getDirectoryProviderStorageOcId(ocId, fileNameView: fileName)!
+            for urlIn in urls {
 
-                if NCUtilityFileSystem.shared.copyFile(atPath: atPath, toPath: toPath) {
+                let fileName = urlIn.lastPathComponent
+                let toPath = CCUtility.getDirectoryProviderStorageOcId(ocId, fileNameView: fileName)!
+                let urlOut = URL(fileURLWithPath: toPath)
+                let serverUrl = appDelegate.activeServerUrl
 
-                    let metadataForUpload = NCManageDatabase.shared.createMetadata(account: appDelegate.account, user: appDelegate.user, userId: appDelegate.userId, fileName: fileName, fileNameView: fileName, ocId: ocId, serverUrl: serverUrl, urlBase: appDelegate.urlBase, url: "", contentType: "")
+                guard let url = self.copySecurityScopedResource(url: urlIn, urlOut: urlOut) else { continue }
 
-                    metadataForUpload.session = NCNetworking.shared.sessionIdentifierBackground
-                    metadataForUpload.sessionSelector = NCGlobal.shared.selectorUploadFile
-                    metadataForUpload.size = NCUtilityFileSystem.shared.getFileSize(filePath: toPath)
-                    metadataForUpload.status = NCGlobal.shared.metadataStatusWaitUpload
+                let metadataForUpload = NCManageDatabase.shared.createMetadata(account: appDelegate.account, user: appDelegate.user, userId: appDelegate.userId, fileName: fileName, fileNameView: fileName, ocId: ocId, serverUrl: serverUrl, urlBase: appDelegate.urlBase, url: "", contentType: "")
 
-                    if NCManageDatabase.shared.getMetadataConflict(account: appDelegate.account, serverUrl: serverUrl, fileNameView: fileName) != nil {
+                metadataForUpload.session = NCNetworking.shared.sessionIdentifierBackground
+                metadataForUpload.sessionSelector = NCGlobal.shared.selectorUploadFile
+                metadataForUpload.size = NCUtilityFileSystem.shared.getFileSize(filePath: toPath)
+                metadataForUpload.status = NCGlobal.shared.metadataStatusWaitUpload
 
-                        if let conflict = UIStoryboard(name: "NCCreateFormUploadConflict", bundle: nil).instantiateInitialViewController() as? NCCreateFormUploadConflict {
+                if NCManageDatabase.shared.getMetadataConflict(account: appDelegate.account, serverUrl: serverUrl, fileNameView: fileName) != nil {
 
-                            conflict.delegate = appDelegate
-                            conflict.serverUrl = serverUrl
-                            conflict.metadatasUploadInConflict = [metadataForUpload]
+                    if let conflict = UIStoryboard(name: "NCCreateFormUploadConflict", bundle: nil).instantiateInitialViewController() as? NCCreateFormUploadConflict {
 
-                            appDelegate.window?.rootViewController?.present(conflict, animated: true, completion: nil)
-                        }
+                        conflict.delegate = appDelegate
+                        conflict.serverUrl = serverUrl
+                        conflict.metadatasUploadInConflict = [metadataForUpload]
 
-                    } else {
-                        NCNetworkingProcessUpload.shared.createProcessUploads(metadatas: [metadataForUpload], completion: { _ in })
+                        appDelegate.window?.rootViewController?.present(conflict, animated: true, completion: nil)
                     }
 
                 } else {
-                    let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_read_file_error_")
-                    NCContentPresenter.shared.showError(error: error)
+                    NCNetworkingProcessUpload.shared.createProcessUploads(metadatas: [metadataForUpload], completion: { _ in })
                 }
             }
         }
     }
+
+    func copySecurityScopedResource(url: URL, urlOut: URL) -> URL? {
+
+        try? FileManager.default.removeItem(at: urlOut)
+        if url.startAccessingSecurityScopedResource() {
+            do {
+                try FileManager.default.copyItem(at: url, to: urlOut)
+                url.stopAccessingSecurityScopedResource()
+                return urlOut
+            } catch {
+            }
+        }
+        return nil
+    }
 }

+ 7 - 7
iOSClient/Networking/NCNetworking.swift

@@ -1535,26 +1535,26 @@ class NCNetworking: NSObject, NKCommonDelegate {
 
     // MARK: - Direct Download
 
-    func getVideoUrl(metadata: tableMetadata, completition: @escaping (_ url: URL?) -> Void) {
+    func getVideoUrl(metadata: tableMetadata, completition: @escaping (_ url: URL?, _ autoplay: Bool) -> Void) {
 
         if !metadata.url.isEmpty {
             if metadata.url.hasPrefix("/") {
-                completition(URL(fileURLWithPath: metadata.url))
+                completition(URL(fileURLWithPath: metadata.url), true)
             } else {
-                completition(URL(string: metadata.url))
+                completition(URL(string: metadata.url), true)
             }
         } else if CCUtility.fileProviderStorageExists(metadata) {
-            completition(URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)))
+            completition(URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)), false)
         } else {
             NextcloudKit.shared.getDirectDownload(fileId: metadata.fileId) { account, url, data, error in
                 if error == .success && url != nil {
                     if let url = URL(string: url!) {
-                        completition(url)
+                        completition(url, false)
                     } else {
-                        completition(nil)
+                        completition(nil, false)
                     }
                 } else {
-                    completition(nil)
+                    completition(nil, false)
                 }
             }
         }

+ 4 - 2
iOSClient/Viewer/NCViewerMedia/NCPlayer/NCPlayer.swift

@@ -66,7 +66,7 @@ class NCPlayer: NSObject {
         NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidEnterBackground), object: nil)
     }
 
-    func openAVPlayer(url: URL) {
+    func openAVPlayer(url: URL, autoplay: Bool = false) {
 
         let userAgent = CCUtility.getUserAgent()!
         var positionSliderToolBar: Float = 0
@@ -98,7 +98,9 @@ class NCPlayer: NSObject {
         playerToolBar?.setBarPlayer(ncplayer: self, position: positionSliderToolBar, metadata: metadata, viewerMediaPage: viewerMediaPage)
 
         player?.play()
-        player?.pause()
+        if !autoplay {
+            player?.pause()
+        }
 
         NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterApplicationDidEnterBackground), object: nil)
     }

+ 2 - 2
iOSClient/Viewer/NCViewerMedia/NCViewerMedia.swift

@@ -176,9 +176,9 @@ class NCViewerMedia: UIViewController {
             if let ncplayer = self.ncplayer {
 
                 if ncplayer.url == nil {
-                    NCNetworking.shared.getVideoUrl(metadata: metadata) { url in
+                    NCNetworking.shared.getVideoUrl(metadata: metadata) { url, autoplay in
                         if let url = url {
-                            ncplayer.openAVPlayer(url: url)
+                            ncplayer.openAVPlayer(url: url, autoplay: autoplay)
                             self.viewerMediaPage?.updateCommandCenter(ncplayer: ncplayer, metadata: self.metadata)
                         }
                     }