Browse Source

Improved Video

marinofaggiana 4 years ago
parent
commit
0986905028

+ 15 - 16
iOSClient/Viewer/NCViewerVideoAudio/NCKTVHTTPCache.swift

@@ -16,12 +16,25 @@ class NCKTVHTTPCache: NSObject {
         return instance
     }()
     
-    func setAuth(user: String, password: String) {
+    func startProxy(user: String, password: String, metadata: tableMetadata) {
         
         guard let authData = (user + ":" + password).data(using: .utf8) else { return }
         
         let authValue = "Basic " + authData.base64EncodedString(options: [])
         KTVHTTPCache.downloadSetAdditionalHeaders(["Authorization":authValue, "User-Agent":CCUtility.getUserAgent()])
+        
+        if !KTVHTTPCache.proxyIsRunning() {
+            try? KTVHTTPCache.proxyStart()
+        }
+        
+        saveCache(metadata: metadata)
+    }
+    
+    func stopProxy() {
+        
+        if KTVHTTPCache.proxyIsRunning() {
+            KTVHTTPCache.proxyStop()
+        }
     }
     
     func getProxyURL(stringURL: String) -> URL {
@@ -29,7 +42,7 @@ class NCKTVHTTPCache: NSObject {
         return KTVHTTPCache.proxyURL(withOriginalURL: URL(string: stringURL))
     }
     
-    func getCompleteFileURL(videoURL: URL?) -> URL? {
+    func getCacheCompleteFileURL(videoURL: URL?) -> URL? {
         
         return KTVHTTPCache.cacheCompleteFileURL(with: videoURL)
     }
@@ -55,20 +68,6 @@ class NCKTVHTTPCache: NSObject {
         }
     }
     
-    func stopProxy() {
-        
-        if KTVHTTPCache.proxyIsRunning() {
-            KTVHTTPCache.proxyStop()
-        }
-    }
-    
-    func startProxy() {
-        
-        if !KTVHTTPCache.proxyIsRunning() {
-            try? KTVHTTPCache.proxyStart()
-        }
-    }
-    
     private func setupHTTPCache() {
         
         KTVHTTPCache.cacheSetMaxCacheLength(Int64(k_maxHTTPCache))

+ 2 - 23
iOSClient/Viewer/NCViewerVideoAudio/NCViewerVideoAudio.swift

@@ -33,8 +33,7 @@ class NCViewerVideoAudio: AVPlayerViewController {
 
         var videoURL: URL?
 
-        NCKTVHTTPCache.shared.startProxy()
-        NCKTVHTTPCache.shared.saveCache(metadata: metadata)
+        NCKTVHTTPCache.shared.startProxy(user: appDelegate.user, password: appDelegate.password, metadata: metadata)
         
         if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
             
@@ -47,7 +46,6 @@ class NCViewerVideoAudio: AVPlayerViewController {
             }
             
             videoURL = NCKTVHTTPCache.shared.getProxyURL(stringURL: stringURL)
-            NCKTVHTTPCache.shared.setAuth(user: appDelegate.user, password: appDelegate.password)
         }
         
         if let url = videoURL {
@@ -76,25 +74,6 @@ class NCViewerVideoAudio: AVPlayerViewController {
         NCKTVHTTPCache.shared.stopProxy()
     }
     
-    //MARK: -
-    
-    func saveCache() {
-        
-        if !CCUtility.fileProviderStorageExists(self.metadata.ocId, fileNameView:self.metadata.fileNameView) {
-            
-            guard let stringURL = (metadata.serverUrl + "/" + metadata.fileName).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { return }
-            
-            let videoURL = URL(string: stringURL)
-            guard let url = NCKTVHTTPCache.shared.getCompleteFileURL(videoURL: videoURL) else { return }
-            
-            CCUtility.copyFile(atPath: url.path, toPath: CCUtility.getDirectoryProviderStorageOcId(self.metadata.ocId, fileNameView: self.metadata.fileNameView))
-            NCManageDatabase.sharedInstance.addLocalFile(metadata: self.metadata)
-            NCKTVHTTPCache.shared.deleteCache(videoURL: videoURL)
-            
-            NotificationCenter.default.postOnMainThread(name: k_notificationCenter_reloadDataSource, userInfo: ["ocId":self.metadata.ocId, "serverUrl":self.metadata.serverUrl])
-        }
-    }
-    
     //MARK: - Observer
 
     override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
@@ -107,7 +86,7 @@ class NCViewerVideoAudio: AVPlayerViewController {
                 print("pause")
             }
             
-            saveCache()
+            NCKTVHTTPCache.shared.saveCache(metadata: metadata)
         }
     }