From bec6232e3e93f76712fa61d286841440f563dab4 Mon Sep 17 00:00:00 2001 From: Vitor Mattos Date: Fri, 5 Apr 2024 11:48:14 -0300 Subject: [PATCH] chore: Change metadata field to be a json and not a text Signed-off-by: Vitor Mattos --- lib/Db/File.php | 19 ++---- .../Version8000Date20240405142042.php | 62 +++++++++++++++++++ lib/Service/FileElementService.php | 10 +-- lib/Service/FileService.php | 6 +- lib/Service/RequestSignatureService.php | 2 +- 5 files changed, 75 insertions(+), 24 deletions(-) create mode 100644 lib/Migration/Version8000Date20240405142042.php diff --git a/lib/Db/File.php b/lib/Db/File.php index dc44812ff1..9147b5abde 100644 --- a/lib/Db/File.php +++ b/lib/Db/File.php @@ -25,7 +25,7 @@ namespace OCA\Libresign\Db; use OCP\AppFramework\Db\Entity; -use stdClass; +use OCP\DB\Types; /** * @method void setId(int $id) @@ -46,7 +46,8 @@ * @method string getCallback() * @method void setStatus(int $status) * @method int getStatus() - * @method string getMetadata() + * @method void setMetadata(array $metadata) + * @method array getMetadata() */ class File extends Entity { /** @var integer */ @@ -95,18 +96,6 @@ public function __construct() { $this->addType('name', 'string'); $this->addType('callback', 'string'); $this->addType('status', 'integer'); - $this->addType('metadata', 'string'); - } - - public function setMetadata($metadata): void { - if (is_array($metadata)) { - $metadata = json_encode($metadata); - } - $this->metadata = (string) $metadata; - $this->markFieldUpdated('metadata'); - } - - public function getMetadataDecoded(): ?stdClass { - return json_decode($this->metadata); + $this->addType('metadata', Types::JSON); } } diff --git a/lib/Migration/Version8000Date20240405142042.php b/lib/Migration/Version8000Date20240405142042.php new file mode 100644 index 0000000000..9b0421f96f --- /dev/null +++ b/lib/Migration/Version8000Date20240405142042.php @@ -0,0 +1,62 @@ + + * + * @author Vitor Mattos + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Libresign\Migration; + +use Closure; +use Doctrine\DBAL\Types\JsonType; +use OCP\DB\ISchemaWrapper; +use OCP\Migration\IOutput; +use OCP\Migration\SimpleMigrationStep; + +/** + * FIXME Auto-generated migration step: Please modify to your needs! + */ +class Version8000Date20240405142042 extends SimpleMigrationStep { + /** + * @param IOutput $output + * @param Closure(): ISchemaWrapper $schemaClosure + * @param array $options + * @return null|ISchemaWrapper + */ + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + $table = $schema->getTable('libresign_file'); + + if ($table->hasColumn('metadata')) { + $options = $table->getColumn('metadata'); + if (!$options->getType() instanceof JsonType) { + $table->modifyColumn('metadata', [ + 'Type' => new JsonType(), + ]); + return $schema; + } + } + + return null; + } +} diff --git a/lib/Service/FileElementService.php b/lib/Service/FileElementService.php index 04122c0ece..f7bc1c556c 100644 --- a/lib/Service/FileElementService.php +++ b/lib/Service/FileElementService.php @@ -71,14 +71,14 @@ private function getVisibleElementFromProperties(array $properties, string $uuid $fileElement->setUry($coordinates['ury']); $fileElement->setLlx($coordinates['llx']); $fileElement->setLly($coordinates['lly']); - $fileElement->setMetadata(!empty($properties['metadata']) ? json_encode($properties['metadata']) : null); + $fileElement->setMetadata($properties['metadata'] ?? null); return $fileElement; } private function translateCoordinatesToInternalNotation(array $properties, File $file): array { $translated['page'] = $properties['coordinates']['page'] ?? 1; - $metadata = $file->getMetadataDecoded(); - $dimension = $metadata->d[$translated['page'] - 1]; + $metadata = $file->getMetadata(); + $dimension = $metadata['d'][$translated['page'] - 1]; if (isset($properties['coordinates']['ury'])) { $translated['ury'] = $properties['coordinates']['ury']; @@ -131,8 +131,8 @@ private function translateCoordinatesToInternalNotation(array $properties, File } public function translateCoordinatesFromInternalNotation(array $properties, File $file): array { - $metadata = $file->getMetadataDecoded(); - $dimension = $metadata->d[$properties['coordinates']['page'] - 1]; + $metadata = $file->getMetadata(); + $dimension = $metadata['d'][$properties['coordinates']['page'] - 1]; $translated['left'] = $properties['coordinates']['llx']; $translated['height'] = abs($properties['coordinates']['ury'] - $properties['coordinates']['lly']); diff --git a/lib/Service/FileService.php b/lib/Service/FileService.php index 675fac269d..5867f4ff40 100644 --- a/lib/Service/FileService.php +++ b/lib/Service/FileService.php @@ -259,15 +259,15 @@ private function getSigners(): array { private function getPages(): array { $return = []; - $metadata = $this->file->getMetadataDecoded(); - for ($page = 1; $page <= $metadata->p; $page++) { + $metadata = $this->file->getMetadata(); + for ($page = 1; $page <= $metadata['p']; $page++) { $return[] = [ 'url' => $this->urlGenerator->linkToRoute('ocs.libresign.File.getPage', [ 'apiVersion' => 'v1', 'uuid' => $this->file->getUuid(), 'page' => $page, ]), - 'resolution' => $metadata->d[$page - 1] + 'resolution' => $metadata['d'][$page - 1] ]; } return $return; diff --git a/lib/Service/RequestSignatureService.php b/lib/Service/RequestSignatureService.php index 7ccb0edb41..ef1b03666a 100644 --- a/lib/Service/RequestSignatureService.php +++ b/lib/Service/RequestSignatureService.php @@ -102,7 +102,7 @@ public function saveFile(array $data): FileEntity { $file->setUuid(UUIDUtil::getUUID()); $file->setCreatedAt(time()); $file->setName($data['name']); - $file->setMetadata(json_encode($this->getFileMetadata($node))); + $file->setMetadata($this->getFileMetadata($node)); if (!empty($data['callback'])) { $file->setCallback($data['callback']); }