NCViewerProviderContextMenu.swift 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. override func loadView() {
  31. view = imageView
  32. imageView.contentMode = .scaleAspectFit
  33. preferredContentSize = imageView.frame.size
  34. }
  35. override func viewDidLayoutSubviews() {
  36. super.viewDidLayoutSubviews()
  37. if let videoLayer = self.videoLayer {
  38. videoLayer.frame = imageView.layer.bounds
  39. }
  40. preferredContentSize = imageView.frame.size
  41. }
  42. init(metadata: tableMetadata) {
  43. super.init(nibName: nil, bundle: nil)
  44. self.metadata = metadata
  45. self.metadataLivePhoto = NCManageDatabase.shared.isLivePhoto(metadata: metadata)
  46. NotificationCenter.default.addObserver(self, selector: #selector(downloadedFile(_:)), name: NSNotification.Name(rawValue: NCBrandGlobal.shared.notificationCenterDownloadedFile), object: nil)
  47. if metadata.directory {
  48. imageView.image = UIImage(named: "folder")!.image(color: NCBrandColor.shared.brandElement, size: UIScreen.main.bounds.width / 2)
  49. imageView.frame = CGRect(x: 0, y: 0, width: imageView.image?.size.width ?? 0, height: imageView.image?.size.height ?? 0)
  50. } else {
  51. // ICON
  52. 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) {
  53. imageView.image = image
  54. imageView.frame = CGRect(x: 0, y: 0, width: imageView.image?.size.width ?? 0, height: imageView.image?.size.height ?? 0)
  55. }
  56. // PREVIEW
  57. if CCUtility.fileProviderStoragePreviewIconExists(metadata.ocId, etag: metadata.etag) {
  58. imageView.image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag))
  59. imageView.frame = CGRect(x: 0, y: 0, width: imageView.image?.size.width ?? 0, height: imageView.image?.size.height ?? 0)
  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. // AUTO DOWNLOAD
  74. if !CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  75. NCOperationQueue.shared.download(metadata: metadata, selector: "", setFavorite: false)
  76. }
  77. if let metadataLivePhoto = self.metadataLivePhoto {
  78. if !CCUtility.fileProviderStorageExists(metadataLivePhoto.ocId, fileNameView: metadataLivePhoto.fileNameView) {
  79. NCOperationQueue.shared.download(metadata: metadataLivePhoto, selector: "", setFavorite: false)
  80. }
  81. }
  82. }
  83. }
  84. @objc func downloadedFile(_ notification: NSNotification) {
  85. if self.view?.window == nil { return }
  86. if let userInfo = notification.userInfo as NSDictionary? {
  87. if let ocId = userInfo["ocId"] as? String, let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId), let errorCode = userInfo["errorCode"] as? Int {
  88. if errorCode == 0 && metadata.ocId == self.metadata?.ocId {
  89. if metadata.typeFile == NCBrandGlobal.shared.metadataTypeFileImage {
  90. viewImage(metadata: metadata)
  91. } else if metadata.typeFile == NCBrandGlobal.shared.metadataTypeFileVideo {
  92. viewVideo(metadata: metadata)
  93. }
  94. }
  95. if errorCode == 0 && metadata.ocId == self.metadataLivePhoto?.ocId {
  96. viewVideo(metadata: metadata)
  97. }
  98. }
  99. }
  100. }
  101. private func viewImage(metadata: tableMetadata) {
  102. var image: UIImage?
  103. let ext = CCUtility.getExtension(metadata.fileNameView)
  104. let filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  105. if ext == "GIF" {
  106. image = UIImage.animatedImage(withAnimatedGIFURL: URL(fileURLWithPath: filePath))
  107. } else {
  108. image = UIImage.init(contentsOfFile: filePath)
  109. }
  110. imageView.image = image
  111. imageView.frame = CGRect(x: 0, y: 0, width: image?.size.width ?? 0, height: image?.size.height ?? 0)
  112. }
  113. private func viewVideo(metadata: tableMetadata) {
  114. let filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  115. if let resolutionVideo = resolutionForLocalVideo(url: URL(fileURLWithPath: filePath)) {
  116. let originRatio = resolutionVideo.width / resolutionVideo.height
  117. let newRatio = UIScreen.main.bounds.width / UIScreen.main.bounds.height
  118. var newSize = resolutionVideo
  119. if originRatio < newRatio {
  120. newSize.height = UIScreen.main.bounds.height
  121. newSize.width = UIScreen.main.bounds.height * originRatio
  122. } else {
  123. newSize.width = UIScreen.main.bounds.width
  124. newSize.height = UIScreen.main.bounds.width / originRatio
  125. }
  126. let player = AVPlayer(url: URL(fileURLWithPath: filePath))
  127. self.videoLayer = AVPlayerLayer(player: player)
  128. if let videoLayer = self.videoLayer {
  129. videoLayer.videoGravity = .resizeAspectFill
  130. imageView.image = nil
  131. imageView.frame = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
  132. imageView.layer.addSublayer(videoLayer)
  133. }
  134. player.isMuted = true
  135. player.play()
  136. }
  137. }
  138. private func resolutionForLocalVideo(url: URL) -> CGSize? {
  139. guard let track = AVURLAsset(url: url).tracks(withMediaType: AVMediaType.video).first else { return nil }
  140. let size = track.naturalSize.applying(track.preferredTransform)
  141. return CGSize(width: abs(size.width), height: abs(size.height))
  142. }
  143. required init?(coder: NSCoder) {
  144. fatalError("init(coder:) has not been implemented")
  145. }
  146. override func viewDidLoad() {
  147. super.viewDidLoad()
  148. }
  149. }