NCKTVHTTPCache.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. //
  2. // NCKTVHTTPCache.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 28/10/2020.
  6. // Copyright © 2020 Marino Faggiana. All rights reserved.
  7. //
  8. import Foundation
  9. import KTVHTTPCache
  10. class NCKTVHTTPCache: NSObject {
  11. @objc static let shared: NCKTVHTTPCache = {
  12. let instance = NCKTVHTTPCache()
  13. instance.setupHTTPCache()
  14. return instance
  15. }()
  16. func setAuth(user: String, password: String) {
  17. guard let authData = (user + ":" + password).data(using: .utf8) else { return }
  18. let authValue = "Basic " + authData.base64EncodedString(options: [])
  19. KTVHTTPCache.downloadSetAdditionalHeaders(["Authorization":authValue, "User-Agent":CCUtility.getUserAgent()])
  20. }
  21. func getProxyURL(stringURL: String) -> URL {
  22. return KTVHTTPCache.proxyURL(withOriginalURL: URL(string: stringURL))
  23. }
  24. func getCompleteFileURL(videoURL: URL?) -> URL? {
  25. return KTVHTTPCache.cacheCompleteFileURL(with: videoURL)
  26. }
  27. func deleteCache(videoURL: URL?) {
  28. KTVHTTPCache.cacheDelete(with: videoURL)
  29. }
  30. func saveCache(metadata: tableMetadata) {
  31. if !CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView:metadata.fileNameView) {
  32. guard let stringURL = (metadata.serverUrl + "/" + metadata.fileName).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { return }
  33. let videoURL = URL(string: stringURL)
  34. guard let url = KTVHTTPCache.cacheCompleteFileURL(with: videoURL) else { return }
  35. CCUtility.copyFile(atPath: url.path, toPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))
  36. NCManageDatabase.sharedInstance.addLocalFile(metadata: metadata)
  37. KTVHTTPCache.cacheDelete(with: videoURL)
  38. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_reloadDataSource, userInfo: ["ocId":metadata.ocId, "serverUrl":metadata.serverUrl])
  39. }
  40. }
  41. func stopProxy() {
  42. if KTVHTTPCache.proxyIsRunning() {
  43. KTVHTTPCache.proxyStop()
  44. }
  45. }
  46. func startProxy() {
  47. if !KTVHTTPCache.proxyIsRunning() {
  48. try? KTVHTTPCache.proxyStart()
  49. }
  50. }
  51. private func setupHTTPCache() {
  52. if KTVHTTPCache.proxyIsRunning() {
  53. KTVHTTPCache.proxyStop()
  54. }
  55. KTVHTTPCache.cacheSetMaxCacheLength(Int64(k_maxHTTPCache))
  56. if ProcessInfo.processInfo.environment["SIMULATOR_DEVICE_NAME"] != nil {
  57. KTVHTTPCache.logSetConsoleLogEnable(true)
  58. }
  59. do {
  60. try KTVHTTPCache.proxyStart()
  61. } catch let error {
  62. print("Proxy Start error : \(error)")
  63. }
  64. KTVHTTPCache.encodeSetURLConverter { (url) -> URL? in
  65. print("URL Filter reviced URL : " + String(describing: url))
  66. return url
  67. }
  68. KTVHTTPCache.downloadSetUnacceptableContentTypeDisposer { (url, contentType) -> Bool in
  69. print("Unsupport Content-Type Filter reviced URL : " + String(describing: url) + " " + String(describing: contentType))
  70. return false
  71. }
  72. }
  73. }