Skip to content

Commit

Permalink
chore: Change metadata field to be a json and not a text
Browse files Browse the repository at this point in the history
Signed-off-by: Vitor Mattos <[email protected]>
  • Loading branch information
vitormattos committed Apr 5, 2024
1 parent 093dcb6 commit bec6232
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 24 deletions.
19 changes: 4 additions & 15 deletions lib/Db/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
namespace OCA\Libresign\Db;

use OCP\AppFramework\Db\Entity;
use stdClass;
use OCP\DB\Types;

/**
* @method void setId(int $id)
Expand All @@ -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 */
Expand Down Expand Up @@ -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);
}
}
62 changes: 62 additions & 0 deletions lib/Migration/Version8000Date20240405142042.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

/**
* @copyright Copyright (c) 2024 Vitor Mattos <[email protected]>
*
* @author Vitor Mattos <[email protected]>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/

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;
}
}
10 changes: 5 additions & 5 deletions lib/Service/FileElementService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down Expand Up @@ -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']);
Expand Down
6 changes: 3 additions & 3 deletions lib/Service/FileService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/Service/RequestSignatureService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
Expand Down

0 comments on commit bec6232

Please sign in to comment.