NCViewer.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. // Set Last Opening Date
  78. NCManageDatabase.shared.setLastOpeningDate(metadata: metadata)
  79. // PDF
  80. if metadata.contentType == "application/pdf" || metadata.contentType == "com.adobe.pdf" {
  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. // EDITORS
  91. let editors = utility.isDirectEditing(account: metadata.account, contentType: metadata.contentType)
  92. let availableRichDocument = utility.isRichDocument(metadata)
  93. // RichDocument: Collabora
  94. if (isRichDocument || (availableRichDocument && editors.isEmpty)) && NCGlobal.shared.capabilityRichdocumentsEnabled && NextcloudKit.shared.isNetworkReachable() {
  95. if metadata.url.isEmpty {
  96. NCActivityIndicator.shared.start(backgroundView: viewController.view)
  97. NextcloudKit.shared.createUrlRichdocuments(fileID: metadata.fileId) { account, url, _, error in
  98. NCActivityIndicator.shared.stop()
  99. if error == .success && account == self.appDelegate.account && url != nil {
  100. if let navigationController = viewController.navigationController,
  101. let viewController: NCViewerRichdocument = UIStoryboard(name: "NCViewerRichdocument", bundle: nil).instantiateInitialViewController() as? NCViewerRichdocument {
  102. viewController.metadata = metadata
  103. viewController.link = url!
  104. viewController.imageIcon = imageIcon
  105. navigationController.pushViewController(viewController, animated: true)
  106. }
  107. } else if error != .success {
  108. NCContentPresenter().showError(error: error)
  109. }
  110. }
  111. } else {
  112. if let navigationController = viewController.navigationController,
  113. let viewController: NCViewerRichdocument = UIStoryboard(name: "NCViewerRichdocument", bundle: nil).instantiateInitialViewController() as? NCViewerRichdocument {
  114. viewController.metadata = metadata
  115. viewController.link = metadata.url
  116. viewController.imageIcon = imageIcon
  117. navigationController.pushViewController(viewController, animated: true)
  118. }
  119. }
  120. return
  121. }
  122. // DirectEditing: Nextcloud Text - OnlyOffice
  123. if !editors.isEmpty, NextcloudKit.shared.isNetworkReachable() {
  124. if editor.isEmpty {
  125. if editors.contains(NCGlobal.shared.editorText) {
  126. editor = NCGlobal.shared.editorText
  127. } else if editors.contains(NCGlobal.shared.editorOnlyoffice) {
  128. editor = NCGlobal.shared.editorOnlyoffice
  129. }
  130. }
  131. if editor == NCGlobal.shared.editorText || editor == NCGlobal.shared.editorOnlyoffice {
  132. if metadata.url.isEmpty {
  133. let fileNamePath = utilityFileSystem.getFileNamePath(metadata.fileName, serverUrl: metadata.serverUrl, urlBase: metadata.urlBase, userId: metadata.userId)
  134. var options = NKRequestOptions()
  135. if editor == NCGlobal.shared.editorOnlyoffice {
  136. options = NKRequestOptions(customUserAgent: utility.getCustomUserAgentOnlyOffice())
  137. } else {
  138. options = NKRequestOptions(customUserAgent: utility.getCustomUserAgentNCText())
  139. }
  140. NCActivityIndicator.shared.start(backgroundView: viewController.view)
  141. NextcloudKit.shared.NCTextOpenFile(fileNamePath: fileNamePath, editor: editor, options: options) { account, url, _, error in
  142. NCActivityIndicator.shared.stop()
  143. if error == .success && account == self.appDelegate.account && url != nil {
  144. if let navigationController = viewController.navigationController,
  145. let viewController: NCViewerNextcloudText = UIStoryboard(name: "NCViewerNextcloudText", bundle: nil).instantiateInitialViewController() as? NCViewerNextcloudText {
  146. viewController.metadata = metadata
  147. viewController.editor = editor
  148. viewController.link = url!
  149. viewController.imageIcon = imageIcon
  150. navigationController.pushViewController(viewController, animated: true)
  151. }
  152. } else if error != .success {
  153. NCContentPresenter().showError(error: error)
  154. }
  155. }
  156. } else {
  157. if let navigationController = viewController.navigationController,
  158. let viewController: NCViewerNextcloudText = UIStoryboard(name: "NCViewerNextcloudText", bundle: nil).instantiateInitialViewController() as? NCViewerNextcloudText {
  159. viewController.metadata = metadata
  160. viewController.editor = editor
  161. viewController.link = metadata.url
  162. viewController.imageIcon = imageIcon
  163. navigationController.pushViewController(viewController, animated: true)
  164. }
  165. }
  166. } else {
  167. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_editor_unknown_")
  168. NCContentPresenter().showError(error: error)
  169. }
  170. return
  171. }
  172. }
  173. // QLPreview
  174. let item = URL(fileURLWithPath: utilityFileSystem.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))
  175. if QLPreviewController.canPreview(item as QLPreviewItem) {
  176. let fileNamePath = NSTemporaryDirectory() + metadata.fileNameView
  177. utilityFileSystem.copyFile(atPath: utilityFileSystem.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView), toPath: fileNamePath)
  178. let viewerQuickLook = NCViewerQuickLook(with: URL(fileURLWithPath: fileNamePath), isEditingEnabled: false, metadata: metadata)
  179. viewController.present(viewerQuickLook, animated: true)
  180. } else {
  181. // Document Interaction Controller
  182. NCActionCenter.shared.openDocumentController(metadata: metadata)
  183. }
  184. }
  185. }
  186. // MARK: - SELECT
  187. extension NCViewer: NCSelectDelegate {
  188. func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String, items: [Any], indexPath: [IndexPath], overwrite: Bool, copy: Bool, move: Bool) {
  189. if let serverUrl = serverUrl,
  190. let metadata = items[0] as? tableMetadata {
  191. if move {
  192. Task {
  193. let error = await NCNetworking.shared.moveMetadata(metadata, serverUrlTo: serverUrl, overwrite: overwrite)
  194. if error != .success {
  195. NCContentPresenter().showError(error: error)
  196. }
  197. }
  198. } else if copy {
  199. Task {
  200. let error = await NCNetworking.shared.copyMetadata(metadata, serverUrlTo: serverUrl, overwrite: overwrite)
  201. if error != .success {
  202. NCContentPresenter().showError(error: error)
  203. }
  204. }
  205. }
  206. }
  207. }
  208. }