NCCreateDocument.swift 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. class NCCreateDocument: NSObject {
  27. let appDelegate = (UIApplication.shared.delegate as? AppDelegate)!
  28. let utility = NCUtility()
  29. func createDocument(controller: NCMainTabBarController, fileNamePath: String, fileName: String, editorId: String, creatorId: String? = nil, templateId: String) {
  30. guard let viewController = controller.currentViewController() else { return }
  31. var UUID = NSUUID().uuidString
  32. UUID = "TEMP" + UUID.replacingOccurrences(of: "-", with: "")
  33. var options = NKRequestOptions()
  34. let serverUrl = controller.currentServerUrl()
  35. if let creatorId, (editorId == NCGlobal.shared.editorText || editorId == NCGlobal.shared.editorOnlyoffice) {
  36. if editorId == NCGlobal.shared.editorOnlyoffice {
  37. options = NKRequestOptions(customUserAgent: NCUtility().getCustomUserAgentOnlyOffice())
  38. } else if editorId == NCGlobal.shared.editorText {
  39. options = NKRequestOptions(customUserAgent: NCUtility().getCustomUserAgentNCText())
  40. }
  41. NextcloudKit.shared.NCTextCreateFile(fileNamePath: fileNamePath, editorId: editorId, creatorId: creatorId, templateId: templateId, account: appDelegate.account, options: options) { account, url, _, error in
  42. guard error == .success, account == self.appDelegate.account, let url = url else {
  43. NCContentPresenter().showError(error: error)
  44. return
  45. }
  46. let contentType = NextcloudKit.shared.nkCommonInstance.getInternalType(fileName: fileName, mimeType: "", directory: false).mimeType
  47. let metadata = NCManageDatabase.shared.createMetadata(account: self.appDelegate.account, user: self.appDelegate.user, userId: self.appDelegate.userId, fileName: fileName, fileNameView: fileName, ocId: UUID, serverUrl: serverUrl, urlBase: self.appDelegate.urlBase, url: url, contentType: contentType)
  48. NCViewer().view(viewController: viewController, metadata: metadata, metadatas: [metadata], imageIcon: nil)
  49. }
  50. } else if editorId == NCGlobal.shared.editorCollabora {
  51. NextcloudKit.shared.createRichdocuments(path: fileNamePath, templateId: templateId, account: appDelegate.account) { account, url, _, error in
  52. guard error == .success, account == self.appDelegate.account, let url = url else {
  53. NCContentPresenter().showError(error: error)
  54. return
  55. }
  56. let contentType = NextcloudKit.shared.nkCommonInstance.getInternalType(fileName: fileName, mimeType: "", directory: false).mimeType
  57. let metadata = NCManageDatabase.shared.createMetadata(account: self.appDelegate.account, user: self.appDelegate.user, userId: self.appDelegate.userId, fileName: fileName, fileNameView: fileName, ocId: UUID, serverUrl: serverUrl, urlBase: self.appDelegate.urlBase, url: url, contentType: contentType)
  58. NCViewer().view(viewController: viewController, metadata: metadata, metadatas: [metadata], imageIcon: nil)
  59. }
  60. }
  61. }
  62. func getTemplate(editorId: String, templateId: String) async -> (templates: [NKEditorTemplates], selectedTemplate: NKEditorTemplates, ext: String) {
  63. var templates: [NKEditorTemplates] = []
  64. var selectedTemplate = NKEditorTemplates()
  65. var ext: String = ""
  66. if editorId == NCGlobal.shared.editorText || editorId == NCGlobal.shared.editorOnlyoffice {
  67. var options = NKRequestOptions()
  68. if editorId == NCGlobal.shared.editorOnlyoffice {
  69. options = NKRequestOptions(customUserAgent: NCUtility().getCustomUserAgentOnlyOffice())
  70. } else if editorId == NCGlobal.shared.editorText {
  71. options = NKRequestOptions(customUserAgent: NCUtility().getCustomUserAgentNCText())
  72. }
  73. let results = await textGetListOfTemplates(account:appDelegate.account, options: options)
  74. if results.error == .success {
  75. for template in results.templates {
  76. let temp = NKEditorTemplates()
  77. temp.identifier = template.identifier
  78. temp.ext = template.ext
  79. temp.name = template.name
  80. temp.preview = template.preview
  81. templates.append(temp)
  82. // default: template empty
  83. if temp.preview.isEmpty {
  84. selectedTemplate = temp
  85. ext = template.ext
  86. }
  87. }
  88. }
  89. if templates.isEmpty {
  90. let temp = NKEditorTemplates()
  91. temp.identifier = ""
  92. if editorId == NCGlobal.shared.editorText {
  93. temp.ext = "md"
  94. } else if editorId == NCGlobal.shared.editorOnlyoffice && templateId == NCGlobal.shared.templateDocument {
  95. temp.ext = "docx"
  96. } else if editorId == NCGlobal.shared.editorOnlyoffice && templateId == NCGlobal.shared.templateSpreadsheet {
  97. temp.ext = "xlsx"
  98. } else if editorId == NCGlobal.shared.editorOnlyoffice && templateId == NCGlobal.shared.templatePresentation {
  99. temp.ext = "pptx"
  100. }
  101. temp.name = "Empty"
  102. temp.preview = ""
  103. templates.append(temp)
  104. selectedTemplate = temp
  105. ext = temp.ext
  106. }
  107. }
  108. if editorId == NCGlobal.shared.editorCollabora {
  109. let results = await getTemplatesRichdocuments(typeTemplate: templateId, account: appDelegate.account)
  110. if results.error == .success {
  111. for template in results.templates! {
  112. let temp = NKEditorTemplates()
  113. temp.identifier = "\(template.templateId)"
  114. temp.delete = template.delete
  115. temp.ext = template.ext
  116. temp.name = template.name
  117. temp.preview = template.preview
  118. temp.type = template.type
  119. templates.append(temp)
  120. // default: template empty
  121. if temp.preview.isEmpty {
  122. selectedTemplate = temp
  123. ext = temp.ext
  124. }
  125. }
  126. }
  127. }
  128. return (templates, selectedTemplate, ext)
  129. }
  130. // MARK: - NextcloudKit async/await
  131. func textGetListOfTemplates(account: String, options: NKRequestOptions = NKRequestOptions()) async -> (account: String, templates: [NKEditorTemplates], data: Data?, error: NKError) {
  132. await withUnsafeContinuation({ continuation in
  133. NextcloudKit.shared.NCTextGetListOfTemplates(account: account) { account, templates, data, error in
  134. continuation.resume(returning: (account: account, templates: templates, data: data, error: error))
  135. }
  136. })
  137. }
  138. func getTemplatesRichdocuments(typeTemplate: String, account: String, options: NKRequestOptions = NKRequestOptions()) async -> (account: String, templates: [NKRichdocumentsTemplate]?, data: Data?, error: NKError) {
  139. await withUnsafeContinuation({ continuation in
  140. NextcloudKit.shared.getTemplatesRichdocuments(typeTemplate: typeTemplate, account: account, options: options) { account, templates, data, error in
  141. continuation.resume(returning: (account: account, templates: templates, data: data, error: error))
  142. }
  143. })
  144. }
  145. }