NCKTVHTTPCache.swift 4.1 KB

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