NCViewer.swift 12 KB

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