NCViewerProviderContextMenu.swift 13 KB

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