NCViewer.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. //
  2. // NCViewer.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 16/10/2020.
  6. // Copyright © 2020 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. import NCCommunication
  25. class NCViewer: NSObject {
  26. @objc static let shared: NCViewer = {
  27. let instance = NCViewer()
  28. return instance
  29. }()
  30. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  31. private var viewerQuickLook: NCViewerQuickLook?
  32. func view(viewController: UIViewController, metadata: tableMetadata) {
  33. // VIDEO AUDIO
  34. if metadata.typeFile == k_metadataTypeFile_audio || metadata.typeFile == k_metadataTypeFile_video {
  35. if let navigationController = getPushNavigationController(viewController: viewController, serverUrl: metadata.serverUrl) {
  36. let viewController:NCViewerVideo = UIStoryboard(name: "NCViewerVideo", bundle: nil).instantiateInitialViewController() as! NCViewerVideo
  37. viewController.metadata = metadata
  38. navigationController.pushViewController(viewController, animated: true)
  39. }
  40. return
  41. }
  42. // DOCUMENTS
  43. if metadata.typeFile == k_metadataTypeFile_document {
  44. // PDF
  45. if metadata.contentType == "application/pdf" {
  46. if let navigationController = getPushNavigationController(viewController: viewController, serverUrl: metadata.serverUrl) {
  47. let viewController:NCViewerPDF = UIStoryboard(name: "NCViewerPDF", bundle: nil).instantiateInitialViewController() as! NCViewerPDF
  48. viewController.metadata = metadata
  49. navigationController.pushViewController(viewController, animated: true)
  50. }
  51. return
  52. }
  53. // DirectEditinf: Nextcloud Text - OnlyOffice
  54. if NCUtility.shared.isDirectEditing(account: metadata.account, contentType: metadata.contentType) != nil && NCCommunication.shared.isNetworkReachable() {
  55. guard let editor = NCUtility.shared.isDirectEditing(account: metadata.account, contentType: metadata.contentType) else { return }
  56. if editor == k_editor_text || editor == k_editor_onlyoffice {
  57. if metadata.url == "" {
  58. var customUserAgent: String?
  59. let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, urlBase: metadata.urlBase, account: metadata.account)!
  60. if editor == k_editor_onlyoffice {
  61. customUserAgent = NCUtility.shared.getCustomUserAgentOnlyOffice()
  62. }
  63. NCCommunication.shared.NCTextOpenFile(fileNamePath: fileNamePath, editor: editor, customUserAgent: customUserAgent) { (account, url, errorCode, errorMessage) in
  64. if errorCode == 0 && account == self.appDelegate.account && url != nil {
  65. if let navigationController = self.getPushNavigationController(viewController: viewController, serverUrl: metadata.serverUrl) {
  66. let viewController:NCViewerNextcloudText = UIStoryboard(name: "NCViewerNextcloudText", bundle: nil).instantiateInitialViewController() as! NCViewerNextcloudText
  67. viewController.metadata = metadata
  68. viewController.editor = editor
  69. viewController.link = url!
  70. navigationController.pushViewController(viewController, animated: true)
  71. }
  72. } else if errorCode != 0 {
  73. NCContentPresenter.shared.messageNotification("_error_", description: errorMessage, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  74. }
  75. }
  76. } else {
  77. if editor == k_editor_onlyoffice {
  78. //self.navigationController?.navigationBar.topItem?.title = ""
  79. }
  80. if let navigationController = self.getPushNavigationController(viewController: viewController, serverUrl: metadata.serverUrl) {
  81. let viewController:NCViewerNextcloudText = UIStoryboard(name: "NCViewerNextcloudText", bundle: nil).instantiateInitialViewController() as! NCViewerNextcloudText
  82. viewController.metadata = metadata
  83. viewController.editor = editor
  84. viewController.link = metadata.url
  85. navigationController.pushViewController(viewController, animated: true)
  86. }
  87. }
  88. } else {
  89. NCContentPresenter.shared.messageNotification("_error_", description: "_editor_unknown_", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: Int(k_CCErrorInternalError))
  90. }
  91. return
  92. }
  93. }
  94. // OTHER
  95. let fileNamePath = NSTemporaryDirectory() + metadata.fileNameView
  96. CCUtility.copyFile(atPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView), toPath: fileNamePath)
  97. viewerQuickLook = NCViewerQuickLook.init()
  98. viewerQuickLook?.quickLook(url: URL(fileURLWithPath: fileNamePath))
  99. }
  100. private func getPushNavigationController(viewController: UIViewController, serverUrl: String) -> UINavigationController? {
  101. if viewController is NCFiles || viewController is NCFavorite || viewController is NCOffline || viewController is NCRecent || viewController is NCFileViewInFolder {
  102. if serverUrl == appDelegate.activeServerUrl {
  103. return viewController.navigationController
  104. }
  105. }
  106. return nil
  107. }
  108. }
  109. //MARK: -
  110. extension NCViewer: NCSelectDelegate {
  111. func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String, items: [Any], buttonType: String, overwrite: Bool) {
  112. if let metadata = metadata, let serverUrl = serverUrl {
  113. if buttonType == "done" {
  114. NCNetworking.shared.moveMetadata(metadata, serverUrlTo: serverUrl, overwrite: overwrite) { (errorCode, errorDescription) in
  115. if errorCode != 0 {
  116. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  117. }
  118. }
  119. } else {
  120. NCNetworking.shared.copyMetadata(metadata, serverUrlTo: serverUrl, overwrite: overwrite) { (errorCode, errorDescription) in
  121. if errorCode != 0 {
  122. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  123. }
  124. }
  125. }
  126. }
  127. }
  128. }