NCViewer.swift 12 KB

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