NCViewerProviderContextMenu.swift 13 KB

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