-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3798 from nextcloud/feat/pdf-template-workflow
feat: Template fields workflow
- Loading branch information
Showing
10 changed files
with
261 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Richdocuments\Controller; | ||
|
||
use OCA\Richdocuments\Service\TemplateFieldService; | ||
use OCA\Richdocuments\TemplateManager; | ||
use OCP\AppFramework\Http; | ||
use OCP\AppFramework\Http\Attribute\NoAdminRequired; | ||
use OCP\AppFramework\Http\DataResponse; | ||
use OCP\AppFramework\OCSController; | ||
use OCP\IRequest; | ||
|
||
class TemplateFieldController extends OCSController { | ||
private TemplateFieldService $templateFieldService; | ||
private TemplateManager $templateManager; | ||
|
||
/** | ||
* Template fields controller | ||
* | ||
* @param string $appName, | ||
* @param IRequest $request, | ||
* @param TemplateFieldService $templateFieldService | ||
* @param TemplateManager $templateManager | ||
*/ | ||
public function __construct( | ||
string $appName, | ||
IRequest $request, | ||
TemplateFieldService $templateFieldService, | ||
TemplateManager $templateManager | ||
) { | ||
parent::__construct($appName, $request); | ||
|
||
$this->templateFieldService = $templateFieldService; | ||
$this->templateManager = $templateManager; | ||
} | ||
|
||
/** | ||
* @param int $fileId | ||
* @return DataResponse | ||
*/ | ||
#[NoAdminRequired] | ||
public function extractFields(int $fileId): DataResponse { | ||
try { | ||
$fields = $this->templateFieldService->extractFields($fileId); | ||
|
||
return new DataResponse($fields, Http::STATUS_OK); | ||
} catch (\Exception $e) { | ||
return new DataResponse(["Unable to extract fields from given file"], Http::STATUS_INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
|
||
/** | ||
* @param int $fileId | ||
* @param array $fields | ||
* @return DataResponse | ||
*/ | ||
#[NoAdminRequired] | ||
public function fillFields(int $fileId, array $fields): DataResponse { | ||
try { | ||
$this->templateFieldService->fillFields($fileId, $fields); | ||
|
||
return new DataResponse([], Http::STATUS_OK); | ||
} catch (\Exception $e) { | ||
return new DataResponse(["Unable to fill fields into the given file"], Http::STATUS_INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Richdocuments\Listener; | ||
|
||
use OCA\Richdocuments\Service\TemplateFieldService; | ||
use OCP\EventDispatcher\Event; | ||
use OCP\EventDispatcher\IEventListener; | ||
use OCP\Files\Template\BeforeGetTemplatesEvent; | ||
|
||
/** @template-implements IEventListener<BeforeGetTemplatesEvent|Event> */ | ||
class BeforeGetTemplatesListener implements IEventListener { | ||
public function __construct( | ||
private TemplateFieldService $templateFieldService | ||
) { | ||
} | ||
|
||
public function handle(Event $event): void { | ||
if (!$event instanceof BeforeGetTemplatesEvent) { | ||
return; | ||
} | ||
|
||
foreach($event->getTemplates() as $template) { | ||
$templateFileId = $template->jsonSerialize()["fileid"]; | ||
$fields = $this->templateFieldService->extractFields($templateFileId); | ||
|
||
$template->setFields($fields); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
|
||
/** | ||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||
*/ | ||
|
||
namespace OCA\Richdocuments\Service; | ||
|
||
use OCA\Richdocuments\AppConfig; | ||
use OCP\Files\IRootFolder; | ||
use OCP\Files\Node; | ||
use OCP\Files\Template\Field; | ||
use OCP\Files\Template\FieldType; | ||
use OCP\Http\Client\IClientService; | ||
use Psr\Log\LoggerInterface; | ||
|
||
class TemplateFieldService { | ||
public function __construct( | ||
private IClientService $clientService, | ||
private AppConfig $appConfig, | ||
private IRootFolder $rootFolder, | ||
private LoggerInterface $logger | ||
) { | ||
} | ||
|
||
/** | ||
* @param Node|int $file | ||
* @return array|string | ||
*/ | ||
public function extractFields(Node|int $file) { | ||
if (is_int($file)) { | ||
$file = $this->rootFolder->getFirstNodeById($file); | ||
} | ||
|
||
$collaboraUrl = $this->appConfig->getCollaboraUrlInternal(); | ||
$httpClient = $this->clientService->newClient(); | ||
|
||
$form = RemoteOptionsService::getDefaultOptions(); | ||
$form['query'] = ['limit' => 'content-control']; | ||
$form['multipart'] = [[ | ||
'name' => 'data', | ||
'contents' => $file->getStorage()->fopen($file->getInternalPath(), 'r'), | ||
'headers' => ['Content-Type' => 'multipart/form-data'], | ||
]]; | ||
|
||
try { | ||
$response = $httpClient->post( | ||
$collaboraUrl . "/cool/extract-document-structure", | ||
$form | ||
); | ||
|
||
$documentStructure = json_decode($response->getBody(), true)['DocStructure']; | ||
$fields = []; | ||
|
||
foreach ($documentStructure as $index => $attr) { | ||
$fieldType = FieldType::tryFrom($attr['type']) ?? null; | ||
if ($fieldType === null) { | ||
continue; | ||
} | ||
|
||
$fields[] = [ | ||
new Field( | ||
$index, | ||
$attr["content"], | ||
$fieldType, | ||
$attr["alias"], | ||
$attr["id"], | ||
$attr["tag"] | ||
) | ||
]; | ||
} | ||
|
||
return array_merge([], ...$fields); | ||
} catch (\Exception $e) { | ||
$this->logger->error($e->getMessage()); | ||
return []; | ||
} | ||
} | ||
|
||
/** | ||
* @param Node|int $file | ||
* @param array $fields | ||
* @return string|resource | ||
*/ | ||
public function fillFields(Node|int $file, array $fields = []) { | ||
if (is_int($file)) { | ||
$file = $this->rootFolder->getFirstNodeById($file); | ||
} | ||
|
||
$collaboraUrl = $this->appConfig->getCollaboraUrlInternal(); | ||
$httpClient = $this->clientService->newClient(); | ||
|
||
$formData = [ | ||
'name' => 'data', | ||
'contents' => $file->getStorage()->fopen($file->getInternalPath(), 'r'), | ||
'headers' => ['Content-Type' => 'multipart/form-data'], | ||
]; | ||
|
||
$formTransform = [ | ||
'name' => 'transform', | ||
'contents' => '{"Transforms": ' . json_encode($fields) . '}', | ||
]; | ||
|
||
$formFormat = [ | ||
'name' => 'format', | ||
'contents' => $file->getExtension(), | ||
]; | ||
|
||
$form = RemoteOptionsService::getDefaultOptions(); | ||
$form['multipart'] = [$formData, $formTransform, $formFormat]; | ||
|
||
try { | ||
$response = $httpClient->post( | ||
$collaboraUrl . '/cool/transform-document-structure', | ||
$form | ||
); | ||
|
||
return $response->getBody(); | ||
} catch (\Exception $e) { | ||
$this->logger->error($e->getMessage()); | ||
throw $e; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters