Skip to content

Commit

Permalink
templates: better support cross-format templates
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
Jaume Pujantell committed Feb 13, 2025
1 parent f002ffc commit 6f42253
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
12 changes: 6 additions & 6 deletions lib/Listener/FileCreatedFromTemplateListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 1 addition & 8 deletions lib/Service/TemplateFieldService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);
}
Expand Down

0 comments on commit 6f42253

Please sign in to comment.