NCViewerDocumentWeb.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. CCUtility.copyFile(atPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView), toPath: fileNamePath)
  42. let url = URL.init(fileURLWithPath: fileNamePath)
  43. let preferences = WKPreferences()
  44. let configuration = WKWebViewConfiguration()
  45. preferences.javaScriptEnabled = true
  46. configuration.preferences = preferences
  47. 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)
  48. webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  49. webView.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  50. webView.isOpaque = false
  51. if fileNameExtension == "CSS" || fileNameExtension == "PY" || fileNameExtension == "XML" || fileNameExtension == "JS" {
  52. do {
  53. let dataFile = try String(contentsOf: url, encoding: String.Encoding(rawValue: String.Encoding.ascii.rawValue))
  54. if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.phone {
  55. webView.loadHTMLString("<div style='font-size:40;font-family:Sans-Serif;'><pre>" + dataFile, baseURL: nil)
  56. } else {
  57. webView.loadHTMLString("<div style='font-size:20;font-family:Sans-Serif;'><pre>" + dataFile, baseURL: nil)
  58. }
  59. } catch {
  60. print("error")
  61. }
  62. } else if CCUtility.isDocumentModifiableExtension(fileNameExtension) {
  63. let session = URLSession(configuration: URLSessionConfiguration.default)
  64. var request = URLRequest(url: url)
  65. request.httpMethod = "HEAD"
  66. let task = session.dataTask(with: request) { (data, response, error) in
  67. guard let data = data else {
  68. return
  69. }
  70. guard let response = response else {
  71. return
  72. }
  73. DispatchQueue.main.async {
  74. guard let encodingName = NCUchardet.sharedNUCharDet()?.encodingStringDetect(with: data) else {
  75. return
  76. }
  77. webView.load(data, mimeType: response.mimeType!, characterEncodingName: encodingName, baseURL: url)
  78. }
  79. }
  80. task.resume()
  81. } else {
  82. webView.load(URLRequest(url: url))
  83. }
  84. detail.view.addSubview(webView)
  85. }
  86. }