NCViewerProviderContextMenu.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 NCCommunication
  25. class NCViewerProviderContextMenu: UIViewController {
  26. private let imageView = UIImageView()
  27. private var videoLayer: AVPlayerLayer?
  28. private var metadata: tableMetadata?
  29. private var metadataLivePhoto: tableMetadata?
  30. required init?(coder: NSCoder) {
  31. fatalError("init(coder:) has not been implemented")
  32. }
  33. init(metadata: tableMetadata) {
  34. super.init(nibName: nil, bundle: nil)
  35. self.metadata = metadata
  36. self.metadataLivePhoto = NCManageDatabase.shared.isLivePhoto(metadata: metadata)
  37. NotificationCenter.default.addObserver(self, selector: #selector(downloadStartFile(_:)), name: NSNotification.Name(rawValue: NCBrandGlobal.shared.notificationCenterDownloadStartFile), object: nil)
  38. NotificationCenter.default.addObserver(self, selector: #selector(downloadedFile(_:)), name: NSNotification.Name(rawValue: NCBrandGlobal.shared.notificationCenterDownloadedFile), object: nil)
  39. NotificationCenter.default.addObserver(self, selector: #selector(downloadCancelFile(_:)), name: NSNotification.Name(rawValue: NCBrandGlobal.shared.notificationCenterDownloadCancelFile), object: nil)
  40. if metadata.directory {
  41. imageView.image = UIImage(named: "folder")!.image(color: NCBrandColor.shared.brandElement, size: UIScreen.main.bounds.width / 2)
  42. imageView.frame = CGRect(x: 0, y: 0, width: imageView.image?.size.width ?? 0, height: imageView.image?.size.height ?? 0)
  43. preferredContentSize = imageView.frame.size
  44. } else {
  45. // ICON
  46. 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) {
  47. imageView.image = image
  48. imageView.frame = CGRect(x: 0, y: 0, width: imageView.image?.size.width ?? 0, height: imageView.image?.size.height ?? 0)
  49. preferredContentSize = imageView.frame.size
  50. }
  51. // PREVIEW
  52. if CCUtility.fileProviderStoragePreviewIconExists(metadata.ocId, etag: metadata.etag) {
  53. imageView.image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag))
  54. imageView.frame = CGRect(x: 0, y: 0, width: imageView.image?.size.width ?? 0, height: imageView.image?.size.height ?? 0)
  55. preferredContentSize = imageView.frame.size
  56. }
  57. // VIEW IMAGE
  58. if metadata.typeFile == NCBrandGlobal.shared.metadataTypeFileImage && CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  59. viewImage(metadata: metadata)
  60. }
  61. // VIEW LIVE PHOTO
  62. if metadataLivePhoto != nil && CCUtility.fileProviderStorageExists(metadataLivePhoto!.ocId, fileNameView: metadataLivePhoto!.fileNameView) {
  63. viewVideo(metadata: metadataLivePhoto!)
  64. }
  65. // VIEW VIDEO
  66. if (metadata.typeFile == NCBrandGlobal.shared.metadataTypeFileVideo && CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView)) {
  67. viewVideo(metadata: metadata)
  68. }
  69. // AUTO DOWNLOAD
  70. if !CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  71. var maxDownload: UInt64 = 0
  72. if NCNetworking.shared.networkReachability == NCCommunicationCommon.typeReachability.reachableCellular {
  73. maxDownload = NCBrandGlobal.shared.maxAutoDownloadCellular
  74. } else {
  75. maxDownload = NCBrandGlobal.shared.maxAutoDownload
  76. }
  77. if metadata.size <= maxDownload {
  78. NCOperationQueue.shared.download(metadata: metadata, selector: "", setFavorite: false)
  79. }
  80. }
  81. if let metadataLivePhoto = self.metadataLivePhoto {
  82. if !CCUtility.fileProviderStorageExists(metadataLivePhoto.ocId, fileNameView: metadataLivePhoto.fileNameView) {
  83. NCOperationQueue.shared.download(metadata: metadataLivePhoto, selector: "", setFavorite: false)
  84. }
  85. }
  86. }
  87. }
  88. override func loadView() {
  89. view = imageView
  90. imageView.contentMode = .scaleAspectFit
  91. }
  92. // MARK: - NotificationCenter
  93. override func viewDidLayoutSubviews() {
  94. super.viewDidLayoutSubviews()
  95. if let videoLayer = self.videoLayer {
  96. if videoLayer.frame.width == 0 && videoLayer.frame.height == 0 {
  97. videoLayer.frame = imageView.frame
  98. } else {
  99. imageView.frame = videoLayer.frame
  100. }
  101. }
  102. }
  103. @objc func downloadStartFile(_ notification: NSNotification) {
  104. if self.view?.window == nil { return }
  105. if let userInfo = notification.userInfo as NSDictionary? {
  106. if let ocId = userInfo["ocId"] as? String {
  107. if ocId == self.metadata?.ocId || ocId == self.metadataLivePhoto?.ocId {
  108. NCUtility.shared.startActivityIndicator(view: self.view)
  109. }
  110. }
  111. }
  112. }
  113. @objc func downloadedFile(_ notification: NSNotification) {
  114. if self.view?.window == nil { return }
  115. if let userInfo = notification.userInfo as NSDictionary? {
  116. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId), let errorCode = userInfo["errorCode"] as? Int {
  117. if errorCode == 0 && metadata.ocId == self.metadata?.ocId {
  118. if metadata.typeFile == NCBrandGlobal.shared.metadataTypeFileImage {
  119. viewImage(metadata: metadata)
  120. } else if metadata.typeFile == NCBrandGlobal.shared.metadataTypeFileVideo {
  121. viewVideo(metadata: metadata)
  122. }
  123. }
  124. if errorCode == 0 && metadata.ocId == self.metadataLivePhoto?.ocId {
  125. viewVideo(metadata: metadata)
  126. }
  127. if ocId == self.metadata?.ocId || ocId == self.metadataLivePhoto?.ocId {
  128. NCUtility.shared.stopActivityIndicator()
  129. }
  130. }
  131. }
  132. }
  133. @objc func downloadCancelFile(_ notification: NSNotification) {
  134. if self.view?.window == nil { return }
  135. if let userInfo = notification.userInfo as NSDictionary? {
  136. if let ocId = userInfo["ocId"] as? String {
  137. if ocId == self.metadata?.ocId || ocId == self.metadataLivePhoto?.ocId {
  138. NCUtility.shared.stopActivityIndicator()
  139. }
  140. }
  141. }
  142. }
  143. // MARK: - Viewer
  144. private func viewImage(metadata: tableMetadata) {
  145. var image: UIImage?
  146. let ext = CCUtility.getExtension(metadata.fileNameView)
  147. let filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  148. if ext == "GIF" {
  149. image = UIImage.animatedImage(withAnimatedGIFURL: URL(fileURLWithPath: filePath))
  150. } else {
  151. image = UIImage.init(contentsOfFile: filePath)
  152. }
  153. imageView.image = image
  154. imageView.frame = CGRect(x: 0, y: 0, width: image?.size.width ?? 0, height: image?.size.height ?? 0)
  155. preferredContentSize = imageView.frame.size
  156. }
  157. private func viewVideo(metadata: tableMetadata) {
  158. let filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  159. if let resolutionVideo = resolutionForLocalVideo(url: URL(fileURLWithPath: filePath)) {
  160. let originRatio = resolutionVideo.width / resolutionVideo.height
  161. let newRatio = UIScreen.main.bounds.width / UIScreen.main.bounds.height
  162. var newSize = resolutionVideo
  163. if originRatio < newRatio {
  164. newSize.height = UIScreen.main.bounds.height
  165. newSize.width = UIScreen.main.bounds.height * originRatio
  166. } else {
  167. newSize.width = UIScreen.main.bounds.width
  168. newSize.height = UIScreen.main.bounds.width / originRatio
  169. }
  170. let player = AVPlayer(url: URL(fileURLWithPath: filePath))
  171. self.videoLayer = AVPlayerLayer(player: player)
  172. if let videoLayer = self.videoLayer {
  173. videoLayer.videoGravity = .resizeAspectFill
  174. imageView.image = nil
  175. imageView.frame = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
  176. imageView.layer.addSublayer(videoLayer)
  177. }
  178. player.isMuted = true
  179. player.play()
  180. preferredContentSize = imageView.frame.size
  181. }
  182. }
  183. private func resolutionForLocalVideo(url: URL) -> CGSize? {
  184. guard let track = AVURLAsset(url: url).tracks(withMediaType: AVMediaType.video).first else { return nil }
  185. let size = track.naturalSize.applying(track.preferredTransform)
  186. return CGSize(width: abs(size.width), height: abs(size.height))
  187. }
  188. }