NCViewerProviderContextMenu.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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 AVFoundation
  25. import NCCommunication
  26. import SVGKit
  27. class NCViewerProviderContextMenu: UIViewController {
  28. private let imageView = UIImageView()
  29. private var videoLayer: AVPlayerLayer?
  30. private var audioPlayer: AVAudioPlayer?
  31. private var metadata: tableMetadata?
  32. private var metadataLivePhoto: tableMetadata?
  33. private var image: UIImage?
  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 = 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
  50. imageView.frame = resize(CGSize(width: sizeIcon, height: sizeIcon))
  51. } else {
  52. // ICON
  53. if let image = UIImage.init(named: metadata.iconName)?.resizeImage(size: CGSize(width: sizeIcon*2, height: sizeIcon*2), isAspectRation: true) {
  54. imageView.image = image
  55. imageView.frame = resize(CGSize(width: sizeIcon, height: sizeIcon))
  56. }
  57. // PREVIEW
  58. if CCUtility.fileProviderStoragePreviewIconExists(metadata.ocId, etag: metadata.etag) {
  59. if let image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag)) {
  60. imageView.image = image
  61. imageView.frame = resize(image.size)
  62. }
  63. }
  64. // VIEW IMAGE
  65. if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue && CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  66. viewImage(metadata: metadata)
  67. }
  68. // VIEW LIVE PHOTO
  69. if metadataLivePhoto != nil && CCUtility.fileProviderStorageExists(metadataLivePhoto!.ocId, fileNameView: metadataLivePhoto!.fileNameView) {
  70. viewVideo(metadata: metadataLivePhoto!)
  71. }
  72. // VIEW VIDEO
  73. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue && CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  74. viewVideo(metadata: metadata)
  75. }
  76. // PLAY SOUND
  77. if metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue && CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  78. playSound(metadata: metadata)
  79. }
  80. // AUTO DOWNLOAD VIDEO / AUDIO
  81. // if !CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) && (metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue || metadata.contentType == "application/pdf") {
  82. if !CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) && (metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue) {
  83. var maxDownload: UInt64 = 0
  84. if NCNetworking.shared.networkReachability == NCCommunicationCommon.typeReachability.reachableCellular {
  85. maxDownload = NCGlobal.shared.maxAutoDownloadCellular
  86. } else {
  87. maxDownload = NCGlobal.shared.maxAutoDownload
  88. }
  89. if metadata.size <= maxDownload {
  90. NCOperationQueue.shared.download(metadata: metadata, selector: "")
  91. }
  92. }
  93. // AUTO DOWNLOAD IMAGE GIF
  94. if !CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) && metadata.contentType == "image/gif" {
  95. NCOperationQueue.shared.download(metadata: metadata, selector: "")
  96. }
  97. // AUTO DOWNLOAD IMAGE SVG
  98. if !CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) && metadata.contentType == "image/svg+xml" {
  99. NCOperationQueue.shared.download(metadata: metadata, selector: "")
  100. }
  101. // AUTO DOWNLOAD LIVE PHOTO
  102. if let metadataLivePhoto = self.metadataLivePhoto {
  103. if !CCUtility.fileProviderStorageExists(metadataLivePhoto.ocId, fileNameView: metadataLivePhoto.fileNameView) {
  104. NCOperationQueue.shared.download(metadata: metadataLivePhoto, selector: "")
  105. }
  106. }
  107. }
  108. }
  109. override func loadView() {
  110. view = imageView
  111. imageView.contentMode = .scaleAspectFill
  112. }
  113. override func viewWillAppear(_ animated: Bool) {
  114. super.viewWillAppear(animated)
  115. NotificationCenter.default.addObserver(self, selector: #selector(downloadStartFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadStartFile), object: nil)
  116. NotificationCenter.default.addObserver(self, selector: #selector(downloadedFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedFile), object: nil)
  117. NotificationCenter.default.addObserver(self, selector: #selector(downloadCancelFile(_:)), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadCancelFile), object: nil)
  118. }
  119. override func viewWillDisappear(_ animated: Bool) {
  120. super.viewWillDisappear(animated)
  121. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadStartFile), object: nil)
  122. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedFile), object: nil)
  123. NotificationCenter.default.removeObserver(self, name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadCancelFile), object: nil)
  124. }
  125. override func viewDidLayoutSubviews() {
  126. super.viewDidLayoutSubviews()
  127. if let videoLayer = self.videoLayer {
  128. if videoLayer.frame == CGRect.zero {
  129. videoLayer.frame = imageView.frame
  130. } else {
  131. imageView.frame = videoLayer.frame
  132. }
  133. }
  134. preferredContentSize = imageView.frame.size
  135. }
  136. // MARK: - NotificationCenter
  137. @objc func downloadStartFile(_ notification: NSNotification) {
  138. if let userInfo = notification.userInfo as NSDictionary? {
  139. if let ocId = userInfo["ocId"] as? String {
  140. if ocId == self.metadata?.ocId || ocId == self.metadataLivePhoto?.ocId {
  141. NCUtility.shared.startActivityIndicator(backgroundView: self.view, blurEffect: false)
  142. }
  143. }
  144. }
  145. }
  146. @objc func downloadedFile(_ notification: NSNotification) {
  147. if let userInfo = notification.userInfo as NSDictionary? {
  148. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId), let errorCode = userInfo["errorCode"] as? Int {
  149. if errorCode == 0 && metadata.ocId == self.metadata?.ocId {
  150. if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue {
  151. viewImage(metadata: metadata)
  152. } else if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
  153. viewVideo(metadata: metadata)
  154. } else if metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue {
  155. playSound(metadata: metadata)
  156. }
  157. }
  158. if errorCode == 0 && metadata.ocId == self.metadataLivePhoto?.ocId {
  159. viewVideo(metadata: metadata)
  160. }
  161. if ocId == self.metadata?.ocId || ocId == self.metadataLivePhoto?.ocId {
  162. NCUtility.shared.stopActivityIndicator()
  163. }
  164. }
  165. }
  166. }
  167. @objc func downloadCancelFile(_ notification: NSNotification) {
  168. if let userInfo = notification.userInfo as NSDictionary? {
  169. if let ocId = userInfo["ocId"] as? String {
  170. if ocId == self.metadata?.ocId || ocId == self.metadataLivePhoto?.ocId {
  171. NCUtility.shared.stopActivityIndicator()
  172. }
  173. }
  174. }
  175. }
  176. // MARK: - Viewer
  177. private func viewImage(metadata: tableMetadata) {
  178. var image: UIImage?
  179. let filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  180. if metadata.contentType == "image/gif" {
  181. image = UIImage.animatedImage(withAnimatedGIFURL: URL(fileURLWithPath: filePath))
  182. } else if metadata.contentType == "image/svg+xml" {
  183. let imagePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  184. if let svgImage = SVGKImage(contentsOfFile: imagePath) {
  185. svgImage.size = CGSize(width: NCGlobal.shared.sizePreview, height: NCGlobal.shared.sizePreview)
  186. image = svgImage.uiImage
  187. }
  188. } else {
  189. image = UIImage.init(contentsOfFile: filePath)
  190. }
  191. imageView.image = image
  192. imageView.frame = resize(image?.size)
  193. }
  194. func playSound(metadata: tableMetadata) {
  195. let filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  196. do {
  197. audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: filePath), fileTypeHint: AVFileType.mp3.rawValue)
  198. guard let player = audioPlayer else { return }
  199. player.play()
  200. } catch let error {
  201. print(error.localizedDescription)
  202. }
  203. preferredContentSize = imageView.frame.size
  204. }
  205. private func viewVideo(metadata: tableMetadata) {
  206. let filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  207. if let resolutionVideo = resolutionForLocalVideo(url: URL(fileURLWithPath: filePath)) {
  208. let player = AVPlayer(url: URL(fileURLWithPath: filePath))
  209. self.videoLayer = AVPlayerLayer(player: player)
  210. if let videoLayer = self.videoLayer {
  211. videoLayer.videoGravity = .resizeAspectFill
  212. imageView.image = nil
  213. imageView.frame = resize(resolutionVideo)
  214. imageView.layer.addSublayer(videoLayer)
  215. }
  216. player.isMuted = true
  217. player.play()
  218. }
  219. }
  220. private func resolutionForLocalVideo(url: URL) -> CGSize? {
  221. guard let track = AVURLAsset(url: url).tracks(withMediaType: AVMediaType.video).first else { return nil }
  222. let size = track.naturalSize.applying(track.preferredTransform)
  223. return CGSize(width: abs(size.width), height: abs(size.height))
  224. }
  225. private func resize(_ size: CGSize?) -> CGRect {
  226. var frame = CGRect.zero
  227. guard let size = size else {
  228. preferredContentSize = frame.size
  229. return frame
  230. }
  231. if size.width <= UIScreen.main.bounds.width {
  232. frame = CGRect(x: 0, y: 0, width: size.width, height: size.height)
  233. preferredContentSize = frame.size
  234. return frame
  235. }
  236. let originRatio = size.width / size.height
  237. let newRatio = UIScreen.main.bounds.width / UIScreen.main.bounds.height
  238. var newSize = CGSize.zero
  239. if originRatio < newRatio {
  240. newSize.height = UIScreen.main.bounds.height
  241. newSize.width = UIScreen.main.bounds.height * originRatio
  242. } else {
  243. newSize.width = UIScreen.main.bounds.width
  244. newSize.height = UIScreen.main.bounds.width / originRatio
  245. }
  246. frame = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
  247. preferredContentSize = frame.size
  248. return frame
  249. }
  250. }