NCViewerDocumentWeb.swift 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 <m.faggiana@twsweb.it>
  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. @objc static let sharedInstance: NCViewerDocumentWeb = {
  26. let instance = NCViewerDocumentWeb()
  27. return instance
  28. }()
  29. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  30. var safeAreaBottom: Int = 0
  31. @objc func isRichDocument( _ metadata: tableMetadata) -> Bool {
  32. var isRichDocument = false
  33. guard let mimeType = CCUtility.getMimeType(metadata.fileNameView) else {
  34. return isRichDocument
  35. }
  36. guard let richdocumentsMimetypes = NCManageDatabase.sharedInstance.getRichdocumentsMimetypes() else {
  37. return isRichDocument
  38. }
  39. if richdocumentsMimetypes.count > 0 && mimeType.components(separatedBy: ".").count > 2 {
  40. let mimeTypeArray = mimeType.components(separatedBy: ".")
  41. let mimeType = mimeTypeArray[mimeTypeArray.count - 2] + "." + mimeTypeArray[mimeTypeArray.count - 1]
  42. for richdocumentMimetype: String in richdocumentsMimetypes {
  43. if richdocumentMimetype.contains(mimeType) {
  44. isRichDocument = true
  45. }
  46. }
  47. }
  48. return isRichDocument
  49. }
  50. @objc func viewDocumentWebAt(_ metadata: tableMetadata, detail: CCDetail) {
  51. if !CCUtility.fileProviderStorageExists(metadata.fileID, fileNameView: metadata.fileNameView) {
  52. detail.navigationController?.popViewController(animated: true)
  53. return
  54. }
  55. guard let rootView = UIApplication.shared.keyWindow else {
  56. return
  57. }
  58. if #available(iOS 11.0, *) {
  59. safeAreaBottom = Int(rootView.safeAreaInsets.bottom)
  60. }
  61. let fileNamePath = NSTemporaryDirectory() + metadata.fileNameView
  62. let fileNameExtension = (metadata.fileNameView as NSString).pathExtension.uppercased()
  63. do {
  64. try FileManager.default.removeItem(atPath:fileNamePath)
  65. } catch { }
  66. do {
  67. try FileManager.default.linkItem(atPath: CCUtility.getDirectoryProviderStorageFileID(metadata.fileID, fileNameView: metadata.fileNameView), toPath: fileNamePath)
  68. } catch {
  69. print("error")
  70. return
  71. }
  72. let url = URL.init(fileURLWithPath: fileNamePath)
  73. let preferences = WKPreferences()
  74. let configuration = WKWebViewConfiguration()
  75. preferences.javaScriptEnabled = true
  76. configuration.preferences = preferences
  77. let webView = WKWebView(frame: CGRect(x: 0, y: 0, width: Int(rootView.bounds.size.width), height: Int(rootView.bounds.size.height) - Int(k_detail_Toolbar_Height) - safeAreaBottom - 1), configuration: configuration)
  78. webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  79. webView.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  80. webView.isOpaque = false
  81. if fileNameExtension == "CSS" || fileNameExtension == "PY" || fileNameExtension == "XML" || fileNameExtension == "JS" {
  82. do {
  83. let dataFile = try String(contentsOf: url, encoding: String.Encoding(rawValue: String.Encoding.ascii.rawValue))
  84. if UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiom.phone {
  85. webView.loadHTMLString("<div style='font-size:40;font-family:Sans-Serif;'><pre>" + dataFile, baseURL: nil)
  86. } else {
  87. webView.loadHTMLString("<div style='font-size:20;font-family:Sans-Serif;'><pre>" + dataFile, baseURL: nil)
  88. }
  89. } catch {
  90. print("error")
  91. }
  92. } else if CCUtility.isDocumentModifiableExtension(fileNameExtension) {
  93. let session = URLSession(configuration: URLSessionConfiguration.default)
  94. var request = URLRequest(url: url)
  95. request.httpMethod = "HEAD"
  96. let task = session.dataTask(with: request) { (data, response, error) in
  97. guard let data = data else {
  98. return
  99. }
  100. guard let response = response else {
  101. return
  102. }
  103. DispatchQueue.main.async {
  104. guard let encodingName = NCUchardet.sharedNUCharDet()?.encodingStringDetect(with: data) else {
  105. return
  106. }
  107. webView.load(data, mimeType: response.mimeType!, characterEncodingName: encodingName, baseURL: url)
  108. }
  109. }
  110. task.resume()
  111. } else {
  112. webView.load(URLRequest(url: url))
  113. }
  114. detail.view.addSubview(webView)
  115. }
  116. }