NCCreateDocument.swift 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //
  2. // NCCreateDocument.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 22/06/24.
  6. // Copyright © 2024 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 UIKit
  25. import NextcloudKit
  26. import Alamofire
  27. class NCCreateDocument: NSObject {
  28. let utility = NCUtility()
  29. let database = NCManageDatabase.shared
  30. func createDocument(controller: NCMainTabBarController, fileNamePath: String, fileName: String, editorId: String, creatorId: String? = nil, templateId: String, account: String) {
  31. let session = NCSession.shared.getSession(account: account)
  32. guard let viewController = controller.currentViewController() else { return }
  33. var UUID = NSUUID().uuidString
  34. UUID = "TEMP" + UUID.replacingOccurrences(of: "-", with: "")
  35. var options = NKRequestOptions()
  36. let serverUrl = controller.currentServerUrl()
  37. if let creatorId, (editorId == NCGlobal.shared.editorText || editorId == NCGlobal.shared.editorOnlyoffice) {
  38. if editorId == NCGlobal.shared.editorOnlyoffice {
  39. options = NKRequestOptions(customUserAgent: NCUtility().getCustomUserAgentOnlyOffice())
  40. } else if editorId == NCGlobal.shared.editorText {
  41. options = NKRequestOptions(customUserAgent: NCUtility().getCustomUserAgentNCText())
  42. }
  43. NextcloudKit.shared.NCTextCreateFile(fileNamePath: fileNamePath, editorId: editorId, creatorId: creatorId, templateId: templateId, account: account, options: options) { returnedAccount, url, _, error in
  44. guard error == .success, let url else {
  45. return NCContentPresenter().showError(error: error)
  46. }
  47. if account == returnedAccount {
  48. let contentType = NextcloudKit.shared.nkCommonInstance.getInternalType(fileName: fileName, mimeType: "", directory: false, account: session.account).mimeType
  49. let metadata = self.database.createMetadata(fileName: fileName,
  50. fileNameView: fileName,
  51. ocId: UUID,
  52. serverUrl: serverUrl,
  53. url: url,
  54. contentType: contentType,
  55. session: session,
  56. sceneIdentifier: controller.sceneIdentifier)
  57. NCViewer().view(viewController: viewController, metadata: metadata)
  58. }
  59. }
  60. } else if editorId == NCGlobal.shared.editorCollabora {
  61. NextcloudKit.shared.createRichdocuments(path: fileNamePath, templateId: templateId, account: account) { returnedAccount, url, _, error in
  62. guard error == .success, let url else {
  63. return NCContentPresenter().showError(error: error)
  64. }
  65. if account == returnedAccount {
  66. let contentType = NextcloudKit.shared.nkCommonInstance.getInternalType(fileName: fileName, mimeType: "", directory: false, account: session.account).mimeType
  67. let metadata = self.database.createMetadata(fileName: fileName,
  68. fileNameView: fileName,
  69. ocId: UUID,
  70. serverUrl: serverUrl,
  71. url: url,
  72. contentType: contentType,
  73. session: session,
  74. sceneIdentifier: controller.sceneIdentifier)
  75. NCViewer().view(viewController: viewController, metadata: metadata)
  76. }
  77. }
  78. }
  79. }
  80. func getTemplate(editorId: String, templateId: String, account: String) async -> (templates: [NKEditorTemplates], selectedTemplate: NKEditorTemplates, ext: String) {
  81. var templates: [NKEditorTemplates] = []
  82. var selectedTemplate = NKEditorTemplates()
  83. var ext: String = ""
  84. if editorId == NCGlobal.shared.editorText || editorId == NCGlobal.shared.editorOnlyoffice {
  85. var options = NKRequestOptions()
  86. if editorId == NCGlobal.shared.editorOnlyoffice {
  87. options = NKRequestOptions(customUserAgent: NCUtility().getCustomUserAgentOnlyOffice())
  88. } else if editorId == NCGlobal.shared.editorText {
  89. options = NKRequestOptions(customUserAgent: NCUtility().getCustomUserAgentNCText())
  90. }
  91. let results = await textGetListOfTemplates(account: account, options: options)
  92. if results.error == .success, let resultTemplates = results.templates {
  93. for template in resultTemplates {
  94. let temp = NKEditorTemplates()
  95. temp.identifier = template.identifier
  96. temp.ext = template.ext
  97. temp.name = template.name
  98. temp.preview = template.preview
  99. templates.append(temp)
  100. // default: template empty
  101. if temp.preview.isEmpty {
  102. selectedTemplate = temp
  103. ext = template.ext
  104. }
  105. }
  106. }
  107. if templates.isEmpty {
  108. let temp = NKEditorTemplates()
  109. temp.identifier = ""
  110. if editorId == NCGlobal.shared.editorText {
  111. temp.ext = "md"
  112. } else if editorId == NCGlobal.shared.editorOnlyoffice && templateId == NCGlobal.shared.templateDocument {
  113. temp.ext = "docx"
  114. } else if editorId == NCGlobal.shared.editorOnlyoffice && templateId == NCGlobal.shared.templateSpreadsheet {
  115. temp.ext = "xlsx"
  116. } else if editorId == NCGlobal.shared.editorOnlyoffice && templateId == NCGlobal.shared.templatePresentation {
  117. temp.ext = "pptx"
  118. }
  119. temp.name = "Empty"
  120. temp.preview = ""
  121. templates.append(temp)
  122. selectedTemplate = temp
  123. ext = temp.ext
  124. }
  125. }
  126. if editorId == NCGlobal.shared.editorCollabora {
  127. let results = await getTemplatesRichdocuments(typeTemplate: templateId, account: account)
  128. if results.error == .success {
  129. for template in results.templates! {
  130. let temp = NKEditorTemplates()
  131. temp.identifier = "\(template.templateId)"
  132. temp.delete = template.delete
  133. temp.ext = template.ext
  134. temp.name = template.name
  135. temp.preview = template.preview
  136. temp.type = template.type
  137. templates.append(temp)
  138. // default: template empty
  139. if temp.preview.isEmpty {
  140. selectedTemplate = temp
  141. ext = temp.ext
  142. }
  143. }
  144. }
  145. }
  146. return (templates, selectedTemplate, ext)
  147. }
  148. // MARK: - NextcloudKit async/await
  149. func textGetListOfTemplates(account: String, options: NKRequestOptions = NKRequestOptions()) async -> (account: String, templates: [NKEditorTemplates]?, responseData: AFDataResponse<Data>?, error: NKError) {
  150. await withUnsafeContinuation({ continuation in
  151. NextcloudKit.shared.NCTextGetListOfTemplates(account: account) { account, templates, responseData, error in
  152. continuation.resume(returning: (account: account, templates: templates, responseData: responseData, error: error))
  153. }
  154. })
  155. }
  156. func getTemplatesRichdocuments(typeTemplate: String, account: String, options: NKRequestOptions = NKRequestOptions()) async -> (account: String, templates: [NKRichdocumentsTemplate]?, responseData: AFDataResponse<Data>?, error: NKError) {
  157. await withUnsafeContinuation({ continuation in
  158. NextcloudKit.shared.getTemplatesRichdocuments(typeTemplate: typeTemplate, account: account, options: options) { account, templates, responseData, error in
  159. continuation.resume(returning: (account: account, templates: templates, responseData: responseData, error: error))
  160. }
  161. })
  162. }
  163. }