NCViewer.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. private var metadata = tableMetadata()
  33. func view(viewController: UIViewController, metadata: tableMetadata) {
  34. self.metadata = metadata
  35. // VIDEO AUDIO
  36. if metadata.typeFile == k_metadataTypeFile_audio || metadata.typeFile == k_metadataTypeFile_video {
  37. if let navigationController = getPushNavigationController(viewController: viewController, serverUrl: metadata.serverUrl) {
  38. let viewController:NCViewerVideo = UIStoryboard(name: "NCViewerVideo", bundle: nil).instantiateInitialViewController() as! NCViewerVideo
  39. viewController.metadata = metadata
  40. navigationController.pushViewController(viewController, animated: true)
  41. }
  42. return
  43. }
  44. // DOCUMENTS
  45. if metadata.typeFile == k_metadataTypeFile_document {
  46. // PDF
  47. if metadata.contentType == "application/pdf" {
  48. if let navigationController = getPushNavigationController(viewController: viewController, serverUrl: metadata.serverUrl) {
  49. let viewController:NCViewerPDF = UIStoryboard(name: "NCViewerPDF", bundle: nil).instantiateInitialViewController() as! NCViewerPDF
  50. viewController.metadata = metadata
  51. navigationController.pushViewController(viewController, animated: true)
  52. }
  53. return
  54. }
  55. // DirectEditinf: Nextcloud Text - OnlyOffice
  56. if NCUtility.shared.isDirectEditing(account: metadata.account, contentType: metadata.contentType) != nil && NCCommunication.shared.isNetworkReachable() {
  57. guard let editor = NCUtility.shared.isDirectEditing(account: metadata.account, contentType: metadata.contentType) else { return }
  58. if editor == k_editor_text || editor == k_editor_onlyoffice {
  59. if metadata.url == "" {
  60. var customUserAgent: String?
  61. let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, urlBase: metadata.urlBase, account: metadata.account)!
  62. if editor == k_editor_onlyoffice {
  63. customUserAgent = NCUtility.shared.getCustomUserAgentOnlyOffice()
  64. }
  65. NCCommunication.shared.NCTextOpenFile(fileNamePath: fileNamePath, editor: editor, customUserAgent: customUserAgent) { (account, url, errorCode, errorMessage) in
  66. if errorCode == 0 && account == self.appDelegate.account && url != nil {
  67. if let navigationController = self.getPushNavigationController(viewController: viewController, serverUrl: metadata.serverUrl) {
  68. let viewController:NCViewerNextcloudText = UIStoryboard(name: "NCViewerNextcloudText", bundle: nil).instantiateInitialViewController() as! NCViewerNextcloudText
  69. viewController.metadata = metadata
  70. viewController.editor = editor
  71. viewController.link = url!
  72. navigationController.pushViewController(viewController, animated: true)
  73. }
  74. } else if errorCode != 0 {
  75. NCContentPresenter.shared.messageNotification("_error_", description: errorMessage, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  76. }
  77. }
  78. } else {
  79. if editor == k_editor_onlyoffice {
  80. //self.navigationController?.navigationBar.topItem?.title = ""
  81. }
  82. if let navigationController = self.getPushNavigationController(viewController: viewController, serverUrl: metadata.serverUrl) {
  83. let viewController:NCViewerNextcloudText = UIStoryboard(name: "NCViewerNextcloudText", bundle: nil).instantiateInitialViewController() as! NCViewerNextcloudText
  84. viewController.metadata = metadata
  85. viewController.editor = editor
  86. viewController.link = metadata.url
  87. navigationController.pushViewController(viewController, animated: true)
  88. }
  89. }
  90. } else {
  91. NCContentPresenter.shared.messageNotification("_error_", description: "_editor_unknown_", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: Int(k_CCErrorInternalError))
  92. }
  93. return
  94. }
  95. // RichDocument: Collabora
  96. if NCUtility.shared.isRichDocument(metadata) && NCCommunication.shared.isNetworkReachable() {
  97. if metadata.url == "" {
  98. NCCommunication.shared.createUrlRichdocuments(fileID: metadata.fileId) { (account, url, errorCode, errorDescription) in
  99. if errorCode == 0 && account == self.appDelegate.account && url != nil {
  100. if let navigationController = self.getPushNavigationController(viewController: viewController, serverUrl: metadata.serverUrl) {
  101. let viewController:NCViewerRichdocument = UIStoryboard(name: "NCViewerRichdocument", bundle: nil).instantiateInitialViewController() as! NCViewerRichdocument
  102. viewController.metadata = metadata
  103. viewController.link = url!
  104. navigationController.pushViewController(viewController, animated: true)
  105. }
  106. } else if errorCode != 0 {
  107. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  108. }
  109. }
  110. } else {
  111. if let navigationController = self.getPushNavigationController(viewController: viewController, serverUrl: metadata.serverUrl) {
  112. let viewController:NCViewerRichdocument = UIStoryboard(name: "NCViewerRichdocument", bundle: nil).instantiateInitialViewController() as! NCViewerRichdocument
  113. viewController.metadata = metadata
  114. viewController.link = metadata.url
  115. navigationController.pushViewController(viewController, animated: true)
  116. }
  117. }
  118. return
  119. }
  120. }
  121. // OTHER
  122. let fileNamePath = NSTemporaryDirectory() + metadata.fileNameView
  123. CCUtility.copyFile(atPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView), toPath: fileNamePath)
  124. viewerQuickLook = NCViewerQuickLook.init()
  125. viewerQuickLook?.quickLook(url: URL(fileURLWithPath: fileNamePath))
  126. }
  127. private func getPushNavigationController(viewController: UIViewController, serverUrl: String) -> UINavigationController? {
  128. if viewController is NCFiles || viewController is NCFavorite || viewController is NCOffline || viewController is NCRecent || viewController is NCFileViewInFolder {
  129. if serverUrl == appDelegate.activeServerUrl {
  130. return viewController.navigationController
  131. }
  132. }
  133. return nil
  134. }
  135. }
  136. //MARK: -
  137. extension NCViewer: NCSelectDelegate {
  138. func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String, items: [Any], buttonType: String, overwrite: Bool) {
  139. if let serverUrl = serverUrl {
  140. if buttonType == "done" {
  141. NCNetworking.shared.moveMetadata(self.metadata, serverUrlTo: serverUrl, overwrite: overwrite) { (errorCode, errorDescription) in
  142. if errorCode != 0 {
  143. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  144. }
  145. }
  146. } else {
  147. NCNetworking.shared.copyMetadata(self.metadata, serverUrlTo: serverUrl, overwrite: overwrite) { (errorCode, errorDescription) in
  148. if errorCode != 0 {
  149. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  150. }
  151. }
  152. }
  153. }
  154. }
  155. }