NCKTVHTTPCache.swift 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 startProxy(user: String, password: String, metadata: tableMetadata) {
  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. if !KTVHTTPCache.proxyIsRunning() {
  21. try? KTVHTTPCache.proxyStart()
  22. }
  23. saveCache(metadata: metadata)
  24. }
  25. func stopProxy() {
  26. if KTVHTTPCache.proxyIsRunning() {
  27. KTVHTTPCache.proxyStop()
  28. }
  29. }
  30. func getProxyURL(stringURL: String) -> URL {
  31. return KTVHTTPCache.proxyURL(withOriginalURL: URL(string: stringURL))
  32. }
  33. func getCacheCompleteFileURL(videoURL: URL?) -> URL? {
  34. return KTVHTTPCache.cacheCompleteFileURL(with: videoURL)
  35. }
  36. func deleteCache(videoURL: URL?) {
  37. KTVHTTPCache.cacheDelete(with: videoURL)
  38. }
  39. func saveCache(metadata: tableMetadata) {
  40. if !CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView:metadata.fileNameView) {
  41. guard let stringURL = (metadata.serverUrl + "/" + metadata.fileName).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { return }
  42. let videoURL = URL(string: stringURL)
  43. guard let url = KTVHTTPCache.cacheCompleteFileURL(with: videoURL) else { return }
  44. CCUtility.copyFile(atPath: url.path, toPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))
  45. NCManageDatabase.sharedInstance.addLocalFile(metadata: metadata)
  46. KTVHTTPCache.cacheDelete(with: videoURL)
  47. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_reloadDataSource, userInfo: ["ocId":metadata.ocId, "serverUrl":metadata.serverUrl])
  48. }
  49. }
  50. private func setupHTTPCache() {
  51. KTVHTTPCache.cacheSetMaxCacheLength(Int64(k_maxHTTPCache))
  52. if ProcessInfo.processInfo.environment["SIMULATOR_DEVICE_NAME"] != nil {
  53. KTVHTTPCache.logSetConsoleLogEnable(true)
  54. }
  55. do {
  56. try KTVHTTPCache.proxyStart()
  57. } catch let error {
  58. print("Proxy Start error : \(error)")
  59. }
  60. KTVHTTPCache.encodeSetURLConverter { (url) -> URL? in
  61. print("URL Filter reviced URL : " + String(describing: url))
  62. return url
  63. }
  64. KTVHTTPCache.downloadSetUnacceptableContentTypeDisposer { (url, contentType) -> Bool in
  65. print("Unsupport Content-Type Filter reviced URL : " + String(describing: url) + " " + String(describing: contentType))
  66. return false
  67. }
  68. }
  69. }