Skip to content

Commit 24a0b2e

Browse files
author
Jaume Pujantell
committed
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 <jaume.pujantell@collabora.com>
1 parent f002ffc commit 24a0b2e

File tree

2 files changed

+20
-27
lines changed

2 files changed

+20
-27
lines changed

lib/Listener/FileCreatedFromTemplateListener.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -41,31 +41,31 @@ public function handle(Event $event): void {
4141

4242
$templateFile = $event->getTemplate();
4343

44-
// Empty template
45-
if ($templateFile === null) {
46-
$event->getTarget()->putContent($this->templateManager->getEmptyFileContent($event->getTarget()->getExtension()));
47-
$templateType = $this->templateManager->getTemplateTypeForExtension($event->getTarget()->getExtension());
48-
$emptyTemplates = $this->templateManager->getEmpty($templateType);
49-
$emptyTemplate = array_shift($emptyTemplates);
50-
if ($emptyTemplate && $this->templateManager->isSupportedTemplateSource($emptyTemplate->getExtension())) {
51-
// Only use TemplateSource if supported filetype
52-
$this->templateManager->setTemplateSource($event->getTarget()->getId(), $emptyTemplate->getId());
53-
}
54-
return;
55-
}
56-
57-
if ($this->templateManager->isSupportedTemplateSource($templateFile->getExtension())) {
58-
// Only use TemplateSource if supported filetype
59-
$this->templateManager->setTemplateSource($event->getTarget()->getId(), $templateFile->getId());
60-
}
61-
6244
if ($this->capabilitiesService->hasFormFilling()) {
6345
try {
64-
$filledTemplate = $this->templateFieldService->fillFields($templateFile, $event->getTemplateFields());
46+
$filledTemplate = $this->templateFieldService->fillFields($templateFile, $event->getTemplateFields(), null, event->getTarget()->getExtension());
6547
$event->getTarget()->putContent($filledTemplate);
6648
} catch (\Exception $e) {
6749
$this->logger->error($e->getMessage(), ['exception' => $e]);
6850
}
51+
} else {
52+
// Empty template
53+
if ($templateFile === null) {
54+
$event->getTarget()->putContent($this->templateManager->getEmptyFileContent($event->getTarget()->getExtension()));
55+
$templateType = $this->templateManager->getTemplateTypeForExtension($event->getTarget()->getExtension());
56+
$emptyTemplates = $this->templateManager->getEmpty($templateType);
57+
$emptyTemplate = array_shift($emptyTemplates);
58+
if ($emptyTemplate && $this->templateManager->isSupportedTemplateSource($emptyTemplate->getExtension())) {
59+
// Only use TemplateSource if supported filetype
60+
$this->templateManager->setTemplateSource($event->getTarget()->getId(), $emptyTemplate->getId());
61+
}
62+
return;
63+
}
64+
65+
if ($this->templateManager->isSupportedTemplateSource($templateFile->getExtension())) {
66+
// Only use TemplateSource if supported filetype
67+
$this->templateManager->setTemplateSource($event->getTarget()->getId(), $templateFile->getId());
68+
}
6969
}
7070

7171
// Avoid having the mimetype of the source file set

lib/Service/TemplateFieldService.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ public function fillFields(Node|int $file, array $fields = [], ?string $destinat
177177

178178
$formFormat = [
179179
'name' => 'format',
180-
'contents' => $file->getExtension(),
180+
'contents' => $format === null ? $file->getExtension() : $format,
181181
];
182182

183183
$form = RemoteOptionsService::getDefaultOptions();
@@ -191,13 +191,6 @@ public function fillFields(Node|int $file, array $fields = [], ?string $destinat
191191

192192
$content = $response->getBody();
193193

194-
if ($format !== null) {
195-
$tmp = $this->tempManager->getTemporaryFile();
196-
file_put_contents($tmp, $content);
197-
$fp = fopen($tmp, 'rb');
198-
$content = $this->remoteService->convertTo($file->getName(), $fp, $format);
199-
}
200-
201194
if ($destination !== null) {
202195
$this->writeToDestination($destination, $content);
203196
}

0 commit comments

Comments
 (0)