NCViewerMedia.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // NCViewerMedia.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 21/09/18.
  6. // Copyright © 2018 TWS. All rights reserved.
  7. //
  8. import Foundation
  9. import KTVHTTPCache
  10. class NCViewerMedia: NSObject {
  11. @objc static let sharedInstance: NCViewerMedia = {
  12. let instance = NCViewerMedia()
  13. return instance
  14. }()
  15. var viewDetail: CCDetail!
  16. var metadata: tableMetadata!
  17. var videoURL: URL!
  18. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  19. @objc func viewMedia(_ metadata: tableMetadata, viewDetail: CCDetail, width: Int, height: Int) {
  20. var videoURLProxy: URL!
  21. self.viewDetail = viewDetail
  22. self.metadata = metadata
  23. guard let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata.directoryID) else {
  24. return
  25. }
  26. if CCUtility.fileProviderStorageExists(metadata.fileID, fileNameView: metadata.fileNameView) {
  27. self.videoURL = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageFileID(metadata.fileID, fileNameView: metadata.fileNameView))
  28. videoURLProxy = videoURL
  29. } else {
  30. guard let stringURL = (serverUrl + "/" + metadata.fileName).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
  31. return
  32. }
  33. self.videoURL = URL(string: stringURL)
  34. videoURLProxy = KTVHTTPCache.proxyURL(withOriginalURL: self.videoURL)
  35. guard let authData = (appDelegate.activeUser + ":" + appDelegate.activePassword).data(using: .utf8) else {
  36. return
  37. }
  38. let authValue = "Basic " + authData.base64EncodedString(options: [])
  39. let header = [authValue:"Authorization", CCUtility.getUserAgent():"User-Agent"] as [String : String]
  40. KTVHTTPCache.downloadSetAdditionalHeaders(header)
  41. // Disable Button Action (the file is in download via Proxy Server)
  42. viewDetail.buttonAction.isEnabled = false
  43. }
  44. appDelegate.player = AVPlayer(url: videoURLProxy)
  45. appDelegate.playerController = AVPlayerViewController()
  46. appDelegate.playerController.player = appDelegate.player
  47. appDelegate.playerController.view.frame = CGRect(x: 0, y: 0, width: width, height: height)
  48. appDelegate.playerController.allowsPictureInPicturePlayback = false
  49. viewDetail.addChild(appDelegate.playerController)
  50. viewDetail.view.addSubview(appDelegate.playerController.view)
  51. appDelegate.playerController.didMove(toParent: viewDetail)
  52. NotificationCenter.default.addObserver(self, selector: #selector(self.itemDidFinishPlaying(notification:)), name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
  53. appDelegate.player.addObserver(self, forKeyPath: "rate", options: [], context: nil)
  54. viewDetail.isMediaObserver = true
  55. appDelegate.player.play()
  56. }
  57. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  58. if keyPath == "rate" {
  59. if appDelegate.player?.rate != nil {
  60. print("start")
  61. } else {
  62. print("stop")
  63. }
  64. saveCacheToFileProvider()
  65. }
  66. }
  67. @objc func removeObserverAVPlayerItemDidPlayToEndTime () {
  68. NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
  69. }
  70. @objc func itemDidFinishPlaying(notification: NSNotification) {
  71. let player = notification.object as! AVPlayerItem
  72. player.seek(to: CMTime.zero)
  73. }
  74. func saveCacheToFileProvider() {
  75. if !CCUtility.fileProviderStorageExists(self.metadata.fileID, fileNameView:self.metadata.fileNameView) {
  76. guard let url = KTVHTTPCache.cacheCompleteFileURLIfExisted(with: self.videoURL) else {
  77. return
  78. }
  79. CCUtility.copyFile(atPath: url.path, toPath: CCUtility.getDirectoryProviderStorageFileID(self.metadata.fileID, fileNameView: self.metadata.fileNameView))
  80. NCManageDatabase.sharedInstance.addLocalFile(metadata: self.metadata)
  81. KTVHTTPCache.cacheDelete(with: self.videoURL)
  82. // reload Data Source
  83. NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: NCManageDatabase.sharedInstance.getServerUrl(self.metadata.directoryID), fileID: self.metadata.fileID, action: k_action_MOD)
  84. // Enabled Button Action (the file is in local)
  85. self.viewDetail.buttonAction.isEnabled = true
  86. }
  87. }
  88. @objc func setupHTTPCache() {
  89. var error: NSError?
  90. KTVHTTPCache.cacheSetMaxCacheLength(Int64(k_maxHTTPCache))
  91. if ProcessInfo.processInfo.environment["SIMULATOR_DEVICE_NAME"] != nil {
  92. KTVHTTPCache.logSetConsoleLogEnable(true)
  93. }
  94. KTVHTTPCache.proxyStart(&error)
  95. if error == nil {
  96. print("Proxy Start Success")
  97. } else {
  98. print("Proxy Start error : \(error!)")
  99. }
  100. KTVHTTPCache.tokenSetURLFilter { (url) -> URL? in
  101. print("URL Filter reviced URL : \(url!)")
  102. return url
  103. }
  104. KTVHTTPCache.downloadSetUnsupportContentTypeFilter { (url, contentType) -> Bool in
  105. print("Unsupport Content-Type Filter reviced URL : \(url!) \(contentType!)")
  106. return false
  107. }
  108. }
  109. }