NCViewerDocumentWeb.swift 4.4 KB

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