NCVideoViewController.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. //
  2. // NCVideo.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 27/10/2020.
  6. // Copyright © 2020 Marino Faggiana. All rights reserved.
  7. //
  8. import Foundation
  9. import KTVHTTPCache
  10. class NCVideoViewController: AVPlayerViewController {
  11. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  12. var metadata = tableMetadata()
  13. override func viewDidLoad() {
  14. super.viewDidLoad()
  15. var videoURL: URL?
  16. setupHTTPCache()
  17. saveCache()
  18. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  19. videoURL = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))
  20. } else {
  21. guard let stringURL = (metadata.serverUrl + "/" + metadata.fileName).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
  22. return
  23. }
  24. videoURL = KTVHTTPCache.proxyURL(withOriginalURL: URL(string: stringURL))
  25. guard let authData = (appDelegate.user + ":" + appDelegate.password).data(using: .utf8) else {
  26. return
  27. }
  28. let authValue = "Basic " + authData.base64EncodedString(options: [])
  29. KTVHTTPCache.downloadSetAdditionalHeaders(["Authorization":authValue, "User-Agent":CCUtility.getUserAgent()])
  30. }
  31. if let url = videoURL {
  32. let video = AVPlayer(url: url)
  33. player = video
  34. // At end go back to start
  35. NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: nil, queue: nil) { (notification) in
  36. let player = notification.object as! AVPlayerItem
  37. player.seek(to: CMTime.zero, completionHandler: nil)
  38. }
  39. player?.addObserver(self, forKeyPath: "rate", options: [], context: nil)
  40. player?.play()
  41. }
  42. }
  43. override func viewDidDisappear(_ animated: Bool) {
  44. super.viewDidDisappear(animated)
  45. player?.pause()
  46. removeObserver()
  47. if KTVHTTPCache.proxyIsRunning() {
  48. KTVHTTPCache.proxyStop()
  49. }
  50. }
  51. //MARK: -
  52. func saveCache() {
  53. if !CCUtility.fileProviderStorageExists(self.metadata.ocId, fileNameView:self.metadata.fileNameView) {
  54. guard let stringURL = (metadata.serverUrl + "/" + metadata.fileName).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { return }
  55. let videoURL = URL(string: stringURL)
  56. guard let url = KTVHTTPCache.cacheCompleteFileURL(with: videoURL) else { return }
  57. CCUtility.copyFile(atPath: url.path, toPath: CCUtility.getDirectoryProviderStorageOcId(self.metadata.ocId, fileNameView: self.metadata.fileNameView))
  58. NCManageDatabase.sharedInstance.addLocalFile(metadata: self.metadata)
  59. KTVHTTPCache.cacheDelete(with: videoURL)
  60. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_reloadDataSource, userInfo: ["ocId":self.metadata.ocId, "serverUrl":self.metadata.serverUrl])
  61. }
  62. }
  63. //MARK: - Observer
  64. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  65. if keyPath != nil && keyPath == "rate" {
  66. if player?.rate == 1 {
  67. print("start")
  68. } else {
  69. print("stop")
  70. }
  71. saveCache()
  72. }
  73. }
  74. @objc func removeObserver() {
  75. player?.removeObserver(self, forKeyPath: "rate", context: nil)
  76. NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
  77. }
  78. //MARK: - KTVHTTPCache
  79. @objc func setupHTTPCache() {
  80. if KTVHTTPCache.proxyIsRunning() {
  81. KTVHTTPCache.proxyStop()
  82. }
  83. KTVHTTPCache.cacheSetMaxCacheLength(Int64(k_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 reviced URL : " + String(describing: url))
  94. return url
  95. }
  96. KTVHTTPCache.downloadSetUnacceptableContentTypeDisposer { (url, contentType) -> Bool in
  97. print("Unsupport Content-Type Filter reviced URL : " + String(describing: url) + " " + String(describing: contentType))
  98. return false
  99. }
  100. }
  101. }