NCViewerDocumentWeb.swift 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // NCViewerDocumentWeb.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 28/09/2018.
  6. // Copyright © 2018 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 WebKit
  25. class NCViewerDocumentWeb: WKWebView {
  26. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  27. var safeAreaBottom: Int = 0
  28. var mimeType: String?
  29. override init(frame: CGRect, configuration: WKWebViewConfiguration) {
  30. super.init(frame: frame, configuration: configuration)
  31. autoresizingMask = [.flexibleWidth, .flexibleHeight]
  32. navigationDelegate = self
  33. backgroundColor = .white
  34. isOpaque = false
  35. }
  36. required init?(coder: NSCoder) {
  37. super.init(coder: coder)
  38. }
  39. @objc func viewDocumentWebAt(_ metadata: tableMetadata, view: UIView) {
  40. if !CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) { return }
  41. if #available(iOS 11.0, *) {
  42. safeAreaBottom = Int((UIApplication.shared.keyWindow?.safeAreaInsets.bottom)!)
  43. }
  44. let fileNamePath = NSTemporaryDirectory() + metadata.fileNameView
  45. let fileNameExtension = (metadata.fileNameView as NSString).pathExtension.uppercased()
  46. CCUtility.copyFile(atPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView), toPath: fileNamePath)
  47. let url = URL.init(fileURLWithPath: fileNamePath)
  48. let preferences = WKPreferences()
  49. preferences.javaScriptEnabled = false
  50. // Detect file xls, xlss for enable javascript
  51. if fileNameExtension == "XLS" || fileNameExtension == "XLSX" {
  52. if let fileHandle = FileHandle(forReadingAtPath: fileNamePath) {
  53. let data = fileHandle.readData(ofLength: 4)
  54. if data.starts(with: [0x50, 0x4b, 0x03, 0x04]) { // "PK\003\004"
  55. preferences.javaScriptEnabled = true
  56. }
  57. fileHandle.closeFile()
  58. }
  59. }
  60. configuration.preferences = preferences
  61. if fileNameExtension == "CSS" || fileNameExtension == "PY" || fileNameExtension == "XML" || fileNameExtension == "JS" {
  62. do {
  63. let dataFile = try String(contentsOf: url, encoding: String.Encoding(rawValue: String.Encoding.ascii.rawValue))
  64. if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.phone {
  65. loadHTMLString("<div style='font-size:40;font-family:Sans-Serif;'><pre>" + dataFile, baseURL: nil)
  66. } else {
  67. loadHTMLString("<div style='font-size:20;font-family:Sans-Serif;'><pre>" + dataFile, baseURL: nil)
  68. }
  69. } catch {
  70. print("error")
  71. }
  72. } else {
  73. load(URLRequest(url: url))
  74. }
  75. view.addSubview(self)
  76. }
  77. }
  78. extension NCViewerDocumentWeb: WKNavigationDelegate {
  79. public func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
  80. if let serverTrust = challenge.protectionSpace.serverTrust {
  81. completionHandler(Foundation.URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: serverTrust))
  82. } else {
  83. completionHandler(URLSession.AuthChallengeDisposition.useCredential, nil);
  84. }
  85. }
  86. public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
  87. decisionHandler(.allow)
  88. }
  89. public func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
  90. print("didStartProvisionalNavigation");
  91. }
  92. public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  93. if self.mimeType != nil && self.mimeType == "text/plain" && CCUtility.getDarkMode() {
  94. let js = "document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'white';DOMReady();"
  95. webView.evaluateJavaScript(js) { (_, _) in }
  96. }
  97. }
  98. public func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
  99. print("didReceiveServerRedirectForProvisionalNavigation");
  100. }
  101. }