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