NCViewerProviderContextMenu.swift 8.8 KB

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