From 0a5698e6d9825910ff9c743b698f0d48671f1166 Mon Sep 17 00:00:00 2001 From: Jaume Pujantell Date: Tue, 11 Feb 2025 09:54:34 +0100 Subject: [PATCH] templates: better support cross-format templates If form filling was used to create a file, do not add a template source. Otherwise Collabora Online creates the file again from the template. Use the format option in 'transform-document-structure' to avoid an intermediate save in the template format, which might not be supported as a save format. Signed-off-by: Jaume Pujantell --- lib/Listener/FileCreatedFromTemplateListener.php | 12 ++++++------ lib/Service/TemplateFieldService.php | 9 +-------- 2 files changed, 7 insertions(+), 14 deletions(-) diff --git a/lib/Listener/FileCreatedFromTemplateListener.php b/lib/Listener/FileCreatedFromTemplateListener.php index 8553a20b8a..746d479bb8 100644 --- a/lib/Listener/FileCreatedFromTemplateListener.php +++ b/lib/Listener/FileCreatedFromTemplateListener.php @@ -54,18 +54,18 @@ public function handle(Event $event): void { return; } - if ($this->templateManager->isSupportedTemplateSource($templateFile->getExtension())) { - // Only use TemplateSource if supported filetype - $this->templateManager->setTemplateSource($event->getTarget()->getId(), $templateFile->getId()); - } - if ($this->capabilitiesService->hasFormFilling()) { try { - $filledTemplate = $this->templateFieldService->fillFields($templateFile, $event->getTemplateFields()); + $filledTemplate = $this->templateFieldService->fillFields($templateFile, $event->getTemplateFields(), null, $event->getTarget()->getExtension()); $event->getTarget()->putContent($filledTemplate); } catch (\Exception $e) { $this->logger->error($e->getMessage(), ['exception' => $e]); } + } else { + if ($this->templateManager->isSupportedTemplateSource($templateFile->getExtension())) { + // Only use TemplateSource if supported filetype + $this->templateManager->setTemplateSource($event->getTarget()->getId(), $templateFile->getId()); + } } // Avoid having the mimetype of the source file set diff --git a/lib/Service/TemplateFieldService.php b/lib/Service/TemplateFieldService.php index 917a23a6fc..d4fd1faf5f 100644 --- a/lib/Service/TemplateFieldService.php +++ b/lib/Service/TemplateFieldService.php @@ -177,7 +177,7 @@ public function fillFields(Node|int $file, array $fields = [], ?string $destinat $formFormat = [ 'name' => 'format', - 'contents' => $file->getExtension(), + 'contents' => $format === null ? $file->getExtension() : $format, ]; $form = RemoteOptionsService::getDefaultOptions(); @@ -191,13 +191,6 @@ public function fillFields(Node|int $file, array $fields = [], ?string $destinat $content = $response->getBody(); - if ($format !== null) { - $tmp = $this->tempManager->getTemporaryFile(); - file_put_contents($tmp, $content); - $fp = fopen($tmp, 'rb'); - $content = $this->remoteService->convertTo($file->getName(), $fp, $format); - } - if ($destination !== null) { $this->writeToDestination($destination, $content); }