NCViewerDocumentWeb.swift 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. class NCViewerDocumentWeb: NSObject {
  25. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  26. var safeAreaBottom: Int = 0
  27. var mimeType: String?
  28. @objc static let sharedInstance: NCViewerDocumentWeb = {
  29. let instance = NCViewerDocumentWeb()
  30. return instance
  31. }()
  32. @objc func viewDocumentWebAt(_ metadata: tableMetadata, detail: CCDetail) {
  33. if !CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  34. detail.navigationController?.popViewController(animated: true)
  35. return
  36. }
  37. if #available(iOS 11.0, *) {
  38. safeAreaBottom = Int((UIApplication.shared.keyWindow?.safeAreaInsets.bottom)!)
  39. }
  40. let fileNamePath = NSTemporaryDirectory() + metadata.fileNameView
  41. let fileNameExtension = (metadata.fileNameView as NSString).pathExtension.uppercased()
  42. CCUtility.copyFile(atPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView), toPath: fileNamePath)
  43. let url = URL.init(fileURLWithPath: fileNamePath)
  44. let preferences = WKPreferences()
  45. let configuration = WKWebViewConfiguration()
  46. preferences.javaScriptEnabled = true
  47. configuration.preferences = preferences
  48. let webView = WKWebView(frame: CGRect(x: 0, y: 0, width: Int(detail.view.bounds.size.width), height: Int(detail.view.bounds.size.height) - Int(k_detail_Toolbar_Height) - safeAreaBottom - 1), configuration: configuration)
  49. webView.navigationDelegate = self
  50. webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  51. webView.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  52. webView.isOpaque = false
  53. if fileNameExtension == "CSS" || fileNameExtension == "PY" || fileNameExtension == "XML" || fileNameExtension == "JS" {
  54. do {
  55. let dataFile = try String(contentsOf: url, encoding: String.Encoding(rawValue: String.Encoding.ascii.rawValue))
  56. if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.phone {
  57. webView.loadHTMLString("<div style='font-size:40;font-family:Sans-Serif;'><pre>" + dataFile, baseURL: nil)
  58. } else {
  59. webView.loadHTMLString("<div style='font-size:20;font-family:Sans-Serif;'><pre>" + dataFile, baseURL: nil)
  60. }
  61. } catch {
  62. print("error")
  63. }
  64. } else if CCUtility.isDocumentModifiableExtension(fileNameExtension) {
  65. let session = URLSession(configuration: URLSessionConfiguration.default)
  66. var request = URLRequest(url: url)
  67. request.httpMethod = "HEAD"
  68. let task = session.dataTask(with: request) { (data, response, error) in
  69. guard let data = data else {
  70. return
  71. }
  72. guard let response = response else {
  73. return
  74. }
  75. DispatchQueue.main.async {
  76. guard let encodingName = NCUchardet.sharedNUCharDet()?.encodingStringDetect(with: data) else {
  77. return
  78. }
  79. self.mimeType = response.mimeType
  80. webView.load(data, mimeType: response.mimeType!, characterEncodingName: encodingName, baseURL: url)
  81. }
  82. }
  83. task.resume()
  84. } else {
  85. webView.load(URLRequest(url: url))
  86. }
  87. detail.view.addSubview(webView)
  88. }
  89. }
  90. extension NCViewerDocumentWeb: WKNavigationDelegate {
  91. public func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
  92. if let serverTrust = challenge.protectionSpace.serverTrust {
  93. completionHandler(Foundation.URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: serverTrust))
  94. } else {
  95. completionHandler(URLSession.AuthChallengeDisposition.useCredential, nil);
  96. }
  97. }
  98. public func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
  99. decisionHandler(.allow)
  100. }
  101. public func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
  102. print("didStartProvisionalNavigation");
  103. }
  104. public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  105. if self.mimeType != nil && self.mimeType == "text/plain" && CCUtility.getDarkMode() {
  106. let js = "document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'white';DOMReady();"
  107. webView.evaluateJavaScript(js) { (_, _) in }
  108. }
  109. }
  110. public func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
  111. print("didReceiveServerRedirectForProvisionalNavigation");
  112. }
  113. }