NCViewerDocumentWeb.swift 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. @objc static let sharedInstance: NCViewerDocumentWeb = {
  28. let instance = NCViewerDocumentWeb()
  29. return instance
  30. }()
  31. @objc func viewDocumentWebAt(_ metadata: tableMetadata, detail: CCDetail) {
  32. if !CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  33. detail.navigationController?.popViewController(animated: true)
  34. return
  35. }
  36. if #available(iOS 11.0, *) {
  37. safeAreaBottom = Int((UIApplication.shared.keyWindow?.safeAreaInsets.bottom)!)
  38. }
  39. let fileNamePath = NSTemporaryDirectory() + metadata.fileNameView
  40. let fileNameExtension = (metadata.fileNameView as NSString).pathExtension.uppercased()
  41. do {
  42. try FileManager.default.removeItem(atPath:fileNamePath)
  43. } catch { }
  44. do {
  45. try FileManager.default.linkItem(atPath: CCUtility.getDirectoryProviderStorageocId(metadata.ocId, fileNameView: metadata.fileNameView), toPath: fileNamePath)
  46. } catch {
  47. print("error")
  48. return
  49. }
  50. let url = URL.init(fileURLWithPath: fileNamePath)
  51. let preferences = WKPreferences()
  52. let configuration = WKWebViewConfiguration()
  53. preferences.javaScriptEnabled = true
  54. configuration.preferences = preferences
  55. 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)
  56. webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  57. webView.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  58. webView.isOpaque = false
  59. if fileNameExtension == "CSS" || fileNameExtension == "PY" || fileNameExtension == "XML" || fileNameExtension == "JS" {
  60. do {
  61. let dataFile = try String(contentsOf: url, encoding: String.Encoding(rawValue: String.Encoding.ascii.rawValue))
  62. if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.phone {
  63. webView.loadHTMLString("<div style='font-size:40;font-family:Sans-Serif;'><pre>" + dataFile, baseURL: nil)
  64. } else {
  65. webView.loadHTMLString("<div style='font-size:20;font-family:Sans-Serif;'><pre>" + dataFile, baseURL: nil)
  66. }
  67. } catch {
  68. print("error")
  69. }
  70. } else if CCUtility.isDocumentModifiableExtension(fileNameExtension) {
  71. let session = URLSession(configuration: URLSessionConfiguration.default)
  72. var request = URLRequest(url: url)
  73. request.httpMethod = "HEAD"
  74. let task = session.dataTask(with: request) { (data, response, error) in
  75. guard let data = data else {
  76. return
  77. }
  78. guard let response = response else {
  79. return
  80. }
  81. DispatchQueue.main.async {
  82. guard let encodingName = NCUchardet.sharedNUCharDet()?.encodingStringDetect(with: data) else {
  83. return
  84. }
  85. webView.load(data, mimeType: response.mimeType!, characterEncodingName: encodingName, baseURL: url)
  86. }
  87. }
  88. task.resume()
  89. } else {
  90. webView.load(URLRequest(url: url))
  91. }
  92. detail.view.addSubview(webView)
  93. }
  94. }