NCViewerProviderContextMenu.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. imageView.image = UIImage(named: "folder")!.image(color: NCBrandColor.shared.brandElement, size: UIScreen.main.bounds.width / 2)
  44. imageView.frame = CGRect(x: 0, y: 0, width: imageView.image?.size.width ?? 0, height: imageView.image?.size.height ?? 0)
  45. preferredContentSize = imageView.frame.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 = CGRect(x: 0, y: 0, width: imageView.image?.size.width ?? 0, height: imageView.image?.size.height ?? 0)
  51. preferredContentSize = imageView.frame.size
  52. }
  53. // PREVIEW
  54. if CCUtility.fileProviderStoragePreviewIconExists(metadata.ocId, etag: metadata.etag) {
  55. if let image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag)) {
  56. imageView.image = image
  57. imageView.frame = CGRect(x: 0, y: 0, width: imageView.image?.size.width ?? 0, height: imageView.image?.size.height ?? 0)
  58. }
  59. preferredContentSize = imageView.frame.size
  60. }
  61. // VIEW IMAGE
  62. if metadata.typeFile == NCBrandGlobal.shared.metadataTypeFileImage && CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  63. viewImage(metadata: metadata)
  64. }
  65. // VIEW LIVE PHOTO
  66. if metadataLivePhoto != nil && CCUtility.fileProviderStorageExists(metadataLivePhoto!.ocId, fileNameView: metadataLivePhoto!.fileNameView) {
  67. viewVideo(metadata: metadataLivePhoto!)
  68. }
  69. // VIEW VIDEO
  70. if metadata.typeFile == NCBrandGlobal.shared.metadataTypeFileVideo && CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  71. viewVideo(metadata: metadata)
  72. }
  73. // PLAY SOUND
  74. if metadata.typeFile == NCBrandGlobal.shared.metadataTypeFileAudio && CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  75. playSound(metadata: metadata)
  76. }
  77. // AUTO DOWNLOAD VIDEO / AUDIO
  78. if !CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) && (metadata.typeFile == NCBrandGlobal.shared.metadataTypeFileVideo || metadata.typeFile == NCBrandGlobal.shared.metadataTypeFileAudio) {
  79. var maxDownload: UInt64 = 0
  80. if NCNetworking.shared.networkReachability == NCCommunicationCommon.typeReachability.reachableCellular {
  81. maxDownload = NCBrandGlobal.shared.maxAutoDownloadCellular
  82. } else {
  83. maxDownload = NCBrandGlobal.shared.maxAutoDownload
  84. }
  85. if metadata.size <= maxDownload {
  86. NCOperationQueue.shared.download(metadata: metadata, selector: "")
  87. }
  88. }
  89. // AUTO DOWNLOAD IMAGE GIF
  90. if !CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) && metadata.contentType == "image/gif" {
  91. NCOperationQueue.shared.download(metadata: metadata, selector: "")
  92. }
  93. // AUTO DOWNLOAD LIVE PHOTO
  94. if let metadataLivePhoto = self.metadataLivePhoto {
  95. if !CCUtility.fileProviderStorageExists(metadataLivePhoto.ocId, fileNameView: metadataLivePhoto.fileNameView) {
  96. NCOperationQueue.shared.download(metadata: metadataLivePhoto, selector: "")
  97. }
  98. }
  99. }
  100. }
  101. override func loadView() {
  102. view = imageView
  103. imageView.contentMode = .scaleAspectFill
  104. }
  105. override func viewDidLayoutSubviews() {
  106. super.viewDidLayoutSubviews()
  107. if let videoLayer = self.videoLayer {
  108. if videoLayer.frame == CGRect.zero {
  109. videoLayer.frame = imageView.frame
  110. } else {
  111. imageView.frame = videoLayer.frame
  112. }
  113. }
  114. preferredContentSize = imageView.frame.size
  115. }
  116. // MARK: - NotificationCenter
  117. @objc func downloadStartFile(_ notification: NSNotification) {
  118. if self.view?.window == nil { return }
  119. if let userInfo = notification.userInfo as NSDictionary? {
  120. if let ocId = userInfo["ocId"] as? String {
  121. if ocId == self.metadata?.ocId || ocId == self.metadataLivePhoto?.ocId {
  122. NCUtility.shared.startActivityIndicator(view: self.view)
  123. }
  124. }
  125. }
  126. }
  127. @objc func downloadedFile(_ notification: NSNotification) {
  128. if self.view?.window == nil { return }
  129. if let userInfo = notification.userInfo as NSDictionary? {
  130. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId), let errorCode = userInfo["errorCode"] as? Int {
  131. if errorCode == 0 && metadata.ocId == self.metadata?.ocId {
  132. if metadata.typeFile == NCBrandGlobal.shared.metadataTypeFileImage {
  133. viewImage(metadata: metadata)
  134. } else if metadata.typeFile == NCBrandGlobal.shared.metadataTypeFileVideo {
  135. viewVideo(metadata: metadata)
  136. } else if metadata.typeFile == NCBrandGlobal.shared.metadataTypeFileAudio {
  137. playSound(metadata: metadata)
  138. }
  139. }
  140. if errorCode == 0 && metadata.ocId == self.metadataLivePhoto?.ocId {
  141. viewVideo(metadata: metadata)
  142. }
  143. if ocId == self.metadata?.ocId || ocId == self.metadataLivePhoto?.ocId {
  144. NCUtility.shared.stopActivityIndicator()
  145. }
  146. }
  147. }
  148. }
  149. @objc func downloadCancelFile(_ notification: NSNotification) {
  150. if self.view?.window == nil { return }
  151. if let userInfo = notification.userInfo as NSDictionary? {
  152. if let ocId = userInfo["ocId"] as? String {
  153. if ocId == self.metadata?.ocId || ocId == self.metadataLivePhoto?.ocId {
  154. NCUtility.shared.stopActivityIndicator()
  155. }
  156. }
  157. }
  158. }
  159. // MARK: - Viewer
  160. private func viewImage(metadata: tableMetadata) {
  161. var image: UIImage?
  162. let filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  163. if metadata.contentType == "image/gif" {
  164. image = UIImage.animatedImage(withAnimatedGIFURL: URL(fileURLWithPath: filePath))
  165. } else {
  166. image = UIImage.init(contentsOfFile: filePath)
  167. }
  168. imageView.image = image
  169. imageView.frame = CGRect(x: 0, y: 0, width: imageView.image?.size.width ?? 0, height: imageView.image?.size.height ?? 0)
  170. preferredContentSize = imageView.frame.size
  171. }
  172. func playSound(metadata: tableMetadata) {
  173. let filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  174. do {
  175. try AVAudioSession.sharedInstance().setCategory(.playback, mode: .default)
  176. try AVAudioSession.sharedInstance().setActive(true)
  177. audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: filePath), fileTypeHint: AVFileType.mp3.rawValue)
  178. guard let player = audioPlayer else { return }
  179. player.play()
  180. } catch let error {
  181. print(error.localizedDescription)
  182. }
  183. preferredContentSize = imageView.frame.size
  184. }
  185. private func viewVideo(metadata: tableMetadata) {
  186. let filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  187. if let resolutionVideo = resolutionForLocalVideo(url: URL(fileURLWithPath: filePath)) {
  188. let originRatio = resolutionVideo.width / resolutionVideo.height
  189. let newRatio = UIScreen.main.bounds.width / UIScreen.main.bounds.height
  190. var newSize = resolutionVideo
  191. if originRatio < newRatio {
  192. newSize.height = UIScreen.main.bounds.height
  193. newSize.width = UIScreen.main.bounds.height * originRatio
  194. } else {
  195. newSize.width = UIScreen.main.bounds.width
  196. newSize.height = UIScreen.main.bounds.width / originRatio
  197. }
  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 = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
  204. imageView.layer.addSublayer(videoLayer)
  205. }
  206. player.isMuted = true
  207. player.play()
  208. preferredContentSize = imageView.frame.size
  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 resizeImage(_ image: UIImage) -> UIImage {
  217. if (image.size.width <= image.size.height) && (image.size.width >= UIScreen.main.bounds.width) {
  218. if let image = image.resizeImage(size: CGSize(width: UIScreen.main.bounds.width/2, height: UIScreen.main.bounds.height/2), isAspectRation: true) {
  219. return image
  220. }
  221. }
  222. return image
  223. }
  224. }