NCViewer.swift 11 KB

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