NCViewerMedia.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. //
  2. // NCViewerMedia.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 21/09/18.
  6. // Copyright © 2018 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 NCViewerMedia: NSObject {
  26. var detail: CCDetail!
  27. var metadata: tableMetadata!
  28. var videoURL: URL!
  29. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  30. var safeAreaBottom: Int = 0
  31. @objc static let sharedInstance: NCViewerMedia = {
  32. let viewMedia = NCViewerMedia()
  33. viewMedia.setupHTTPCache()
  34. return viewMedia
  35. }()
  36. @objc func viewMedia(_ metadata: tableMetadata, detail: CCDetail) {
  37. var videoURLProxy: URL!
  38. self.detail = detail
  39. self.metadata = metadata
  40. guard let rootView = UIApplication.shared.keyWindow else {
  41. return
  42. }
  43. if #available(iOS 11.0, *) {
  44. safeAreaBottom = Int(rootView.safeAreaInsets.bottom)
  45. }
  46. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  47. self.videoURL = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))
  48. videoURLProxy = videoURL
  49. } else {
  50. guard let stringURL = (metadata.serverUrl + "/" + metadata.fileName).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
  51. return
  52. }
  53. self.videoURL = URL(string: stringURL)
  54. videoURLProxy = KTVHTTPCache.proxyURL(withOriginalURL: self.videoURL)
  55. guard let authData = (appDelegate.activeUser + ":" + appDelegate.activePassword).data(using: .utf8) else {
  56. return
  57. }
  58. let authValue = "Basic " + authData.base64EncodedString(options: [])
  59. KTVHTTPCache.downloadSetAdditionalHeaders(["Authorization":authValue, "User-Agent":CCUtility.getUserAgent()])
  60. // Disable Button Action (the file is in download via Proxy Server)
  61. detail.buttonAction.isEnabled = false
  62. }
  63. appDelegate.player = AVPlayer(url: videoURLProxy)
  64. appDelegate.playerController = AVPlayerViewController()
  65. appDelegate.playerController.player = appDelegate.player
  66. appDelegate.playerController.view.frame = CGRect(x: 0, y: 0, width: Int(rootView.bounds.size.width), height: Int(rootView.bounds.size.height) - Int(k_detail_Toolbar_Height) - safeAreaBottom - 1)
  67. appDelegate.playerController.allowsPictureInPicturePlayback = false
  68. detail.addChild(appDelegate.playerController)
  69. detail.view.addSubview(appDelegate.playerController.view)
  70. appDelegate.playerController.didMove(toParent: detail)
  71. NotificationCenter.default.addObserver(forName: .AVPlayerItemDidPlayToEndTime, object: nil, queue: nil) { (notification) in
  72. let player = notification.object as! AVPlayerItem
  73. player.seek(to: CMTime.zero)
  74. }
  75. appDelegate.player.addObserver(self, forKeyPath: "rate", options: [], context: nil)
  76. detail.isMediaObserver = true
  77. appDelegate.player.play()
  78. }
  79. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  80. if keyPath != nil && keyPath == "rate" {
  81. if appDelegate.player.rate == 1 {
  82. print("start")
  83. } else {
  84. print("stop")
  85. }
  86. // Save cache
  87. if !CCUtility.fileProviderStorageExists(self.metadata.ocId, fileNameView:self.metadata.fileNameView) {
  88. guard let url = KTVHTTPCache.cacheCompleteFileURL(with: self.videoURL) else {
  89. return
  90. }
  91. CCUtility.copyFile(atPath: url.path, toPath: CCUtility.getDirectoryProviderStorageOcId(self.metadata.ocId, fileNameView: self.metadata.fileNameView))
  92. _ = NCManageDatabase.sharedInstance.addLocalFile(metadata: self.metadata)
  93. KTVHTTPCache.cacheDelete(with: self.videoURL)
  94. // reload Data Source
  95. NCMainCommon.sharedInstance.reloadDatasource(ServerUrl:self.metadata.serverUrl, ocId: self.metadata.ocId, action: k_action_MOD)
  96. // Enabled Button Action (the file is in local)
  97. self.detail.buttonAction.isEnabled = true
  98. }
  99. }
  100. }
  101. @objc func removeObserver() {
  102. appDelegate.player.removeObserver(self, forKeyPath: "rate", context: nil)
  103. NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
  104. }
  105. @objc func setupHTTPCache() {
  106. KTVHTTPCache.cacheSetMaxCacheLength(Int64(k_maxHTTPCache))
  107. if ProcessInfo.processInfo.environment["SIMULATOR_DEVICE_NAME"] != nil {
  108. KTVHTTPCache.logSetConsoleLogEnable(true)
  109. }
  110. do {
  111. try KTVHTTPCache.proxyStart()
  112. } catch let error {
  113. print("Proxy Start error : \(error)")
  114. }
  115. KTVHTTPCache.encodeSetURLConverter { (url) -> URL? in
  116. print("URL Filter reviced URL : " + String(describing: url))
  117. return url
  118. }
  119. KTVHTTPCache.downloadSetUnacceptableContentTypeDisposer { (url, contentType) -> Bool in
  120. print("Unsupport Content-Type Filter reviced URL : " + String(describing: url) + " " + String(describing: contentType))
  121. return false
  122. }
  123. }
  124. }