|
| 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 | + |
| 24 | +import Foundation |
| 25 | +import UIKit |
| 26 | +import NextcloudKit |
| 27 | + |
| 28 | +class NCCreateDocument: NSObject { |
| 29 | + let utility = NCUtility() |
| 30 | + let database = NCManageDatabase.shared |
| 31 | + let global = NCGlobal.shared |
| 32 | +// var fileNameExtension = "" |
| 33 | + |
| 34 | + @MainActor |
| 35 | + func createDocument(controller: NCMainTabBarController, fileNamePath: String, fileName: String, fileNameExtension: String = "", editorId: String, creatorId: String? = nil, templateId: String, account: String) async { |
| 36 | + let session = NCSession.shared.getSession(account: account) |
| 37 | + guard let viewController = controller.currentViewController() else { |
| 38 | + return |
| 39 | + } |
| 40 | + var UUID = NSUUID().uuidString |
| 41 | + UUID = "TEMP" + UUID.replacingOccurrences(of: "-", with: "") |
| 42 | + var options = NKRequestOptions() |
| 43 | + let serverUrl = controller.currentServerUrl() |
| 44 | + |
| 45 | + if let creatorId, editorId == "text" || editorId == "onlyoffice" { |
| 46 | + if editorId == "onlyoffice" { |
| 47 | + options = NKRequestOptions(customUserAgent: NCUtility().getCustomUserAgentOnlyOffice()) |
| 48 | + } else if editorId == "text" { |
| 49 | + options = NKRequestOptions(customUserAgent: NCUtility().getCustomUserAgentNCText()) |
| 50 | + } |
| 51 | + let results = await NextcloudKit.shared.textCreateFileAsync(fileNamePath: fileNamePath, editorId: editorId, creatorId: creatorId, templateId: templateId, account: account, options: options) { task in |
| 52 | + Task { |
| 53 | + let identifier = await NCNetworking.shared.networkingTasks.createIdentifier(account: account, |
| 54 | + path: fileNamePath, |
| 55 | + name: "textCreateFile") |
| 56 | + await NCNetworking.shared.networkingTasks.track(identifier: identifier, task: task) |
| 57 | + } |
| 58 | + } |
| 59 | + guard results.error == .success, let url = results.url else { |
| 60 | + return NCContentPresenter().showError(error: results.error) |
| 61 | + } |
| 62 | + |
| 63 | + var result = await NKTypeIdentifiers.shared.getInternalType(fileName: fileName, mimeType: "", directory: false, account: session.account) |
| 64 | + |
| 65 | + controller.dismiss(animated: true, completion: { |
| 66 | + |
| 67 | + Task { |
| 68 | + let metadata = await NCManageDatabaseCreateMetadata().createMetadataAsync( |
| 69 | + fileName: fileName, |
| 70 | + ocId: UUID, |
| 71 | + serverUrl: serverUrl, |
| 72 | + url: url, |
| 73 | + session: session, |
| 74 | + sceneIdentifier: controller.sceneIdentifier) |
| 75 | + if let vc = await NCViewer().getViewerController(metadata: metadata, delegate: viewController) { |
| 76 | + viewController.navigationController?.pushViewController(vc, animated: true) |
| 77 | + } |
| 78 | + } |
| 79 | + }) |
| 80 | + |
| 81 | + } else if editorId == "collabora" { |
| 82 | + |
| 83 | + let results = await NextcloudKit.shared.createRichdocumentsAsync(path: fileNamePath, templateId: templateId, account: account) { task in |
| 84 | + Task { |
| 85 | + let identifier = await NCNetworking.shared.networkingTasks.createIdentifier(account: account, |
| 86 | + path: fileNamePath, |
| 87 | + name: "CreateRichdocuments") |
| 88 | + await NCNetworking.shared.networkingTasks.track(identifier: identifier, task: task) |
| 89 | + } |
| 90 | + } |
| 91 | + guard results.error == .success, let url = results.url else { |
| 92 | + return NCContentPresenter().showError(error: results.error) |
| 93 | + } |
| 94 | + |
| 95 | + controller.dismiss(animated: true, completion: { |
| 96 | + |
| 97 | + Task { |
| 98 | + let createFileName = (fileName as NSString).deletingPathExtension + "." + fileNameExtension |
| 99 | + let metadata = await NCManageDatabaseCreateMetadata().createMetadataAsync( |
| 100 | + fileName: createFileName, |
| 101 | + ocId: UUID, |
| 102 | + serverUrl: serverUrl, |
| 103 | + url: url, |
| 104 | + session: session, |
| 105 | + sceneIdentifier: controller.sceneIdentifier) |
| 106 | + AnalyticsHelper.shared.trackCreateFile(metadata: metadata) |
| 107 | + if let vc = await NCViewer().getViewerController(metadata: metadata, delegate: viewController) { |
| 108 | + viewController.navigationController?.pushViewController(vc, animated: true) |
| 109 | + } |
| 110 | + } |
| 111 | + }) |
| 112 | + |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + func getTemplate(editorId: String, templateId: String, account: String) async -> (templates: [NKEditorTemplate], selectedTemplate: NKEditorTemplate, ext: String) { |
| 117 | + var templates: [NKEditorTemplate] = [] |
| 118 | + var selectedTemplate = NKEditorTemplate() |
| 119 | + var ext: String = "" |
| 120 | + |
| 121 | + if editorId == "text" || editorId == "onlyoffice" { |
| 122 | + var options = NKRequestOptions() |
| 123 | + if editorId == "onlyoffice" { |
| 124 | + options = NKRequestOptions(customUserAgent: NCUtility().getCustomUserAgentOnlyOffice()) |
| 125 | + } else if editorId == "text" { |
| 126 | + options = NKRequestOptions(customUserAgent: NCUtility().getCustomUserAgentNCText()) |
| 127 | + } |
| 128 | + |
| 129 | + let results = await NextcloudKit.shared.textGetListOfTemplatesAsync(account: account, options: options) { task in |
| 130 | + Task { |
| 131 | + let identifier = await NCNetworking.shared.networkingTasks.createIdentifier(account: account, |
| 132 | + name: "textGetListOfTemplates") |
| 133 | + await NCNetworking.shared.networkingTasks.track(identifier: identifier, task: task) |
| 134 | + } |
| 135 | + } |
| 136 | + if results.error == .success, let resultTemplates = results.templates { |
| 137 | + for template in resultTemplates { |
| 138 | + var temp = NKEditorTemplate() |
| 139 | + temp.identifier = template.identifier |
| 140 | + temp.ext = template.ext |
| 141 | + temp.name = template.name |
| 142 | + temp.preview = template.preview |
| 143 | + templates.append(temp) |
| 144 | + // default: template empty |
| 145 | + if temp.preview.isEmpty { |
| 146 | + selectedTemplate = temp |
| 147 | + ext = template.ext |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + if templates.isEmpty { |
| 153 | + var temp = NKEditorTemplate() |
| 154 | + temp.identifier = "" |
| 155 | + if editorId == "text" { |
| 156 | + temp.ext = "md" |
| 157 | + } else if editorId == "onlyoffice" && templateId == "document" { |
| 158 | + temp.ext = "docx" |
| 159 | + } else if editorId == "onlyoffice" && templateId == "spreadsheet" { |
| 160 | + temp.ext = "xlsx" |
| 161 | + } else if editorId == "onlyoffice" && templateId == "presentation" { |
| 162 | + temp.ext = "pptx" |
| 163 | + } |
| 164 | + temp.name = "Empty" |
| 165 | + temp.preview = "" |
| 166 | + templates.append(temp) |
| 167 | + selectedTemplate = temp |
| 168 | + ext = temp.ext |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + if editorId == "collabora" { |
| 173 | + let results = await NextcloudKit.shared.getTemplatesRichdocumentsAsync(typeTemplate: templateId, account: account) { task in |
| 174 | + Task { |
| 175 | + let identifier = await NCNetworking.shared.networkingTasks.createIdentifier(account: account, |
| 176 | + path: templateId, |
| 177 | + name: "getTemplatesRichdocuments") |
| 178 | + await NCNetworking.shared.networkingTasks.track(identifier: identifier, task: task) |
| 179 | + } |
| 180 | + } |
| 181 | + if results.error == .success { |
| 182 | + for template in results.templates! { |
| 183 | + var temp = NKEditorTemplate() |
| 184 | + temp.identifier = "\(template.templateId)" |
| 185 | + temp.ext = template.ext |
| 186 | + temp.name = template.name |
| 187 | + temp.preview = template.preview |
| 188 | + templates.append(temp) |
| 189 | + // default: template empty |
| 190 | + if temp.preview.isEmpty { |
| 191 | + selectedTemplate = temp |
| 192 | + ext = temp.ext |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | + |
| 198 | + return (templates, selectedTemplate, ext) |
| 199 | + } |
| 200 | +} |
0 commit comments