NCViewerDocumentWeb.swift 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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, detail: CCDetail, width: Int, height: Int) {
  31. if !CCUtility.fileProviderStorageExists(metadata.fileID, fileNameView: metadata.fileNameView) {
  32. detail.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. detail.view.addSubview(webView)
  89. }
  90. func createToolbar(detail: CCDetail) {
  91. var safeAreaBottom: CGFloat = 0
  92. guard let rootView = UIApplication.shared.keyWindow else {
  93. return
  94. }
  95. if #available(iOS 11.0, *) {
  96. safeAreaBottom = rootView.safeAreaInsets.bottom
  97. }
  98. guard let detailView = detail.view else {
  99. return
  100. }
  101. let toolbar = UITabBar(frame: CGRect(x: 0 as CGFloat, y: detailView.bounds.size.height - CGFloat(k_detail_Toolbar_Height) - safeAreaBottom, width: detailView.bounds.size.width, height: CGFloat(k_detail_Toolbar_Height)))
  102. let flexible = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: detail, action: nil)
  103. let fixedSpaceMini = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonItem.SystemItem.flexibleSpace, target: detail, action: nil)
  104. fixedSpaceMini.width = 25
  105. }
  106. /*
  107. - (void)createToolbar
  108. {
  109. CGFloat safeAreaBottom = 0;
  110. NSString *serverUrl = [[NCManageDatabase sharedInstance] getServerUrl:_metadataDetail.directoryID];
  111. if (!serverUrl)
  112. return;
  113. if (@available(iOS 11, *)) {
  114. safeAreaBottom = [UIApplication sharedApplication].delegate.window.safeAreaInsets.bottom;
  115. }
  116. self.toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.bounds.size.height - TOOLBAR_HEIGHT - safeAreaBottom, self.view.bounds.size.width, TOOLBAR_HEIGHT)];
  117. UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
  118. UIBarButtonItem *fixedSpaceMini = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:self action:nil];
  119. fixedSpaceMini.width = 25;
  120. buttonModifyTxt = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"actionSheetModify"] style:UIBarButtonItemStylePlain target:self action:@selector(modifyTxtButtonPressed:)];
  121. if (![NCBrandOptions sharedInstance].disable_openin_file) {
  122. self.buttonAction = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"openFile"] style:UIBarButtonItemStylePlain target:self action:@selector(actionButtonPressed:)];
  123. }
  124. buttonShare = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"share"] style:UIBarButtonItemStylePlain target:self action:@selector(shareButtonPressed:)];
  125. buttonDelete = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(deleteButtonPressed:)];
  126. if ([CCUtility isDocumentModifiableExtension:fileNameExtension]) {
  127. if ([CCUtility isFolderEncrypted:serverUrl account:appDelegate.activeAccount]) // E2EE
  128. [self.toolbar setItems:[NSArray arrayWithObjects: buttonModifyTxt, flexible, buttonDelete, fixedSpaceMini, self.buttonAction, nil]];
  129. else
  130. [self.toolbar setItems:[NSArray arrayWithObjects: buttonModifyTxt, flexible, buttonDelete, fixedSpaceMini, buttonShare, fixedSpaceMini, self.buttonAction, nil]];
  131. } else {
  132. if ([CCUtility isFolderEncrypted:serverUrl account:appDelegate.activeAccount]) // E2EE
  133. [self.toolbar setItems:[NSArray arrayWithObjects: flexible, buttonDelete, fixedSpaceMini, self.buttonAction, nil]];
  134. else
  135. [self.toolbar setItems:[NSArray arrayWithObjects: flexible, buttonDelete, fixedSpaceMini, buttonShare, fixedSpaceMini, self.buttonAction, nil]];
  136. }
  137. [self.toolbar setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin];
  138. self.toolbar.barTintColor = [NCBrandColor sharedInstance].tabBar;
  139. self.toolbar.tintColor = [NCBrandColor sharedInstance].brandElement;
  140. [self.view addSubview:self.toolbar];
  141. }
  142. */
  143. }