NCViewerProviderContextMenu.swift 5.8 KB

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