Browse Source

Improved Video

marinofaggiana 4 years ago
parent
commit
e64442dcdd

+ 4 - 0
Nextcloud.xcodeproj/project.pbxproj

@@ -288,6 +288,7 @@
 		F7F8D71C1ED6183000E711F3 /* CCCellShareExt.m in Sources */ = {isa = PBXBuildFile; fileRef = F7F8D71A1ED6183000E711F3 /* CCCellShareExt.m */; };
 		F7F8D71D1ED6183000E711F3 /* CCCellShareExt.xib in Resources */ = {isa = PBXBuildFile; fileRef = F7F8D71B1ED6183000E711F3 /* CCCellShareExt.xib */; };
 		F7F9D1BB25397CE000D9BFF5 /* NCViewer.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7F9D1BA25397CE000D9BFF5 /* NCViewer.swift */; };
+		F7FAC2672549E1B5008EE2C4 /* NCKTVHTTPCache.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7FAC2662549E1B5008EE2C4 /* NCKTVHTTPCache.swift */; };
 		F7FCFFD81D70798C000E6E29 /* CCPeekPop.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F7FCFFD61D70798C000E6E29 /* CCPeekPop.storyboard */; };
 		F7FCFFE01D707B83000E6E29 /* CCPeekPop.m in Sources */ = {isa = PBXBuildFile; fileRef = F7FCFFDE1D707B83000E6E29 /* CCPeekPop.m */; };
 /* End PBXBuildFile section */
@@ -673,6 +674,7 @@
 		F7F8D71A1ED6183000E711F3 /* CCCellShareExt.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCCellShareExt.m; sourceTree = "<group>"; };
 		F7F8D71B1ED6183000E711F3 /* CCCellShareExt.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = CCCellShareExt.xib; sourceTree = "<group>"; };
 		F7F9D1BA25397CE000D9BFF5 /* NCViewer.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCViewer.swift; sourceTree = "<group>"; };
+		F7FAC2662549E1B5008EE2C4 /* NCKTVHTTPCache.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCKTVHTTPCache.swift; sourceTree = "<group>"; };
 		F7FC7D551DC1F93800BB2C6A /* libz.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = libz.tbd; path = usr/lib/libz.tbd; sourceTree = SDKROOT; };
 		F7FCFFD61D70798C000E6E29 /* CCPeekPop.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = CCPeekPop.storyboard; sourceTree = "<group>"; };
 		F7FCFFDD1D707B83000E6E29 /* CCPeekPop.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CCPeekPop.h; sourceTree = "<group>"; };
@@ -1118,6 +1120,7 @@
 			isa = PBXGroup;
 			children = (
 				F752011C25480387000BF3A7 /* NCViewerVideoAudio.swift */,
+				F7FAC2662549E1B5008EE2C4 /* NCKTVHTTPCache.swift */,
 			);
 			path = NCViewerVideoAudio;
 			sourceTree = "<group>";
@@ -2067,6 +2070,7 @@
 				F7F4B1D823C74B3E00D82A6E /* NCRichWorkspace.swift in Sources */,
 				F726EEEC1FED1C820030B9C8 /* NCEndToEndInitialize.swift in Sources */,
 				F79A65C62191D95E00FF6DCC /* NCSelect.swift in Sources */,
+				F7FAC2672549E1B5008EE2C4 /* NCKTVHTTPCache.swift in Sources */,
 				F7E0E1DC22327885006B0911 /* NCAudioRecorderViewController.swift in Sources */,
 				F70CAE3A1F8CF31A008125FD /* NCEndToEndEncryption.m in Sources */,
 				F70753EB2542A99800972D44 /* NCViewerImagePageContainer.swift in Sources */,

+ 94 - 0
iOSClient/Viewer/NCViewerVideoAudio/NCKTVHTTPCache.swift

@@ -0,0 +1,94 @@
+//
+//  NCKTVHTTPCache.swift
+//  Nextcloud
+//
+//  Created by Marino Faggiana on 28/10/2020.
+//  Copyright © 2020 Marino Faggiana. All rights reserved.
+//
+
+import Foundation
+import KTVHTTPCache
+
+class NCKTVHTTPCache: NSObject {
+    @objc static let shared: NCKTVHTTPCache = {
+        let instance = NCKTVHTTPCache()
+        return instance
+    }()
+    
+    func setAuth(user: String, password: String) {
+        
+        guard let authData = (user + ":" + password).data(using: .utf8) else {
+            return
+        }
+        
+        let authValue = "Basic " + authData.base64EncodedString(options: [])
+        KTVHTTPCache.downloadSetAdditionalHeaders(["Authorization":authValue, "User-Agent":CCUtility.getUserAgent()])
+    }
+    
+    func getProxyURL(stringURL: String) -> URL {
+        
+        return KTVHTTPCache.proxyURL(withOriginalURL: URL(string: stringURL))
+    }
+    
+    func getCompleteFileURL(videoURL: URL?) -> URL? {
+        
+        return KTVHTTPCache.cacheCompleteFileURL(with: videoURL)
+    }
+    
+    func deleteCache(videoURL: URL?) {
+        KTVHTTPCache.cacheDelete(with: videoURL)
+    }
+    
+    func saveCache(metadata: tableMetadata) {
+        
+        if !CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView:metadata.fileNameView) {
+            
+            guard let stringURL = (metadata.serverUrl + "/" + metadata.fileName).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { return }
+            
+            let videoURL = URL(string: stringURL)
+            guard let url = KTVHTTPCache.cacheCompleteFileURL(with: videoURL) else { return }
+            
+            CCUtility.copyFile(atPath: url.path, toPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))
+            NCManageDatabase.sharedInstance.addLocalFile(metadata: metadata)
+            KTVHTTPCache.cacheDelete(with: videoURL)
+            
+            NotificationCenter.default.postOnMainThread(name: k_notificationCenter_reloadDataSource, userInfo: ["ocId":metadata.ocId, "serverUrl":metadata.serverUrl])
+        }
+    }
+    
+    func stopProxy() {
+        
+        if KTVHTTPCache.proxyIsRunning() {
+            KTVHTTPCache.proxyStop()
+        }
+    }
+    
+    func setupHTTPCache() {
+        
+        if KTVHTTPCache.proxyIsRunning() {
+            KTVHTTPCache.proxyStop()
+        }
+        
+        KTVHTTPCache.cacheSetMaxCacheLength(Int64(k_maxHTTPCache))
+        
+        if ProcessInfo.processInfo.environment["SIMULATOR_DEVICE_NAME"] != nil {
+            KTVHTTPCache.logSetConsoleLogEnable(true)
+        }
+        
+        do {
+            try KTVHTTPCache.proxyStart()
+        } catch let error {
+            print("Proxy Start error : \(error)")
+        }
+        
+        KTVHTTPCache.encodeSetURLConverter { (url) -> URL? in
+            print("URL Filter reviced URL : " + String(describing: url))
+            return url
+        }
+        
+        KTVHTTPCache.downloadSetUnacceptableContentTypeDisposer { (url, contentType) -> Bool in
+            print("Unsupport Content-Type Filter reviced URL : " + String(describing: url) + " " + String(describing: contentType))
+            return false
+        }
+    }
+}

