NCViewer.swift 11 KB

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