NCViewerMedia.swift 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 viewMedia = NCViewerMedia()
  13. viewMedia.setupHTTPCache()
  14. return viewMedia
  15. }()
  16. var viewDetail: CCDetail!
  17. var metadata: tableMetadata!
  18. var videoURL: URL!
  19. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  20. @objc func viewMedia(_ metadata: tableMetadata, viewDetail: CCDetail, width: Int, height: Int) {
  21. var videoURLProxy: URL!
  22. self.viewDetail = viewDetail
  23. self.metadata = metadata
  24. guard let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata.directoryID) else {
  25. return
  26. }
  27. if CCUtility.fileProviderStorageExists(metadata.fileID, fileNameView: metadata.fileNameView) {
  28. self.videoURL = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageFileID(metadata.fileID, fileNameView: metadata.fileNameView))
  29. videoURLProxy = videoURL
  30. } else {
  31. guard let stringURL = (serverUrl + "/" + metadata.fileName).addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else {
  32. return
  33. }
  34. self.videoURL = URL(string: stringURL)
  35. videoURLProxy = KTVHTTPCache.proxyURL(withOriginalURL: self.videoURL)
  36. guard let authData = (appDelegate.activeUser + ":" + appDelegate.activePassword).data(using: .utf8) else {
  37. return
  38. }
  39. let authValue = "Basic " + authData.base64EncodedString(options: [])
  40. KTVHTTPCache.downloadSetAdditionalHeaders(["Authorization":authValue, "User-Agent":CCUtility.getUserAgent()])
  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(forName: .AVPlayerItemDidPlayToEndTime, object: nil, queue: nil) { (notification) in
  53. let player = notification.object as! AVPlayerItem
  54. player.seek(to: CMTime.zero)
  55. }
  56. appDelegate.player.addObserver(self, forKeyPath: "rate", options: [], context: nil)
  57. viewDetail.isMediaObserver = true
  58. appDelegate.player.play()
  59. }
  60. override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
  61. if keyPath != nil && keyPath == "rate" {
  62. if appDelegate.player.rate == 1 {
  63. print("start")
  64. } else {
  65. print("stop")
  66. }
  67. // Save cache
  68. if !CCUtility.fileProviderStorageExists(self.metadata.fileID, fileNameView:self.metadata.fileNameView) {
  69. guard let url = KTVHTTPCache.cacheCompleteFileURLIfExisted(with: self.videoURL) else {
  70. return
  71. }
  72. CCUtility.copyFile(atPath: url.path, toPath: CCUtility.getDirectoryProviderStorageFileID(self.metadata.fileID, fileNameView: self.metadata.fileNameView))
  73. NCManageDatabase.sharedInstance.addLocalFile(metadata: self.metadata)
  74. KTVHTTPCache.cacheDelete(with: self.videoURL)
  75. // reload Data Source
  76. NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: NCManageDatabase.sharedInstance.getServerUrl(self.metadata.directoryID), fileID: self.metadata.fileID, action: k_action_MOD)
  77. // Enabled Button Action (the file is in local)
  78. self.viewDetail.buttonAction.isEnabled = true
  79. }
  80. }
  81. }
  82. @objc func removeObserver() {
  83. appDelegate.player.removeObserver(self, forKeyPath: "rate", context: nil)
  84. NotificationCenter.default.removeObserver(self, name: NSNotification.Name.AVPlayerItemDidPlayToEndTime, object: nil)
  85. }
  86. @objc func setupHTTPCache() {
  87. var error: NSError?
  88. KTVHTTPCache.cacheSetMaxCacheLength(Int64(k_maxHTTPCache))
  89. if ProcessInfo.processInfo.environment["SIMULATOR_DEVICE_NAME"] != nil {
  90. KTVHTTPCache.logSetConsoleLogEnable(true)
  91. }
  92. KTVHTTPCache.proxyStart(&error)
  93. if error == nil {
  94. print("Proxy Start Success")
  95. } else {
  96. print("Proxy Start error : \(error!)")
  97. }
  98. KTVHTTPCache.tokenSetURLFilter { (url) -> URL? in
  99. print("URL Filter reviced URL : " + String(describing: url))
  100. return url
  101. }
  102. KTVHTTPCache.downloadSetUnsupportContentTypeFilter { (url, contentType) -> Bool in
  103. print("Unsupport Content-Type Filter reviced URL : " + String(describing: url) + " " + String(describing: contentType))
  104. return false
  105. }
  106. }
  107. }