NCViewer.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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?) {
  35. self.metadata = metadata
  36. self.metadatas = metadatas
  37. // IMAGE AUDIO VIDEO
  38. if metadata.typeFile == NCGlobal.shared.metadataTypeFileImage || metadata.typeFile == NCGlobal.shared.metadataTypeFileAudio || metadata.typeFile == NCGlobal.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 == NCGlobal.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. viewController.imageIcon = imageIcon
  62. navigationController.pushViewController(viewController, animated: true)
  63. }
  64. return
  65. }
  66. // DirectEditing: Nextcloud Text - OnlyOffice
  67. if NCUtility.shared.isDirectEditing(account: metadata.account, contentType: metadata.contentType) != nil && NCCommunication.shared.isNetworkReachable() {
  68. guard let editor = NCUtility.shared.isDirectEditing(account: metadata.account, contentType: metadata.contentType) else { return }
  69. if editor == NCGlobal.shared.editorText || editor == NCGlobal.shared.editorOnlyoffice {
  70. if metadata.url == "" {
  71. var customUserAgent: String?
  72. let fileNamePath = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, urlBase: metadata.urlBase, account: metadata.account)!
  73. if editor == NCGlobal.shared.editorOnlyoffice {
  74. customUserAgent = NCUtility.shared.getCustomUserAgentOnlyOffice()
  75. }
  76. NCUtility.shared.startActivityIndicator(backgroundView: viewController.view, blurEffect: true)
  77. NCCommunication.shared.NCTextOpenFile(fileNamePath: fileNamePath, editor: editor, customUserAgent: customUserAgent) { (account, url, errorCode, errorMessage) in
  78. NCUtility.shared.stopActivityIndicator()
  79. if errorCode == 0 && account == self.appDelegate.account && url != nil {
  80. if let navigationController = viewController.navigationController {
  81. let viewController:NCViewerNextcloudText = UIStoryboard(name: "NCViewerNextcloudText", bundle: nil).instantiateInitialViewController() as! NCViewerNextcloudText
  82. viewController.metadata = metadata
  83. viewController.editor = editor
  84. viewController.link = url!
  85. viewController.imageIcon = imageIcon
  86. navigationController.pushViewController(viewController, animated: true)
  87. }
  88. } else if errorCode != 0 {
  89. NCContentPresenter.shared.messageNotification("_error_", description: errorMessage, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  90. }
  91. }
  92. } else {
  93. if let navigationController = viewController.navigationController {
  94. let viewController:NCViewerNextcloudText = UIStoryboard(name: "NCViewerNextcloudText", bundle: nil).instantiateInitialViewController() as! NCViewerNextcloudText
  95. viewController.metadata = metadata
  96. viewController.editor = editor
  97. viewController.link = metadata.url
  98. viewController.imageIcon = imageIcon
  99. navigationController.pushViewController(viewController, animated: true)
  100. }
  101. }
  102. } else {
  103. NCContentPresenter.shared.messageNotification("_error_", description: "_editor_unknown_", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: NCGlobal.shared.errorInternalError)
  104. }
  105. return
  106. }
  107. // RichDocument: Collabora
  108. if NCUtility.shared.isRichDocument(metadata) && NCCommunication.shared.isNetworkReachable() {
  109. if metadata.url == "" {
  110. NCUtility.shared.startActivityIndicator(backgroundView: viewController.view, blurEffect: true)
  111. NCCommunication.shared.createUrlRichdocuments(fileID: metadata.fileId) { (account, url, errorCode, errorDescription) in
  112. NCUtility.shared.stopActivityIndicator()
  113. if errorCode == 0 && account == self.appDelegate.account && url != nil {
  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 = url!
  118. viewController.imageIcon = imageIcon
  119. navigationController.pushViewController(viewController, animated: true)
  120. }
  121. } else if errorCode != 0 {
  122. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  123. }
  124. }
  125. } else {
  126. if let navigationController = viewController.navigationController {
  127. let viewController:NCViewerRichdocument = UIStoryboard(name: "NCViewerRichdocument", bundle: nil).instantiateInitialViewController() as! NCViewerRichdocument
  128. viewController.metadata = metadata
  129. viewController.link = metadata.url
  130. viewController.imageIcon = imageIcon
  131. navigationController.pushViewController(viewController, animated: true)
  132. }
  133. }
  134. return
  135. }
  136. }
  137. // OTHER
  138. let fileNamePath = NSTemporaryDirectory() + metadata.fileNameView
  139. CCUtility.copyFile(atPath: CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView), toPath: fileNamePath)
  140. viewerQuickLook = NCViewerQuickLook.init()
  141. viewerQuickLook?.quickLook(url: URL(fileURLWithPath: fileNamePath))
  142. }
  143. }
  144. //MARK: - SELECT
  145. extension NCViewer: NCSelectDelegate {
  146. func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String, items: [Any], overwrite: Bool, copy: Bool, move: Bool) {
  147. if let serverUrl = serverUrl {
  148. let metadata = items[0] as! tableMetadata
  149. if move {
  150. NCNetworking.shared.moveMetadata(metadata, serverUrlTo: serverUrl, overwrite: overwrite) { (errorCode, errorDescription) in
  151. if errorCode != 0 {
  152. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  153. }
  154. }
  155. } else if copy {
  156. NCNetworking.shared.copyMetadata(metadata, serverUrlTo: serverUrl, overwrite: overwrite) { (errorCode, errorDescription) in
  157. if errorCode != 0 {
  158. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  159. }
  160. }
  161. }
  162. }
  163. }
  164. }