NCViewerProviderContextMenu.swift 13 KB

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