NCViewerQuickLook.swift 3.5 KB

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