NCViewerProviderContextMenu.swift 12 KB

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