NCViewer.swift 12 KB

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