NCViewerProviderContextMenu.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. override func loadView() {
  29. view = imageView
  30. }
  31. override func viewDidLayoutSubviews() {
  32. super.viewDidLayoutSubviews()
  33. if let videoLayer = self.videoLayer {
  34. videoLayer.frame = imageView.layer.bounds
  35. }
  36. }
  37. init(metadata: tableMetadata) {
  38. super.init(nibName: nil, bundle: nil)
  39. var metadata = metadata
  40. var image: UIImage?
  41. let ext = CCUtility.getExtension(metadata.fileNameView)
  42. var filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  43. imageView.contentMode = .scaleAspectFill
  44. if metadata.directory {
  45. image = UIImage(named: "folder")!.image(color: NCBrandColor.shared.brandElement, size: UIScreen.main.bounds.width / 2)
  46. imageView.image = image
  47. imageView.frame = CGRect(x: 0, y: 0, width: image?.size.width ?? 0, height: image?.size.height ?? 0)
  48. } else {
  49. // ICON - IMAGE
  50. image = UIImage.init(named: metadata.iconName)?.resizeImage(size: CGSize(width: UIScreen.main.bounds.width / 2, height: UIScreen.main.bounds.height / 2), isAspectRation: true)
  51. // PREVIEW - IMAGE
  52. if CCUtility.fileProviderStoragePreviewIconExists(metadata.ocId, etag: metadata.etag) {
  53. image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStoragePreviewOcId(metadata.ocId, etag: metadata.etag))
  54. }
  55. // IMAGE - IMAGE
  56. if metadata.typeFile == NCBrandGlobal.shared.metadataTypeFileImage && CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  57. if ext == "GIF" {
  58. image = UIImage.animatedImage(withAnimatedGIFURL: URL(fileURLWithPath: filePath))
  59. } else {
  60. image = UIImage.init(contentsOfFile: filePath)
  61. }
  62. }
  63. imageView.image = image
  64. imageView.frame = CGRect(x: 0, y: 0, width: image?.size.width ?? 0, height: image?.size.height ?? 0)
  65. // LIVE PHOTO
  66. let fileName = (metadata.fileNameView as NSString).deletingPathExtension + ".mov"
  67. if let metadataLivePhoto = NCManageDatabase.shared.getMetadata(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileNameView LIKE[c] %@", metadata.account, metadata.serverUrl, fileName)) {
  68. if CCUtility.fileProviderStorageExists(metadataLivePhoto.ocId, fileNameView: metadataLivePhoto.fileNameView) {
  69. metadata = metadataLivePhoto
  70. filePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  71. }
  72. }
  73. // VIDEO
  74. if (metadata.typeFile == NCBrandGlobal.shared.metadataTypeFileVideo && CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView)) {
  75. if let resolutionVideo = resolutionForLocalVideo(url: URL(fileURLWithPath: filePath)) {
  76. let originRatio = resolutionVideo.width / resolutionVideo.height
  77. let newRatio = UIScreen.main.bounds.width / UIScreen.main.bounds.height
  78. var newSize = resolutionVideo
  79. if originRatio < newRatio {
  80. newSize.height = UIScreen.main.bounds.height
  81. newSize.width = UIScreen.main.bounds.height * originRatio
  82. } else {
  83. newSize.width = UIScreen.main.bounds.width
  84. newSize.height = UIScreen.main.bounds.width / originRatio
  85. }
  86. let player = AVPlayer(url: URL(fileURLWithPath: filePath))
  87. self.videoLayer = AVPlayerLayer(player: player)
  88. if let videoLayer = self.videoLayer {
  89. videoLayer.videoGravity = .resizeAspectFill
  90. imageView.image = nil
  91. imageView.frame = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height)
  92. imageView.layer.addSublayer(videoLayer)
  93. }
  94. player.isMuted = true
  95. player.play()
  96. }
  97. }
  98. }
  99. preferredContentSize = imageView.frame.size
  100. }
  101. private func resolutionForLocalVideo(url: URL) -> CGSize? {
  102. guard let track = AVURLAsset(url: url).tracks(withMediaType: AVMediaType.video).first else { return nil }
  103. let size = track.naturalSize.applying(track.preferredTransform)
  104. return CGSize(width: abs(size.width), height: abs(size.height))
  105. }
  106. required init?(coder: NSCoder) {
  107. fatalError("init(coder:) has not been implemented")
  108. }
  109. override func viewDidLoad() {
  110. super.viewDidLoad()
  111. }
  112. }