+ 7 - 46
iOSClient/Viewer/NCViewerVideoAudio/NCViewerVideoAudio.swift

@@ -22,7 +22,6 @@
 //
 
 import Foundation
-import KTVHTTPCache
 
 class NCViewerVideoAudio: AVPlayerViewController {
     
@@ -34,8 +33,8 @@ class NCViewerVideoAudio: AVPlayerViewController {
 
         var videoURL: URL?
 
-        setupHTTPCache()
-        saveCache()
+        NCKTVHTTPCache.shared.setupHTTPCache()
+        NCKTVHTTPCache.shared.saveCache(metadata: metadata)
         
         if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
             
@@ -47,14 +46,8 @@ class NCViewerVideoAudio: AVPlayerViewController {
                 return
             }
             
-            videoURL = KTVHTTPCache.proxyURL(withOriginalURL: URL(string: stringURL))
-            
-            guard let authData = (appDelegate.user + ":" + appDelegate.password).data(using: .utf8) else {
-                return
-            }
-            
-            let authValue = "Basic " + authData.base64EncodedString(options: [])
-            KTVHTTPCache.downloadSetAdditionalHeaders(["Authorization":authValue, "User-Agent":CCUtility.getUserAgent()])
+            videoURL = NCKTVHTTPCache.shared.getProxyURL(stringURL: stringURL)
+            NCKTVHTTPCache.shared.setAuth(user: appDelegate.user, password: appDelegate.password)
         }
         
         if let url = videoURL {
@@ -80,9 +73,7 @@ class NCViewerVideoAudio: AVPlayerViewController {
         
         player?.pause()
         removeObserver()
-        if KTVHTTPCache.proxyIsRunning() {
-            KTVHTTPCache.proxyStop()
-        }
+        NCKTVHTTPCache.shared.stopProxy()
     }
     
     //MARK: -
@@ -94,11 +85,11 @@ class NCViewerVideoAudio: AVPlayerViewController {
             guard let stringURL = (metadata.serverUrl + "/" + metadata.fileName).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { return }
             
             let videoURL = URL(string: stringURL)
-            guard let url = KTVHTTPCache.cacheCompleteFileURL(with: videoURL) else { return }
+            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)
-            KTVHTTPCache.cacheDelete(with: videoURL)
+            NCKTVHTTPCache.shared.deleteCache(videoURL: videoURL)
             
             NotificationCenter.default.postOnMainThread(name: k_notificationCenter_reloadDataSource, userInfo: ["ocId":self.metadata.ocId, "serverUrl":self.metadata.serverUrl])
         }
@@ -125,34 +116,4 @@ class NCViewerVideoAudio: AVPlayerViewController {
         player?.removeObserver(self, forKeyPath: "rate", context: nil)
         NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
     }
-    
-    //MARK: - KTVHTTPCache
-    
-    @objc func setupHTTPCache() {
-        
-        if KTVHTTPCache.proxyIsRunning() {
-            KTVHTTPCache.proxyStop()
-        }
-        KTVHTTPCache.cacheSetMaxCacheLength(Int64(k_maxHTTPCache))
-        
-        if ProcessInfo.processInfo.environment["SIMULATOR_DEVICE_NAME"] != nil {
-            KTVHTTPCache.logSetConsoleLogEnable(true)
-        }
-        
-        do {
-            try KTVHTTPCache.proxyStart()
-        } catch let error {
-            print("Proxy Start error : \(error)")
-        }
-        
-        KTVHTTPCache.encodeSetURLConverter { (url) -> URL? in
-            print("URL Filter reviced URL : " + String(describing: url))
-            return url
-        }
-        
-        KTVHTTPCache.downloadSetUnacceptableContentTypeDisposer { (url, contentType) -> Bool in
-            print("Unsupport Content-Type Filter reviced URL : " + String(describing: url) + " " + String(describing: contentType))
-            return false
-        }
-    }
 }