NCViewerProviderContextMenu.swift 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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. NCOperationQueue.shared.download(metadata: metadata, selector: "", setFavorite: false)
  70. }
  71. if let metadataLivePhoto = self.metadataLivePhoto {
  72. if !CCUtility.fileProviderStorageExists(metadataLivePhoto.ocId, fileNameView: metadataLivePhoto.fileNameView) {
  73. NCOperationQueue.shared.download(metadata: metadataLivePhoto, selector: "", setFavorite: false)
  74. }
  75. }
  76. }
  77. }
  78. override func loadView() {
  79. view = imageView
  80. imageView.contentMode = .scaleAspectFit
  81. }
  82. override func viewDidLayoutSubviews() {
  83. super.viewDidLayoutSubviews()
  84. if let videoLayer = self.videoLayer {
  85. if videoLayer.frame.width == 0 && videoLayer.frame.height == 0 {
  86. videoLayer.frame = imageView.frame
  87. } else {
  88. imageView.frame = videoLayer.frame
  89. }
  90. }
  91. }
  92. @objc func downloadedFile(_ notification: NSNotification) {
  93. if self.view?.window == nil { return }
  94. if let userInfo = notification.userInfo as NSDictionary? {
  95. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId), let errorCode = userInfo["errorCode"] as? Int {
  96. if errorCode == 0 && metadata.ocId == self.metadata?.ocId {
  97. if metadata.typeFile == NCBrandGlobal.shared.metadataTypeFileImage {
  98. viewImage(metadata: metadata)
  99. } else if metadata.typeFile == NCBrandGlobal.shared.metadataTypeFileVideo {
  100. viewVideo(metadata: metadata)
  101. }
  102. }
  103. if errorCode == 0 && metadata.ocId == self.metadataLivePhoto?.ocId {
  104. viewVideo(metadata: metadata)
  105. }
  106. }
  107. }
  108. }
  109. private func viewImage(metadata: tableMetadata) {
  110. var image: UIImage?
  111. let ext = CCUtility.getExtension(metadata.fileNameView)
  112. let filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  113. if ext == "GIF" {
  114. image = UIImage.animatedImage(withAnimatedGIFURL: URL(fileURLWithPath: filePath))
  115. } else {
  116. image = UIImage.init(contentsOfFile: filePath)
  117. }
  118. imageView.image = image
  119. imageView.frame = CGRect(x: 0, y: 0, width: image?.size.width ?? 0, height: image?.size.height ?? 0)
  120. preferredContentSize = imageView.frame.size
  121. }
  122. private func viewVideo(metadata: tableMetadata) {
  123. let filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  124. if let resolutionVideo = resolutionForLocalVideo(url: URL(fileURLWithPath: filePath)) {
  125. let originRatio = resolutionVideo.width / resolutionVideo.height
  126. let newRatio = UIScreen.main.bounds.width / UIScreen.main.bounds.height
  127. var newSize = resolutionVideo
  128. if originRatio < newRatio {
  129. newSize.height = UIScreen.main.bounds.height
  130. newSize.width = UIScreen.main.bounds.height * originRatio
  131. } else {
  132. newSize.width = UIScreen.main.bounds.width
  133. newSize.height = UIScreen.main.bounds.width / originRatio
  134. }
  135. let player = AVPlayer(url: URL(fileURLWithPath: filePath))
  136. self.videoLayer = AVPlayerLayer(player: player)
  137. if let videoLayer = self.videoLayer {
  138. videoLayer.videoGravity = .resizeAspectFill
  139. imageView.image = nil
  140. imageView.frame = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
  141. imageView.layer.addSublayer(videoLayer)
  142. }
  143. player.isMuted = true
  144. player.play()
  145. preferredContentSize = imageView.frame.size
  146. }
  147. }
  148. private func resolutionForLocalVideo(url: URL) -> CGSize? {
  149. guard let track = AVURLAsset(url: url).tracks(withMediaType: AVMediaType.video).first else { return nil }
  150. let size = track.naturalSize.applying(track.preferredTransform)
  151. return CGSize(width: abs(size.width), height: abs(size.height))
  152. }
  153. }