mirror of
https://github.com/nextcloud/richdocuments.git
synced 2025-12-17 21:12:14 +01:00
118 lines
2.9 KiB
PHP
118 lines
2.9 KiB
PHP
<?php
|
|
/**
|
|
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace OCA\Richdocuments\TaskProcessing;
|
|
|
|
use OCA\Richdocuments\AppInfo\Application;
|
|
use OCA\Richdocuments\Service\DocumentGenerationService;
|
|
use OCP\IL10N;
|
|
use OCP\TaskProcessing\EShapeType;
|
|
use OCP\TaskProcessing\ISynchronousWatermarkingProvider;
|
|
use OCP\TaskProcessing\ShapeDescriptor;
|
|
use OCP\TaskProcessing\ShapeEnumValue;
|
|
|
|
class TextToDocumentProvider implements ISynchronousWatermarkingProvider {
|
|
public const DEFAULT_TARGET_FORMAT = 'docx';
|
|
|
|
public function __construct(
|
|
private DocumentGenerationService $documentGenerationService,
|
|
private IL10N $l,
|
|
) {
|
|
}
|
|
|
|
public function getId(): string {
|
|
return Application::APPNAME . '-text_document_generator';
|
|
}
|
|
|
|
public function getName(): string {
|
|
return $this->l->t('Nextcloud Office text document generator');
|
|
}
|
|
|
|
public function getTaskTypeId(): string {
|
|
return TextToDocumentTaskType::ID;
|
|
}
|
|
|
|
public function getExpectedRuntime(): int {
|
|
return 120;
|
|
}
|
|
|
|
public function getInputShapeEnumValues(): array {
|
|
return [];
|
|
}
|
|
|
|
public function getInputShapeDefaults(): array {
|
|
return [];
|
|
}
|
|
|
|
public function getOptionalInputShape(): array {
|
|
return [
|
|
'target_format' => new ShapeDescriptor(
|
|
$this->l->t('Document format'),
|
|
$this->l->t('The format of the generated document'),
|
|
EShapeType::Enum
|
|
),
|
|
];
|
|
}
|
|
|
|
public function getOptionalInputShapeEnumValues(): array {
|
|
return [
|
|
'target_format' => [
|
|
new ShapeEnumValue($this->l->t('OpenXML (docx)'), 'docx'),
|
|
new ShapeEnumValue($this->l->t('OpenDocument (odt)'), 'odt'),
|
|
new ShapeEnumValue($this->l->t('Portable Document Format (pdf)'), 'pdf'),
|
|
],
|
|
];
|
|
}
|
|
|
|
public function getOptionalInputShapeDefaults(): array {
|
|
return [
|
|
'target_format' => self::DEFAULT_TARGET_FORMAT,
|
|
];
|
|
}
|
|
|
|
public function getOutputShapeEnumValues(): array {
|
|
return [];
|
|
}
|
|
|
|
public function getOptionalOutputShape(): array {
|
|
return [];
|
|
}
|
|
|
|
public function getOptionalOutputShapeEnumValues(): array {
|
|
return [];
|
|
}
|
|
/**
|
|
* @inheritDoc
|
|
*/
|
|
public function process(?string $userId, array $input, callable $reportProgress, bool $includeWatermark = true): array {
|
|
if ($userId === null) {
|
|
throw new \RuntimeException('User ID is required to process the prompt.');
|
|
}
|
|
|
|
if (!isset($input['text']) || !is_string($input['text'])) {
|
|
throw new \RuntimeException('Invalid input, expected "text" key with string value');
|
|
}
|
|
|
|
$targetFormat = self::DEFAULT_TARGET_FORMAT;
|
|
if (isset($input['target_format']) && is_string($input['target_format']) && in_array($input['target_format'], ['docx', 'odt', 'pdf'], true)) {
|
|
$targetFormat = $input['target_format'];
|
|
}
|
|
|
|
|
|
$fileContent = $this->documentGenerationService->generateTextDocument(
|
|
$userId,
|
|
$input['text'],
|
|
$targetFormat,
|
|
$includeWatermark
|
|
);
|
|
|
|
return [
|
|
'file' => $fileContent,
|
|
];
|
|
}
|
|
}
|