NCViewer.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 NCCommunication
  25. class NCViewer: NSObject {
  26. @objc static let shared: NCViewer = {
  27. let instance = NCViewer()
  28. return instance
  29. }()
  30. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  31. private var viewerQuickLook: NCViewerQuickLook?
  32. private var metadata = tableMetadata()
  33. private var metadatas: [tableMetadata] = []
  34. func view(viewController: UIViewController, metadata: tableMetadata, metadatas: [tableMetadata], imageIcon: UIImage?, editor: String = "", isRichDocument: Bool = false) {
  35. self.metadata = metadata
  36. self.metadatas = metadatas
  37. var editor = editor
  38. // IMAGE AUDIO VIDEO
  39. if metadata.classFile == NCGlobal.shared.metadataTypeFileImage || metadata.classFile == NCGlobal.shared.metadataTypeFileAudio || metadata.classFile == NCGlobal.shared.metadataTypeFileVideo {
  40. if let navigationController = viewController.navigationController {
  41. let viewerImagePageContainer:NCViewerImage = UIStoryboard(name: "NCViewerImage", bundle: nil).instantiateInitialViewController() as! NCViewerImage
  42. var index = 0
  43. for medatasImage in metadatas {
  44. if medatasImage.ocId == metadata.ocId {
  45. viewerImagePageContainer.currentIndex = index
  46. break
  47. }
  48. index += 1
  49. }
  50. viewerImagePageContainer.metadatas = metadatas
  51. navigationController.pushViewController(viewerImagePageContainer, animated: true)
  52. }
  53. return
  54. }
  55. // DOCUMENTS
  56. if metadata.classFile == NCGlobal.shared.metadataTypeFileDocument {
  57. // PDF
  58. if metadata.contentType == "application/pdf" || metadata.contentType == "com.adobe.pdf" {
  59. if let navigationController = viewController.navigationController {
  60. let viewController:NCViewerPDF = UIStoryboard(name: "NCViewerPDF", bundle: nil).instantiateInitialViewController() as! NCViewerPDF
  61. viewController.metadata = metadata
  62. viewController.imageIcon = imageIcon
  63. navigationController.pushViewController(viewController, animated: true)
  64. }
  65. return
  66. }
  67. // EDITORS
  68. let editors = NCUtility.shared.isDirectEditing(account: metadata.account, contentType: metadata.contentType)
  69. let availableRichDocument = NCUtility.shared.isRichDocument(metadata)
  70. // RichDocument: Collabora
  71. if (isRichDocument || (availableRichDocument && editors.count == 0)) && NCCommunication.shared.isNetworkReachable() {
  72. if metadata.url == "" {
  73. NCUtility.shared.startActivityIndicator(backgroundView: viewController.view, blurEffect: true)
  74. NCCommunication.shared.createUrlRichdocuments(fileID: metadata.fileId) { (account, url, errorCode, errorDescription) in
  75. NCUtility.shared.stopActivityIndicator()
  76. if errorCode == 0 && account == self.appDelegate.account && url != nil {
  77. if let navigationController = viewController.navigationController {
  78. let viewController:NCViewerRichdocument = UIStoryboard(name: "NCViewerRichdocument", bundle: nil).instantiateInitialViewController() as! NCViewerRichdocument
  79. viewController.metadata = metadata
  80. viewController.link = url!
  81. viewController.imageIcon = imageIcon
  82. navigationController.pushViewController(viewController, animated: true)
  83. }
  84. } else if errorCode != 0 {
  85. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  86. }
  87. }
  88. } else {
  89. if let navigationController = viewController.navigationController {
  90. let viewController:NCViewerRichdocument = UIStoryboard(name: "NCViewerRichdocument", bundle: nil).instantiateInitialViewController() as! NCViewerRichdocument
  91. viewController.metadata = metadata
  92. viewController.link = metadata.url
  93. viewController.imageIcon = imageIcon
  94. navigationController.pushViewController(viewController, animated: true)
  95. }
  96. }
  97. return
  98. }
  99. // DirectEditing: Nextcloud Text - OnlyOffice
  100. if editors.count > 0 && NCCommunication.shared.isNetworkReachable() {
  101. if editor == "" {
  102. if editors.contains(NCGlobal.shared.editorText) {
  103. editor = NCGlobal.shared.editorText
  104. } else if editors.contains(NCGlobal.shared.editorOnlyoffice) {
  105. editor = NCGlobal.shared.editorOnlyoffice
  106. }
  107. }
  108. if editor == NCGlobal.shared.editorText || editor == NCGlobal.shared.editorOnlyoffice {
  109. if metadata.url == "" {
  110. var customUserAgent: String?
  111. let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, urlBase: metadata.urlBase, account: metadata.account)!
  112. if editor == NCGlobal.shared.editorOnlyoffice {
  113. customUserAgent = NCUtility.shared.getCustomUserAgentOnlyOffice()
  114. }
  115. NCUtility.shared.startActivityIndicator(backgroundView: viewController.view, blurEffect: true)
  116. NCCommunication.shared.NCTextOpenFile(fileNamePath: fileNamePath, editor: editor, customUserAgent: customUserAgent) { (account, url, errorCode, errorMessage) in
  117. NCUtility.shared.stopActivityIndicator()
  118. if errorCode == 0 && account == self.appDelegate.account && url != nil {
  119. if let navigationController = viewController.navigationController {
  120. let viewController:NCViewerNextcloudText = UIStoryboard(name: "NCViewerNextcloudText", bundle: nil).instantiateInitialViewController() as! NCViewerNextcloudText
  121. viewController.metadata = metadata
  122. viewController.editor = editor
  123. viewController.link = url!
  124. viewController.imageIcon = imageIcon
  125. navigationController.pushViewController(viewController, animated: true)
  126. }
  127. } else if errorCode != 0 {
  128. NCContentPresenter.shared.messageNotification("_error_", description: errorMessage, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  129. }
  130. }
  131. } else {
  132. if let navigationController = viewController.navigationController {
  133. let viewController:NCViewerNextcloudText = UIStoryboard(name: "NCViewerNextcloudText", bundle: nil).instantiateInitialViewController() as! NCViewerNextcloudText
  134. viewController.metadata = metadata
  135. viewController.editor = editor
  136. viewController.link = metadata.url
  137. viewController.imageIcon = imageIcon
  138. navigationController.pushViewController(viewController, animated: true)
  139. }
  140. }
  141. } else {
  142. NCContentPresenter.shared.messageNotification("_error_", description: "_editor_unknown_", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.errorInternalError)
  143. }
  144. return
  145. }
  146. }
  147. // OTHER
  148. let fileNamePath = NSTemporaryDirectory() + metadata.fileNameView
  149. CCUtility.copyFile(atPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView), toPath: fileNamePath)
  150. let viewerQuickLook = NCViewerQuickLook(with: URL(fileURLWithPath: fileNamePath), editingMode: false, metadata: metadata)
  151. let navigationController = UINavigationController(rootViewController: viewerQuickLook)
  152. navigationController.modalPresentationStyle = .overFullScreen
  153. viewController.present(navigationController, animated: true)
  154. }
  155. }
  156. //MARK: - SELECT
  157. extension NCViewer: NCSelectDelegate {
  158. func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String, items: [Any], overwrite: Bool, copy: Bool, move: Bool) {
  159. if let serverUrl = serverUrl {
  160. let metadata = items[0] as! tableMetadata
  161. if move {
  162. NCNetworking.shared.moveMetadata(metadata, serverUrlTo: serverUrl, overwrite: overwrite) { (errorCode, errorDescription) in
  163. if errorCode != 0 {
  164. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  165. }
  166. }
  167. } else if copy {
  168. NCNetworking.shared.copyMetadata(metadata, serverUrlTo: serverUrl, overwrite: overwrite) { (errorCode, errorDescription) in
  169. if errorCode != 0 {
  170. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  171. }
  172. }
  173. }
  174. }
  175. }
  176. }