NCViewer.swift 11 KB

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