NCViewerQuickLook.swift 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. //
  2. // NCViewerQuickLook.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 03/05/2020.
  6. // Copyright © 2020 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 QuickLook
  25. @objc class NCViewerQuickLook: NSObject, QLPreviewControllerDelegate, QLPreviewControllerDataSource {
  26. let previewController = QLPreviewController()
  27. var previewItems: [PreviewItem] = []
  28. var viewController: UIViewController?
  29. @objc func quickLook(url: URL, viewController: UIViewController) {
  30. self.viewController = viewController
  31. URLSession.shared.dataTask(with: url) { data, response, error in
  32. guard let _ = data, error == nil else {
  33. self.presentAlertController(with: error?.localizedDescription ?? "Failed to look the file")
  34. return
  35. }
  36. //let httpURLResponse = response as? HTTPURLResponse
  37. //let mimeType = httpURLResponse?.mimeType
  38. var previewURL = url
  39. previewURL.hasHiddenExtension = true
  40. let previewItem = PreviewItem()
  41. previewItem.previewItemURL = previewURL
  42. self.previewItems.append(previewItem)
  43. DispatchQueue.main.async {
  44. UIApplication.shared.isNetworkActivityIndicatorVisible = false
  45. self.previewController.delegate = self
  46. self.previewController.dataSource = self
  47. self.previewController.currentPreviewItemIndex = 0
  48. self.viewController?.present(self.previewController, animated: true)
  49. }
  50. }.resume()
  51. UIApplication.shared.isNetworkActivityIndicatorVisible = true
  52. }
  53. func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem { previewItems[index] }
  54. func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
  55. previewItems.count
  56. }
  57. func presentAlertController(with message: String) {
  58. // present your alert controller from the main thread
  59. DispatchQueue.main.async {
  60. UIApplication.shared.isNetworkActivityIndicatorVisible = false
  61. let alert = UIAlertController(title: "Alert", message: message, preferredStyle: .alert)
  62. alert.addAction(.init(title: "OK", style: .default))
  63. self.viewController?.present(alert, animated: true)
  64. }
  65. }
  66. }
  67. extension URL {
  68. var hasHiddenExtension: Bool {
  69. get { (try? resourceValues(forKeys: [.hasHiddenExtensionKey]))?.hasHiddenExtension == true }
  70. set {
  71. var resourceValues = URLResourceValues()
  72. resourceValues.hasHiddenExtension = newValue
  73. try? setResourceValues(resourceValues)
  74. }
  75. }
  76. }
  77. import QuickLook
  78. class PreviewItem: NSObject, QLPreviewItem {
  79. var previewItemURL: URL?
  80. }