NCViewerProviderContextMenu.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. //
  2. // NCViewerProviderContextMenu.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 12/01/21.
  6. // Copyright © 2021 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 UIKit
  24. import NextcloudKit
  25. import SVGKit
  26. import MobileVLCKit
  27. class NCViewerProviderContextMenu: UIViewController {
  28. private let imageView = UIImageView()
  29. private var metadata: tableMetadata?
  30. private var metadataLivePhoto: tableMetadata?
  31. private var image: UIImage?
  32. private let player = VLCMediaPlayer()
  33. private let utilityFileSystem = NCUtilityFileSystem()
  34. private let sizeIcon: CGFloat = 150
  35. // MARK: - View Life Cycle
  36. required init?(coder: NSCoder) {
  37. fatalError("init(coder:) has not been implemented")
  38. }
  39. init(metadata: tableMetadata, image: UIImage?) {
  40. super.init(nibName: nil, bundle: nil)
  41. self.metadata = tableMetadata(value: metadata)
  42. self.metadataLivePhoto = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata)
  43. self.image = image
  44. if metadata.directory {
  45. imageView.image = NCImageCache.shared.getFolder(account: metadata.account)
  46. imageView.frame = resize(CGSize(width: sizeIcon, height: sizeIcon))
  47. } else {
  48. // ICON
  49. let image = NCUtility().loadImage(named: metadata.iconName, useTypeIconFile: true, account: metadata.account)
  50. imageView.image = image
  51. imageView.frame = resize(CGSize(width: sizeIcon, height: sizeIcon))
  52. // PREVIEW
  53. if let image = NCUtility().getImage(ocId: metadata.ocId, etag: metadata.etag, ext: NCGlobal.shared.previewExt512) {
  54. imageView.image = image
  55. imageView.frame = resize(image.size)
  56. }
  57. // VIEW IMAGE
  58. if metadata.isImage && utilityFileSystem.fileProviderStorageExists(metadata) {
  59. viewImage(metadata: metadata)
  60. }
  61. // VIEW LIVE PHOTO
  62. if let metadataLivePhoto = metadataLivePhoto, utilityFileSystem.fileProviderStorageExists(metadataLivePhoto) {
  63. viewVideo(metadata: metadataLivePhoto)
  64. }
  65. // VIEW VIDEO
  66. if metadata.isVideo {
  67. if !NCUtility().existsImage(ocId: metadata.ocId, etag: metadata.etag, ext: NCGlobal.shared.previewExt512) {
  68. let newSize = CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
  69. imageView.image = nil
  70. imageView.frame = newSize
  71. preferredContentSize = newSize.size
  72. }
  73. viewVideo(metadata: metadata)
  74. }
  75. // PLAY AUDIO
  76. if metadata.isAudio {
  77. var maxDownload: UInt64 = 0
  78. if utilityFileSystem.fileProviderStorageExists(metadata) {
  79. viewVideo(metadata: metadata)
  80. } else {
  81. if NCNetworking.shared.networkReachability == NKCommon.TypeReachability.reachableCellular {
  82. maxDownload = NCGlobal.shared.maxAutoDownloadCellular
  83. } else {
  84. maxDownload = NCGlobal.shared.maxAutoDownload
  85. }
  86. if metadata.size <= maxDownload,
  87. NCNetworking.shared.downloadQueue.operations.filter({ ($0 as? NCOperationDownload)?.metadata.ocId == metadata.ocId }).isEmpty {
  88. NCNetworking.shared.downloadQueue.addOperation(NCOperationDownload(metadata: metadata, selector: ""))
  89. }
  90. }
  91. }
  92. // AUTO DOWNLOAD IMAGE GIF
  93. if !utilityFileSystem.fileProviderStorageExists(metadata),
  94. NCNetworking.shared.isOnline,
  95. metadata.contentType == "image/gif",
  96. NCNetworking.shared.downloadQueue.operations.filter({ ($0 as? NCOperationDownload)?.metadata.ocId == metadata.ocId }).isEmpty {
  97. NCNetworking.shared.downloadQueue.addOperation(NCOperationDownload(metadata: metadata, selector: ""))
  98. }
  99. // AUTO DOWNLOAD IMAGE SVG
  100. if !utilityFileSystem.fileProviderStorageExists(metadata),
  101. NCNetworking.shared.isOnline,
  102. metadata.contentType == "image/svg+xml",
  103. NCNetworking.shared.downloadQueue.operations.filter({ ($0 as? NCOperationDownload)?.metadata.ocId == metadata.ocId }).isEmpty {
  104. NCNetworking.shared.downloadQueue.addOperation(NCOperationDownload(metadata: metadata, selector: ""))
  105. }
  106. // AUTO DOWNLOAD LIVE PHOTO
  107. if let metadataLivePhoto = self.metadataLivePhoto,
  108. NCNetworking.shared.isOnline,
  109. !utilityFileSystem.fileProviderStorageExists(metadataLivePhoto),
  110. NCNetworking.shared.downloadQueue.operations.filter({ ($0 as? NCOperationDownload)?.metadata.ocId == metadata.ocId }).isEmpty {
  111. NCNetworking.shared.downloadQueue.addOperation(NCOperationDownload(metadata: metadataLivePhoto, selector: ""))
  112. }
  113. }
  114. }
  115. override func loadView() {
  116. view = imageView
  117. imageView.contentMode = .scaleAspectFill
  118. }
  119. override func viewWillAppear(_ animated: Bool) {
  120. super.viewWillAppear(animated)
  121. NotificationCenter.default.addObserver(self, selector: #selector(downloadStartFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadStartFile), object: nil)
  122. NotificationCenter.default.addObserver(self, selector: #selector(downloadedFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedFile), object: nil)
  123. NotificationCenter.default.addObserver(self, selector: #selector(downloadCancelFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadCancelFile), object: nil)
  124. }
  125. override func viewWillDisappear(_ animated: Bool) {
  126. super.viewWillDisappear(animated)
  127. player.stop()
  128. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadStartFile), object: nil)
  129. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedFile), object: nil)
  130. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadCancelFile), object: nil)
  131. }
  132. // MARK: - NotificationCenter
  133. @objc func downloadStartFile(_ notification: NSNotification) {
  134. guard let userInfo = notification.userInfo as NSDictionary?,
  135. let ocId = userInfo["ocId"] as? String
  136. else { return }
  137. if ocId == self.metadata?.ocId || ocId == self.metadataLivePhoto?.ocId {
  138. DispatchQueue.main.async { NCActivityIndicator.shared.start(backgroundView: self.view) }
  139. }
  140. }
  141. @objc func downloadedFile(_ notification: NSNotification) {
  142. guard let userInfo = notification.userInfo as NSDictionary?,
  143. let ocId = userInfo["ocId"] as? String,
  144. let error = userInfo["error"] as? NKError,
  145. let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId)
  146. else { return }
  147. if error == .success && metadata.ocId == self.metadata?.ocId {
  148. if metadata.isImage {
  149. DispatchQueue.main.async {
  150. self.viewImage(metadata: metadata)
  151. }
  152. } else if metadata.isVideo {
  153. viewVideo(metadata: metadata)
  154. } else if metadata.isAudio {
  155. viewVideo(metadata: metadata)
  156. }
  157. }
  158. if error == .success && metadata.ocId == self.metadataLivePhoto?.ocId {
  159. viewVideo(metadata: metadata)
  160. }
  161. if ocId == self.metadata?.ocId || ocId == self.metadataLivePhoto?.ocId {
  162. NCActivityIndicator.shared.stop()
  163. }
  164. }
  165. @objc func downloadCancelFile(_ notification: NSNotification) {
  166. guard let userInfo = notification.userInfo as NSDictionary?,
  167. let ocId = userInfo["ocId"] as? String
  168. else { return }
  169. if ocId == self.metadata?.ocId || ocId == self.metadataLivePhoto?.ocId {
  170. NCActivityIndicator.shared.stop()
  171. }
  172. }
  173. // MARK: - Viewer
  174. private func viewImage(metadata: tableMetadata) {
  175. var image: UIImage?
  176. let filePath = utilityFileSystem.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)
  177. if metadata.contentType == "image/gif" {
  178. image = UIImage.animatedImage(withAnimatedGIFURL: URL(fileURLWithPath: filePath))
  179. } else if metadata.contentType == "image/svg+xml" {
  180. let imagePath = utilityFileSystem.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)
  181. if let svgImage = SVGKImage(contentsOfFile: imagePath) {
  182. svgImage.size = NCGlobal.shared.size1024
  183. image = svgImage.uiImage
  184. }
  185. } else {
  186. image = UIImage(contentsOfFile: filePath)
  187. }
  188. imageView.image = image
  189. imageView.frame = resize(image?.size)
  190. }
  191. private func viewVideo(metadata: tableMetadata) {
  192. NCNetworking.shared.getVideoUrl(metadata: metadata) { url, _, _ in
  193. if let url = url {
  194. self.player.media = VLCMedia(url: url)
  195. self.player.delegate = self
  196. self.player.media?.addOption(":http-user-agent=\(userAgent)")
  197. self.player.drawable = self.imageView
  198. self.player.play()
  199. }
  200. }
  201. }
  202. private func resize(_ size: CGSize?) -> CGRect {
  203. var frame = CGRect.zero
  204. guard let size = size else {
  205. preferredContentSize = frame.size
  206. return frame
  207. }
  208. if size.width <= UIScreen.main.bounds.width {
  209. frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
  210. preferredContentSize = frame.size
  211. return frame
  212. }
  213. let originRatio = size.width / size.height
  214. let newRatio = UIScreen.main.bounds.width / UIScreen.main.bounds.height
  215. var newSize = CGSize.zero
  216. if originRatio < newRatio {
  217. newSize.height = UIScreen.main.bounds.height
  218. newSize.width = UIScreen.main.bounds.height * originRatio
  219. } else {
  220. newSize.width = UIScreen.main.bounds.width
  221. newSize.height = UIScreen.main.bounds.width / originRatio
  222. }
  223. frame = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
  224. preferredContentSize = frame.size
  225. return frame
  226. }
  227. }
  228. extension NCViewerProviderContextMenu: VLCMediaPlayerDelegate {
  229. func mediaPlayerStateChanged(_ aNotification: Notification) {
  230. switch player.state {
  231. case .stopped:
  232. print("Played mode: STOPPED")
  233. case .opening:
  234. NCActivityIndicator.shared.start(backgroundView: self.view)
  235. print("Played mode: OPENING")
  236. case .buffering:
  237. print("Played mode: BUFFERING")
  238. case .ended:
  239. print("Played mode: ENDED")
  240. case .error:
  241. NCActivityIndicator.shared.stop()
  242. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_error_something_wrong_")
  243. NCContentPresenter().showError(error: error, priority: .max)
  244. print("Played mode: ERROR")
  245. case .playing:
  246. NCActivityIndicator.shared.stop()
  247. print("Played mode: PLAYING")
  248. case .paused:
  249. print("Played mode: PAUSED")
  250. default: break
  251. }
  252. }
  253. func mediaPlayerTimeChanged(_ aNotification: Notification) {
  254. // Handle other states...
  255. }
  256. func mediaPlayerTitleChanged(_ aNotification: Notification) {
  257. // Handle other states...
  258. }
  259. func mediaPlayerChapterChanged(_ aNotification: Notification) {
  260. // Handle other states...
  261. }
  262. func mediaPlayerLoudnessChanged(_ aNotification: Notification) {
  263. // Handle other states...
  264. }
  265. func mediaPlayerSnapshot(_ aNotification: Notification) {
  266. // Handle other states...
  267. }
  268. func mediaPlayerStartedRecording(_ player: VLCMediaPlayer) {
  269. // Handle other states...
  270. }
  271. func mediaPlayer(_ player: VLCMediaPlayer, recordingStoppedAtPath path: String) {
  272. // Handle other states...
  273. }
  274. }