NCViewer.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 Foundation
  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]) {
  35. self.metadata = metadata
  36. self.metadatas = metadatas
  37. // IMAGE AUDIO VIDEO
  38. if metadata.typeFile == NCBrandGlobal.shared.metadataTypeFileImage || metadata.typeFile == NCBrandGlobal.shared.metadataTypeFileAudio || metadata.typeFile == NCBrandGlobal.shared.metadataTypeFileVideo {
  39. if let navigationController = viewController.navigationController {
  40. let viewerImagePageContainer:NCViewerImage = UIStoryboard(name: "NCViewerImage", bundle: nil).instantiateInitialViewController() as! NCViewerImage
  41. var index = 0
  42. for medatasImage in metadatas {
  43. if medatasImage.ocId == metadata.ocId {
  44. viewerImagePageContainer.currentIndex = index
  45. break
  46. }
  47. index += 1
  48. }
  49. viewerImagePageContainer.metadatas = metadatas
  50. navigationController.pushViewController(viewerImagePageContainer, animated: true)
  51. }
  52. return
  53. }
  54. // DOCUMENTS
  55. if metadata.typeFile == NCBrandGlobal.shared.metadataTypeFileDocument {
  56. // PDF
  57. if metadata.contentType == "application/pdf" {
  58. if let navigationController = viewController.navigationController {
  59. let viewController:NCViewerPDF = UIStoryboard(name: "NCViewerPDF", bundle: nil).instantiateInitialViewController() as! NCViewerPDF
  60. viewController.metadata = metadata
  61. navigationController.pushViewController(viewController, animated: true)
  62. }
  63. return
  64. }
  65. // DirectEditinf: Nextcloud Text - OnlyOffice
  66. if NCUtility.shared.isDirectEditing(account: metadata.account, contentType: metadata.contentType) != nil && NCCommunication.shared.isNetworkReachable() {
  67. guard let editor = NCUtility.shared.isDirectEditing(account: metadata.account, contentType: metadata.contentType) else { return }
  68. if editor == NCBrandGlobal.shared.editorText || editor == NCBrandGlobal.shared.editorOnlyoffice {
  69. if metadata.url == "" {
  70. var customUserAgent: String?
  71. let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, urlBase: metadata.urlBase, account: metadata.account)!
  72. if editor == NCBrandGlobal.shared.editorOnlyoffice {
  73. customUserAgent = NCUtility.shared.getCustomUserAgentOnlyOffice()
  74. }
  75. NCUtility.shared.startActivityIndicator(view: viewController.view)
  76. NCCommunication.shared.NCTextOpenFile(fileNamePath: fileNamePath, editor: editor, customUserAgent: customUserAgent) { (account, url, errorCode, errorMessage) in
  77. NCUtility.shared.stopActivityIndicator()
  78. if errorCode == 0 && account == self.appDelegate.account && url != nil {
  79. if let navigationController = viewController.navigationController {
  80. let viewController:NCViewerNextcloudText = UIStoryboard(name: "NCViewerNextcloudText", bundle: nil).instantiateInitialViewController() as! NCViewerNextcloudText
  81. viewController.metadata = metadata
  82. viewController.editor = editor
  83. viewController.link = url!
  84. navigationController.pushViewController(viewController, animated: true)
  85. }
  86. } else if errorCode != 0 {
  87. NCContentPresenter.shared.messageNotification("_error_", description: errorMessage, delay: NCBrandGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  88. }
  89. }
  90. } else {
  91. if let navigationController = viewController.navigationController {
  92. let viewController:NCViewerNextcloudText = UIStoryboard(name: "NCViewerNextcloudText", bundle: nil).instantiateInitialViewController() as! NCViewerNextcloudText
  93. viewController.metadata = metadata
  94. viewController.editor = editor
  95. viewController.link = metadata.url
  96. navigationController.pushViewController(viewController, animated: true)
  97. }
  98. }
  99. } else {
  100. NCContentPresenter.shared.messageNotification("_error_", description: "_editor_unknown_", delay: NCBrandGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCBrandGlobal.shared.ErrorInternalError)
  101. }
  102. return
  103. }
  104. // RichDocument: Collabora
  105. if NCUtility.shared.isRichDocument(metadata) && NCCommunication.shared.isNetworkReachable() {
  106. if metadata.url == "" {
  107. NCUtility.shared.startActivityIndicator(view: viewController.view)
  108. NCCommunication.shared.createUrlRichdocuments(fileID: metadata.fileId) { (account, url, errorCode, errorDescription) in
  109. NCUtility.shared.stopActivityIndicator()
  110. if errorCode == 0 && account == self.appDelegate.account && url != nil {
  111. if let navigationController = viewController.navigationController {
  112. let viewController:NCViewerRichdocument = UIStoryboard(name: "NCViewerRichdocument", bundle: nil).instantiateInitialViewController() as! NCViewerRichdocument
  113. viewController.metadata = metadata
  114. viewController.link = url!
  115. navigationController.pushViewController(viewController, animated: true)
  116. }
  117. } else if errorCode != 0 {
  118. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCBrandGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  119. }
  120. }
  121. } else {
  122. if let navigationController = viewController.navigationController {
  123. let viewController:NCViewerRichdocument = UIStoryboard(name: "NCViewerRichdocument", bundle: nil).instantiateInitialViewController() as! NCViewerRichdocument
  124. viewController.metadata = metadata
  125. viewController.link = metadata.url
  126. navigationController.pushViewController(viewController, animated: true)
  127. }
  128. }
  129. return
  130. }
  131. }
  132. // OTHER
  133. let fileNamePath = NSTemporaryDirectory() + metadata.fileNameView
  134. CCUtility.copyFile(atPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView), toPath: fileNamePath)
  135. viewerQuickLook = NCViewerQuickLook.init()
  136. viewerQuickLook?.quickLook(url: URL(fileURLWithPath: fileNamePath))
  137. }
  138. }
  139. //MARK: - SELECT
  140. extension NCViewer: NCSelectDelegate {
  141. func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String, items: [Any], buttonType: String, overwrite: Bool) {
  142. if let serverUrl = serverUrl {
  143. let metadata = items[0] as! tableMetadata
  144. if buttonType == "done" {
  145. NCNetworking.shared.moveMetadata(metadata, serverUrlTo: serverUrl, overwrite: overwrite) { (errorCode, errorDescription) in
  146. if errorCode != 0 {
  147. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCBrandGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  148. }
  149. }
  150. } else {
  151. NCNetworking.shared.copyMetadata(metadata, serverUrlTo: serverUrl, overwrite: overwrite) { (errorCode, errorDescription) in
  152. if errorCode != 0 {
  153. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCBrandGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  154. }
  155. }
  156. }
  157. }
  158. }
  159. }