NCViewer.swift 12 KB

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