NCKTVHTTPCache.swift 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. return instance
  14. }()
  15. func setAuth(user: String, password: String) {
  16. guard let authData = (user + ":" + password).data(using: .utf8) else {
  17. return
  18. }
  19. let authValue = "Basic " + authData.base64EncodedString(options: [])
  20. KTVHTTPCache.downloadSetAdditionalHeaders(["Authorization":authValue, "User-Agent":CCUtility.getUserAgent()])
  21. }
  22. func getProxyURL(stringURL: String) -> URL {
  23. return KTVHTTPCache.proxyURL(withOriginalURL: URL(string: stringURL))
  24. }
  25. func getCompleteFileURL(videoURL: URL?) -> URL? {
  26. return KTVHTTPCache.cacheCompleteFileURL(with: videoURL)
  27. }
  28. func deleteCache(videoURL: URL?) {
  29. KTVHTTPCache.cacheDelete(with: videoURL)
  30. }
  31. func saveCache(metadata: tableMetadata) {
  32. if !CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView:metadata.fileNameView) {
  33. guard let stringURL = (metadata.serverUrl + "/" + metadata.fileName).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { return }
  34. let videoURL = URL(string: stringURL)
  35. guard let url = KTVHTTPCache.cacheCompleteFileURL(with: videoURL) else { return }
  36. CCUtility.copyFile(atPath: url.path, toPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))
  37. NCManageDatabase.sharedInstance.addLocalFile(metadata: metadata)
  38. KTVHTTPCache.cacheDelete(with: videoURL)
  39. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_reloadDataSource, userInfo: ["ocId":metadata.ocId, "serverUrl":metadata.serverUrl])
  40. }
  41. }
  42. func stopProxy() {
  43. if KTVHTTPCache.proxyIsRunning() {
  44. KTVHTTPCache.proxyStop()
  45. }
  46. }
  47. func setupHTTPCache() {
  48. if KTVHTTPCache.proxyIsRunning() {
  49. KTVHTTPCache.proxyStop()
  50. }
  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. }