Skip to content

Commit

Permalink
fix: review comments
Browse files Browse the repository at this point in the history
Signed-off-by: Luka Trovic <[email protected]>
  • Loading branch information
luka-nextcloud committed Jul 31, 2024
1 parent c01c855 commit 88eb9d7
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 36 deletions.
2 changes: 1 addition & 1 deletion appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,6 @@
['name' => 'Target#getPreview', 'url' => '/api/v1/targets/preview', 'verb' => 'GET'],

['name' => 'TemplateField#extractFields', 'url' => '/api/v1/template/fields/extract/{fileId}', 'verb' => 'GET'],
['name' => 'TemplateField#fillFields', 'url' => '/api/v1/template/fields/fill', 'verb' => 'POST'],
['name' => 'TemplateField#fillFields', 'url' => '/api/v1/template/fields/fill/{fileId}', 'verb' => 'POST'],
],
];
10 changes: 5 additions & 5 deletions composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,27 @@
class ComposerStaticInitRichdocuments
{
public static $prefixLengthsPsr4 = array (
'm' =>
'm' =>
array (
'mikehaertl\\shellcommand\\' => 24,
'mikehaertl\\pdftk\\' => 17,
),
'O' =>
'O' =>
array (
'OCA\\Richdocuments\\' => 18,
),
);

public static $prefixDirsPsr4 = array (
'mikehaertl\\shellcommand\\' =>
'mikehaertl\\shellcommand\\' =>
array (
0 => __DIR__ . '/..' . '/../vendor/mikehaertl/php-shellcommand/src',
),
'mikehaertl\\pdftk\\' =>
'mikehaertl\\pdftk\\' =>
array (
0 => __DIR__ . '/..' . '/../vendor/mikehaertl/php-pdftk/src',
),
'OCA\\Richdocuments\\' =>
'OCA\\Richdocuments\\' =>
array (
0 => __DIR__ . '/..' . '/../lib',
),
Expand Down
2 changes: 1 addition & 1 deletion lib/Controller/TemplateFieldController.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function extractFields(int $fileId): DataResponse {
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);
Expand Down
30 changes: 18 additions & 12 deletions lib/Service/PdfService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@

use mikehaertl\pdftk\Pdf;
use OCP\Files\Node;
use OCP\IConfig;
use OCP\Files\Template\Field;
use OCP\Files\Template\FieldType;
use Psr\Log\LoggerInterface;

class PdfService {
public function __construct(
private IConfig $config,
private LoggerInterface $logger
) {
}
Expand All @@ -26,32 +26,38 @@ public function extractFields(Node $file): array {
$pdf = new Pdf($filePath);
$fields = $pdf->getDataFields();
$templateFields = [];
$index = 0;
foreach ($fields as $field) {
$templateFields[] = [
'name' => $field['FieldName'],
'type' => $field['FieldType'],
'default' => $field['FieldValueDefault'] ?? '',
'options' => $field['FieldStateOption'] ?? [],
];
$fieldType = FieldType::tryFrom($field['FieldType']) ?? null;

if ($fieldType === null) {
continue;
}

$templateFields[] = new Field(
$index,

Check failure on line 38 in lib/Service/PdfService.php

View workflow job for this annotation

GitHub Actions / static-psalm-analysis

InvalidScalarArgument

lib/Service/PdfService.php:38:6: InvalidScalarArgument: Argument 1 of OCP\Files\Template\Field::__construct expects string, but int<0, max> provided (see https://psalm.dev/012)
$field['FieldName'],
$fieldType,
);
$index++;
}
return $templateFields;
} catch (\Exception $e) {
$this->logger->debug('Failed to extract fields from PDF: {error}', ['error' => $e->getMessage()]);
$this->logger->error('Failed to extract fields from PDF: {error}', ['error' => $e->getMessage(), 'exception' => $e]);
return [];
}
}

public function fillFields(Node $file, array $fieldValues): void {
$dataDir = $this->config->getSystemValue('datadirectory');
$filePath = $dataDir . $file->getPath();
$filePath = $file->getStorage()->getLocalFile($file->getInternalPath());

try {
$pdf = new Pdf($filePath);
$pdf->fillForm($fieldValues);
$pdf->flatten();
$pdf->saveAs($filePath);
} catch (\Exception $e) {
$this->logger->debug('Failed to fill fields in PDF: {error}', ['error' => $e->getMessage()]);
$this->logger->error('Failed to fill fields in PDF: {error}', ['error' => $e->getMessage(), 'exception' => $e]);
throw $e;
}
}
Expand Down
32 changes: 15 additions & 17 deletions lib/Service/TemplateFieldService.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,21 @@ public function __construct(
private AppConfig $appConfig,
private IRootFolder $rootFolder,
private LoggerInterface $logger,
private PdfService $pdfService
private PdfService $pdfService,
) {
}

public function extractFields(Node $file): ?array {
if ($file->getMimeType() === 'application/pdf') {
return $this->pdfService->extractFields($file);
/**
* @param Node|int $file
* @return array|string
*/
public function extractFields(Node|int $file) {
if (is_int($file)) {
$file = $this->rootFolder->getFirstNodeById($file);
}

// TODO: Won't work until Collabora's endpoint is ready
$collaboraUrl = $this->appConfig->getCollaboraUrlInternal();
$httpClient = $this->clientService->newClient();

try {
$response = $httpClient->get(
$collaboraUrl . "/cool/extract-doc-structure",
['query' => ['limit' => 'content-control']]
);

return [$response->getBody()];
} catch (\Exception $e) {
return null;
if ($file->getMimeType() === 'application/pdf') {
return $this->pdfService->extractFields($file);
}

$collaboraUrl = $this->appConfig->getCollaboraUrlInternal();
Expand Down Expand Up @@ -100,6 +93,11 @@ public function fillFields(Node|int $file, array $fields = []) {
$file = $this->rootFolder->getFirstNodeById($file);
}

if ($file->getMimeType() === 'application/pdf') {
$this->pdfService->fillFields($file, $fields);
return '';
}

$collaboraUrl = $this->appConfig->getCollaboraUrlInternal();
$httpClient = $this->clientService->newClient();

Expand Down

0 comments on commit 88eb9d7

Please sign in to comment.