NCViewer.swift 11 KB

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