NCViewer.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 UIKit
  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], imageIcon: UIImage?, editor: String = "", isRichDocument: Bool = false) {
  35. self.metadata = metadata
  36. self.metadatas = metadatas
  37. var editor = editor
  38. var xxxxxxx = NCCommunicationCommon.shared.getInternalTypeIdentifier(typeIdentifier: metadata.contentType)
  39. // IMAGE AUDIO VIDEO
  40. if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.audio.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
  41. if let navigationController = viewController.navigationController {
  42. let viewerMediaPageContainer:NCViewerMedia = UIStoryboard(name: "NCViewerMedia", bundle: nil).instantiateInitialViewController() as! NCViewerMedia
  43. var index = 0
  44. for medatasImage in metadatas {
  45. if medatasImage.ocId == metadata.ocId {
  46. viewerMediaPageContainer.currentIndex = index
  47. break
  48. }
  49. index += 1
  50. }
  51. viewerMediaPageContainer.metadatas = metadatas
  52. navigationController.pushViewController(viewerMediaPageContainer, animated: true)
  53. }
  54. return
  55. }
  56. // DOCUMENTS
  57. if metadata.classFile == NCCommunicationCommon.typeClassFile.document.rawValue {
  58. // PDF
  59. if metadata.contentType == "application/pdf" || metadata.contentType == "com.adobe.pdf" {
  60. if let navigationController = viewController.navigationController {
  61. let viewController:NCViewerPDF = UIStoryboard(name: "NCViewerPDF", bundle: nil).instantiateInitialViewController() as! NCViewerPDF
  62. viewController.metadata = metadata
  63. viewController.imageIcon = imageIcon
  64. navigationController.pushViewController(viewController, animated: true)
  65. }
  66. return
  67. }
  68. // EDITORS
  69. let editors = NCUtility.shared.isDirectEditing(account: metadata.account, contentType: metadata.contentType)
  70. let availableRichDocument = NCUtility.shared.isRichDocument(metadata)
  71. // RichDocument: Collabora
  72. if (isRichDocument || (availableRichDocument && editors.count == 0)) && NCCommunication.shared.isNetworkReachable() {
  73. if metadata.url == "" {
  74. NCUtility.shared.startActivityIndicator(backgroundView: viewController.view, blurEffect: true)
  75. NCCommunication.shared.createUrlRichdocuments(fileID: metadata.fileId) { (account, url, errorCode, errorDescription) in
  76. NCUtility.shared.stopActivityIndicator()
  77. if errorCode == 0 && account == self.appDelegate.account && url != nil {
  78. if let navigationController = viewController.navigationController {
  79. let viewController:NCViewerRichdocument = UIStoryboard(name: "NCViewerRichdocument", bundle: nil).instantiateInitialViewController() as! NCViewerRichdocument
  80. viewController.metadata = metadata
  81. viewController.link = url!
  82. viewController.imageIcon = imageIcon
  83. navigationController.pushViewController(viewController, animated: true)
  84. }
  85. } else if errorCode != 0 {
  86. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  87. }
  88. }
  89. } else {
  90. if let navigationController = viewController.navigationController {
  91. let viewController:NCViewerRichdocument = UIStoryboard(name: "NCViewerRichdocument", bundle: nil).instantiateInitialViewController() as! NCViewerRichdocument
  92. viewController.metadata = metadata
  93. viewController.link = metadata.url
  94. viewController.imageIcon = imageIcon
  95. navigationController.pushViewController(viewController, animated: true)
  96. }
  97. }
  98. return
  99. }
  100. // DirectEditing: Nextcloud Text - OnlyOffice
  101. if editors.count > 0 && NCCommunication.shared.isNetworkReachable() {
  102. if editor == "" {
  103. if editors.contains(NCGlobal.shared.editorText) {
  104. editor = NCGlobal.shared.editorText
  105. } else if editors.contains(NCGlobal.shared.editorOnlyoffice) {
  106. editor = NCGlobal.shared.editorOnlyoffice
  107. }
  108. }
  109. if editor == NCGlobal.shared.editorText || editor == NCGlobal.shared.editorOnlyoffice {
  110. if metadata.url == "" {
  111. var customUserAgent: String?
  112. let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, urlBase: metadata.urlBase, account: metadata.account)!
  113. if editor == NCGlobal.shared.editorOnlyoffice {
  114. customUserAgent = NCUtility.shared.getCustomUserAgentOnlyOffice()
  115. }
  116. NCUtility.shared.startActivityIndicator(backgroundView: viewController.view, blurEffect: true)
  117. NCCommunication.shared.NCTextOpenFile(fileNamePath: fileNamePath, editor: editor, customUserAgent: customUserAgent) { (account, url, errorCode, errorMessage) in
  118. NCUtility.shared.stopActivityIndicator()
  119. if errorCode == 0 && account == self.appDelegate.account && url != nil {
  120. if let navigationController = viewController.navigationController {
  121. let viewController:NCViewerNextcloudText = UIStoryboard(name: "NCViewerNextcloudText", bundle: nil).instantiateInitialViewController() as! NCViewerNextcloudText
  122. viewController.metadata = metadata
  123. viewController.editor = editor
  124. viewController.link = url!
  125. viewController.imageIcon = imageIcon
  126. navigationController.pushViewController(viewController, animated: true)
  127. }
  128. } else if errorCode != 0 {
  129. NCContentPresenter.shared.messageNotification("_error_", description: errorMessage, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  130. }
  131. }
  132. } else {
  133. if let navigationController = viewController.navigationController {
  134. let viewController:NCViewerNextcloudText = UIStoryboard(name: "NCViewerNextcloudText", bundle: nil).instantiateInitialViewController() as! NCViewerNextcloudText
  135. viewController.metadata = metadata
  136. viewController.editor = editor
  137. viewController.link = metadata.url
  138. viewController.imageIcon = imageIcon
  139. navigationController.pushViewController(viewController, animated: true)
  140. }
  141. }
  142. } else {
  143. NCContentPresenter.shared.messageNotification("_error_", description: "_editor_unknown_", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.errorInternalError)
  144. }
  145. return
  146. }
  147. }
  148. // OTHER
  149. let fileNamePath = NSTemporaryDirectory() + metadata.fileNameView
  150. CCUtility.copyFile(atPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView), toPath: fileNamePath)
  151. let viewerQuickLook = NCViewerQuickLook(with: URL(fileURLWithPath: fileNamePath), editingMode: false, metadata: metadata)
  152. let navigationController = UINavigationController(rootViewController: viewerQuickLook)
  153. navigationController.modalPresentationStyle = .overFullScreen
  154. viewController.present(navigationController, animated: true)
  155. }
  156. }
  157. //MARK: - SELECT
  158. extension NCViewer: NCSelectDelegate {
  159. func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String, items: [Any], overwrite: Bool, copy: Bool, move: Bool) {
  160. if let serverUrl = serverUrl {
  161. let metadata = items[0] as! tableMetadata
  162. if move {
  163. NCNetworking.shared.moveMetadata(metadata, serverUrlTo: serverUrl, overwrite: overwrite) { (errorCode, errorDescription) in
  164. if errorCode != 0 {
  165. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  166. }
  167. }
  168. } else if copy {
  169. NCNetworking.shared.copyMetadata(metadata, serverUrlTo: serverUrl, overwrite: overwrite) { (errorCode, errorDescription) in
  170. if errorCode != 0 {
  171. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  172. }
  173. }
  174. }
  175. }
  176. }
  177. }