NCKTVHTTPCache.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. instance.setupHTTPCache()
  29. return instance
  30. }()
  31. func getVideoURL(metadata: tableMetadata) -> URL? {
  32. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  33. return URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))
  34. } else {
  35. guard let stringURL = (metadata.serverUrl + "/" + metadata.fileName).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { return nil }
  36. return NCKTVHTTPCache.shared.getProxyURL(stringURL: stringURL)
  37. }
  38. }
  39. func startProxy(user: String, password: String, metadata: tableMetadata) {
  40. guard let authData = (user + ":" + password).data(using: .utf8) else { return }
  41. let authValue = "Basic " + authData.base64EncodedString(options: [])
  42. KTVHTTPCache.downloadSetAdditionalHeaders(["Authorization":authValue, "User-Agent":CCUtility.getUserAgent()])
  43. if !KTVHTTPCache.proxyIsRunning() {
  44. do {
  45. try KTVHTTPCache.proxyStart()
  46. } catch let error {
  47. print("Proxy Start error : \(error)")
  48. }
  49. }
  50. saveCache(metadata: metadata)
  51. }
  52. func stopProxy() {
  53. if KTVHTTPCache.proxyIsRunning() {
  54. KTVHTTPCache.proxyStop()
  55. }
  56. }
  57. func getProxyURL(stringURL: String) -> URL {
  58. return KTVHTTPCache.proxyURL(withOriginalURL: URL(string: stringURL))
  59. }
  60. func getCacheCompleteFileURL(videoURL: URL?) -> URL? {
  61. return KTVHTTPCache.cacheCompleteFileURL(with: videoURL)
  62. }
  63. func deleteCache(videoURL: URL?) {
  64. KTVHTTPCache.cacheDelete(with: videoURL)
  65. }
  66. func saveCache(metadata: tableMetadata) {
  67. if !CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView:metadata.fileNameView) {
  68. guard let stringURL = (metadata.serverUrl + "/" + metadata.fileName).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { return }
  69. let videoURL = URL(string: stringURL)
  70. guard let url = KTVHTTPCache.cacheCompleteFileURL(with: videoURL) else { return }
  71. CCUtility.copyFile(atPath: url.path, toPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))
  72. NCManageDatabase.shared.addLocalFile(metadata: metadata)
  73. KTVHTTPCache.cacheDelete(with: videoURL)
  74. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource, userInfo: ["ocId":metadata.ocId, "serverUrl":metadata.serverUrl])
  75. }
  76. }
  77. private func setupHTTPCache() {
  78. KTVHTTPCache.cacheSetMaxCacheLength(NCGlobal.shared.maxHTTPCache)
  79. if ProcessInfo.processInfo.environment["SIMULATOR_DEVICE_NAME"] != nil {
  80. KTVHTTPCache.logSetConsoleLogEnable(true)
  81. }
  82. do {
  83. try KTVHTTPCache.proxyStart()
  84. } catch let error {
  85. print("Proxy Start error : \(error)")
  86. }
  87. KTVHTTPCache.encodeSetURLConverter { (url) -> URL? in
  88. print("URL Filter reviced URL : " + String(describing: url))
  89. return url
  90. }
  91. KTVHTTPCache.downloadSetUnacceptableContentTypeDisposer { (url, contentType) -> Bool in
  92. print("Unsupport Content-Type Filter reviced URL : " + String(describing: url) + " " + String(describing: contentType))
  93. return false
  94. }
  95. }
  96. }