NCViewer.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  32. private var viewerQuickLook: NCViewerQuickLook?
  33. private var metadata = tableMetadata()
  34. private var metadatas: [tableMetadata] = []
  35. func view(viewController: UIViewController, metadata: tableMetadata, metadatas: [tableMetadata], imageIcon: UIImage?, editor: String = "", isRichDocument: Bool = false) {
  36. self.metadata = metadata
  37. self.metadatas = metadatas
  38. var editor = editor
  39. var xxxxxxx = NKCommon.shared.getInternalTypeIdentifier(typeIdentifier: metadata.contentType)
  40. // URL
  41. if metadata.classFile == NKCommon.typeClassFile.url.rawValue {
  42. // nextcloudtalk://open-conversation?server={serverURL}&user={userId}&withRoomToken={roomToken}
  43. if metadata.name == NCGlobal.shared.talkName {
  44. let pathComponents = metadata.url.components(separatedBy: "/")
  45. if pathComponents.contains("call") {
  46. let talkComponents = pathComponents.last?.components(separatedBy: "#")
  47. if let roomToken = talkComponents?.first {
  48. let urlString = "nextcloudtalk://open-conversation?server=\(appDelegate.urlBase)&user=\(appDelegate.userId)&withRoomToken=\(roomToken)"
  49. if let url = URL(string: urlString), UIApplication.shared.canOpenURL(url) {
  50. UIApplication.shared.open(url)
  51. return
  52. }
  53. }
  54. }
  55. }
  56. if let url = URL(string: metadata.url) {
  57. UIApplication.shared.open(url)
  58. }
  59. return
  60. }
  61. // IMAGE AUDIO VIDEO
  62. if metadata.classFile == NKCommon.typeClassFile.image.rawValue || metadata.classFile == NKCommon.typeClassFile.audio.rawValue || metadata.classFile == NKCommon.typeClassFile.video.rawValue {
  63. if let navigationController = viewController.navigationController {
  64. let viewerMediaPageContainer: NCViewerMediaPage = UIStoryboard(name: "NCViewerMediaPage", bundle: nil).instantiateInitialViewController() as! NCViewerMediaPage
  65. var index = 0
  66. for medatasImage in metadatas {
  67. if medatasImage.ocId == metadata.ocId {
  68. viewerMediaPageContainer.currentIndex = index
  69. break
  70. }
  71. index += 1
  72. }
  73. viewerMediaPageContainer.metadatas = metadatas
  74. navigationController.pushViewController(viewerMediaPageContainer, animated: true)
  75. }
  76. return
  77. }
  78. // DOCUMENTS
  79. if metadata.classFile == NKCommon.typeClassFile.document.rawValue {
  80. // PDF
  81. if metadata.contentType == "application/pdf" || metadata.contentType == "com.adobe.pdf" {
  82. if let navigationController = viewController.navigationController {
  83. let viewController: NCViewerPDF = UIStoryboard(name: "NCViewerPDF", bundle: nil).instantiateInitialViewController() as! NCViewerPDF
  84. viewController.metadata = metadata
  85. viewController.imageIcon = imageIcon
  86. navigationController.pushViewController(viewController, animated: true)
  87. }
  88. return
  89. }
  90. // EDITORS
  91. let editors = NCUtility.shared.isDirectEditing(account: metadata.account, contentType: metadata.contentType)
  92. let availableRichDocument = NCUtility.shared.isRichDocument(metadata)
  93. // RichDocument: Collabora
  94. if (isRichDocument || (availableRichDocument && editors.count == 0)) && NextcloudKit.shared.isNetworkReachable() {
  95. if metadata.url == "" {
  96. NCActivityIndicator.shared.start(backgroundView: viewController.view)
  97. NextcloudKit.shared.createUrlRichdocuments(fileID: metadata.fileId) { account, url, data, 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.shared.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.count > 0 && NextcloudKit.shared.isNetworkReachable() {
  124. if editor == "" {
  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 == "" {
  133. let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, urlBase: metadata.urlBase, userId: metadata.userId, account: metadata.account)!
  134. var options = NKRequestOptions()
  135. if editor == NCGlobal.shared.editorOnlyoffice {
  136. options = NKRequestOptions(customUserAgent: NCUtility.shared.getCustomUserAgentOnlyOffice())
  137. } else {
  138. options = NKRequestOptions(customUserAgent: NCUtility.shared.getCustomUserAgentNCText())
  139. }
  140. NCActivityIndicator.shared.start(backgroundView: viewController.view)
  141. NextcloudKit.shared.NCTextOpenFile(fileNamePath: fileNamePath, editor: editor, options: options) { account, url, data, 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.shared.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.shared.showError(error: error)
  169. }
  170. return
  171. }
  172. }
  173. // QLPreview
  174. let item = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView))
  175. if QLPreviewController.canPreview(item as QLPreviewItem) {
  176. let fileNamePath = NSTemporaryDirectory() + metadata.fileNameView
  177. CCUtility.copyFile(atPath: CCUtility.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. NCFunctionCenter.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], overwrite: Bool, copy: Bool, move: Bool) {
  189. if let serverUrl = serverUrl {
  190. let metadata = items[0] as! tableMetadata
  191. if move {
  192. NCNetworking.shared.moveMetadata(metadata, serverUrlTo: serverUrl, overwrite: overwrite) { error in
  193. if error != .success {
  194. NCContentPresenter.shared.showError(error: error)
  195. }
  196. }
  197. } else if copy {
  198. NCNetworking.shared.copyMetadata(metadata, serverUrlTo: serverUrl, overwrite: overwrite) { error in
  199. if error != .success {
  200. NCContentPresenter.shared.showError(error: error)
  201. }
  202. }
  203. }
  204. }
  205. }
  206. }