NCViewerProviderContextMenu.swift 13 KB

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