NCViewer.swift 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. let utilityFileSystem = NCUtilityFileSystem()
  28. let utility = NCUtility()
  29. let database = NCManageDatabase.shared
  30. private var viewerQuickLook: NCViewerQuickLook?
  31. func view(viewController: UIViewController, metadata: tableMetadata, ocIds: [String]? = nil, image: UIImage? = nil) {
  32. let session = NCSession.shared.getSession(account: metadata.account)
  33. // URL
  34. if metadata.classFile == NKCommon.TypeClassFile.url.rawValue {
  35. // nextcloudtalk://open-conversation?server={serverURL}&user={userId}&withRoomToken={roomToken}
  36. if metadata.name == NCGlobal.shared.talkName {
  37. let pathComponents = metadata.url.components(separatedBy: "/")
  38. if pathComponents.contains("call") {
  39. let talkComponents = pathComponents.last?.components(separatedBy: "#")
  40. if let roomToken = talkComponents?.first {
  41. let urlString = "nextcloudtalk://open-conversation?server=\(session.urlBase)&user=\(session.userId)&withRoomToken=\(roomToken)"
  42. if let url = URL(string: urlString), UIApplication.shared.canOpenURL(url) {
  43. UIApplication.shared.open(url)
  44. return
  45. }
  46. }
  47. }
  48. }
  49. if let url = URL(string: metadata.url) {
  50. UIApplication.shared.open(url)
  51. }
  52. return
  53. }
  54. // IMAGE AUDIO VIDEO
  55. if metadata.isImage || metadata.isAudioOrVideo {
  56. if let navigationController = viewController.navigationController,
  57. let viewerMediaPageContainer: NCViewerMediaPage = UIStoryboard(name: "NCViewerMediaPage", bundle: nil).instantiateInitialViewController() as? NCViewerMediaPage {
  58. viewerMediaPageContainer.delegateViewController = viewController
  59. if let ocIds {
  60. viewerMediaPageContainer.currentIndex = ocIds.firstIndex(where: { $0 == metadata.ocId }) ?? 0
  61. viewerMediaPageContainer.ocIds = ocIds
  62. } else {
  63. viewerMediaPageContainer.currentIndex = 0
  64. viewerMediaPageContainer.ocIds = [metadata.ocId]
  65. }
  66. navigationController.pushViewController(viewerMediaPageContainer, animated: true)
  67. }
  68. return
  69. }
  70. // DOCUMENTS
  71. if metadata.classFile == NKCommon.TypeClassFile.document.rawValue {
  72. // Set Last Opening Date
  73. self.database.setLastOpeningDate(metadata: metadata)
  74. // PDF
  75. if metadata.isPDF {
  76. if let navigationController = viewController.navigationController,
  77. let viewController: NCViewerPDF = UIStoryboard(name: "NCViewerPDF", bundle: nil).instantiateInitialViewController() as? NCViewerPDF {
  78. viewController.metadata = metadata
  79. viewController.titleView = metadata.fileNameView
  80. viewController.imageIcon = image
  81. navigationController.pushViewController(viewController, animated: true)
  82. }
  83. return
  84. }
  85. // RichDocument: Collabora
  86. if metadata.isAvailableRichDocumentEditorView {
  87. if metadata.url.isEmpty {
  88. NCActivityIndicator.shared.start(backgroundView: viewController.view)
  89. NextcloudKit.shared.createUrlRichdocuments(fileID: metadata.fileId, account: metadata.account) { _, url, _, error in
  90. NCActivityIndicator.shared.stop()
  91. if error == .success, url != nil {
  92. if let navigationController = viewController.navigationController,
  93. let viewController: NCViewerRichDocument = UIStoryboard(name: "NCViewerRichdocument", bundle: nil).instantiateInitialViewController() as? NCViewerRichDocument {
  94. viewController.metadata = metadata
  95. viewController.link = url!
  96. viewController.imageIcon = image
  97. navigationController.pushViewController(viewController, animated: true)
  98. }
  99. } else if error != .success {
  100. NCContentPresenter().showError(error: error)
  101. }
  102. }
  103. } else {
  104. if let navigationController = viewController.navigationController,
  105. let viewController: NCViewerRichDocument = UIStoryboard(name: "NCViewerRichdocument", bundle: nil).instantiateInitialViewController() as? NCViewerRichDocument {
  106. viewController.metadata = metadata
  107. viewController.link = metadata.url
  108. viewController.imageIcon = image
  109. navigationController.pushViewController(viewController, animated: true)
  110. }
  111. }
  112. return
  113. }
  114. // DirectEditing: Nextcloud Text - OnlyOffice
  115. if metadata.isAvailableDirectEditingEditorView {
  116. var options = NKRequestOptions()
  117. var editor = ""
  118. let editors = utility.editorsDirectEditing(account: metadata.account, contentType: metadata.contentType)
  119. if editors.contains(NCGlobal.shared.editorText) {
  120. editor = NCGlobal.shared.editorText
  121. options = NKRequestOptions(customUserAgent: utility.getCustomUserAgentNCText())
  122. } else if editors.contains(NCGlobal.shared.editorOnlyoffice) {
  123. editor = NCGlobal.shared.editorOnlyoffice
  124. options = NKRequestOptions(customUserAgent: utility.getCustomUserAgentOnlyOffice())
  125. }
  126. if metadata.url.isEmpty {
  127. let fileNamePath = utilityFileSystem.getFileNamePath(metadata.fileName, serverUrl: metadata.serverUrl, session: session)
  128. NCActivityIndicator.shared.start(backgroundView: viewController.view)
  129. NextcloudKit.shared.NCTextOpenFile(fileNamePath: fileNamePath, editor: editor, account: metadata.account, options: options) { _, url, _, error in
  130. NCActivityIndicator.shared.stop()
  131. if error == .success, url != nil {
  132. if let navigationController = viewController.navigationController,
  133. let viewController: NCViewerNextcloudText = UIStoryboard(name: "NCViewerNextcloudText", bundle: nil).instantiateInitialViewController() as? NCViewerNextcloudText {
  134. viewController.metadata = metadata
  135. viewController.editor = editor
  136. viewController.link = url!
  137. viewController.imageIcon = image
  138. navigationController.pushViewController(viewController, animated: true)
  139. }
  140. } else if error != .success {
  141. NCContentPresenter().showError(error: error)
  142. }
  143. }
  144. } else {
  145. if let navigationController = viewController.navigationController,
  146. let viewController: NCViewerNextcloudText = UIStoryboard(name: "NCViewerNextcloudText", bundle: nil).instantiateInitialViewController() as? NCViewerNextcloudText {
  147. viewController.metadata = metadata
  148. viewController.editor = editor
  149. viewController.link = metadata.url
  150. viewController.imageIcon = image
  151. navigationController.pushViewController(viewController, animated: true)
  152. }
  153. }
  154. return
  155. }
  156. }
  157. // QLPreview
  158. let item = URL(fileURLWithPath: utilityFileSystem.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))
  159. if QLPreviewController.canPreview(item as QLPreviewItem) {
  160. let fileNamePath = NSTemporaryDirectory() + metadata.fileNameView
  161. utilityFileSystem.copyFile(atPath: utilityFileSystem.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView), toPath: fileNamePath)
  162. let viewerQuickLook = NCViewerQuickLook(with: URL(fileURLWithPath: fileNamePath), isEditingEnabled: false, metadata: metadata)
  163. viewController.present(viewerQuickLook, animated: true)
  164. } else {
  165. // Document Interaction Controller
  166. if let controller = viewController.tabBarController as? NCMainTabBarController {
  167. NCActionCenter.shared.openDocumentController(metadata: metadata, controller: controller)
  168. }
  169. }
  170. }
  171. }