NCKTVHTTPCache.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. import KTVHTTPCache
  25. class NCKTVHTTPCache: NSObject {
  26. @objc static let shared: NCKTVHTTPCache = {
  27. let instance = NCKTVHTTPCache()
  28. return instance
  29. }()
  30. func getVideoURL(metadata: tableMetadata) -> URL? {
  31. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  32. return URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))
  33. } else {
  34. guard let stringURL = (metadata.serverUrl + "/" + metadata.fileName).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { return nil }
  35. return NCKTVHTTPCache.shared.getProxyURL(stringURL: stringURL)
  36. }
  37. }
  38. func startProxy(user: String, password: String) {
  39. guard let authData = (user + ":" + password).data(using: .utf8) else { return }
  40. let authValue = "Basic " + authData.base64EncodedString(options: [])
  41. KTVHTTPCache.downloadSetAdditionalHeaders(["Authorization":authValue, "User-Agent":CCUtility.getUserAgent()])
  42. self.setupHTTPCache()
  43. if !KTVHTTPCache.proxyIsRunning() {
  44. do {
  45. try KTVHTTPCache.proxyStart()
  46. } catch let error {
  47. print("Proxy Start error : \(error)")
  48. }
  49. }
  50. }
  51. func stopProxy() {
  52. if KTVHTTPCache.proxyIsRunning() {
  53. KTVHTTPCache.proxyStop()
  54. }
  55. }
  56. func restartProxy(user: String, password: String) {
  57. if KTVHTTPCache.proxyIsRunning() {
  58. KTVHTTPCache.proxyStop()
  59. }
  60. startProxy(user: user, password: password)
  61. }
  62. func getProxyURL(stringURL: String) -> URL {
  63. return KTVHTTPCache.proxyURL(withOriginalURL: URL(string: stringURL))
  64. }
  65. func getCacheCompleteFileURL(videoURL: URL?) -> URL? {
  66. return KTVHTTPCache.cacheCompleteFileURL(with: videoURL)
  67. }
  68. func deleteCache(videoURL: URL?) {
  69. KTVHTTPCache.cacheDelete(with: videoURL)
  70. }
  71. func saveCache(metadata: tableMetadata) {
  72. if !CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView:metadata.fileNameView) {
  73. guard let stringURL = (metadata.serverUrl + "/" + metadata.fileName).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { return }
  74. let videoURL = URL(string: stringURL)
  75. guard let url = KTVHTTPCache.cacheCompleteFileURL(with: videoURL) else { return }
  76. CCUtility.copyFile(atPath: url.path, toPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))
  77. NCManageDatabase.shared.addLocalFile(metadata: metadata)
  78. KTVHTTPCache.cacheDelete(with: videoURL)
  79. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource, userInfo: ["ocId":metadata.ocId, "serverUrl":metadata.serverUrl])
  80. }
  81. }
  82. private func setupHTTPCache() {
  83. KTVHTTPCache.cacheSetMaxCacheLength(NCGlobal.shared.maxHTTPCache)
  84. if ProcessInfo.processInfo.environment["SIMULATOR_DEVICE_NAME"] != nil {
  85. KTVHTTPCache.logSetConsoleLogEnable(true)
  86. }
  87. // do {
  88. // try KTVHTTPCache.proxyStart()
  89. // } catch let error {
  90. // print("Proxy Start error : \(error)")
  91. // }
  92. KTVHTTPCache.encodeSetURLConverter { (url) -> URL? in
  93. print("URL Filter received URL : " + String(describing: url))
  94. return url
  95. }
  96. KTVHTTPCache.downloadSetUnacceptableContentTypeDisposer { (url, contentType) -> Bool in
  97. print("Unsupport Content-Type Filter received URL : " + String(describing: url) + " " + String(describing: contentType))
  98. return false
  99. }
  100. }
  101. }