Skip to content

Commit

Permalink
Merge pull request #2667 from LibreSign/chore/change-field-type
Browse files Browse the repository at this point in the history
chore: Change metadata field to be a json and not a text
  • Loading branch information
vitormattos authored Apr 5, 2024
2 parents d5e4247 + 0a4251f commit 6ffb134
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 73 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
],
"post-update-cmd": [
"composer dump-autoload"
]
],
"test:unit": "vendor/bin/phpunit --colors=always"
},
"extra": {
"bamarni-bin": {
Expand Down
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);
}
}
7 changes: 4 additions & 3 deletions lib/Db/FileElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
namespace OCA\Libresign\Db;

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

/**
* @method int getId()
Expand All @@ -35,8 +36,8 @@
* @method void setSignRequestId(int $signRequestId)
* @method string getType()
* @method void setType(string $type)
* @method string getMetadata()
* @method void setMetadata(string $metadata)
* @method array getMetadata()
* @method void setMetadata(array $metadata)
* @method int getPage()
* @method void setPage(int $page)
* @method int getUrx()
Expand Down Expand Up @@ -88,7 +89,7 @@ public function __construct() {
$this->addType('fileId', 'integer');
$this->addType('signRequestId', 'integer');
$this->addType('type', 'string');
$this->addType('metadata', 'string');
$this->addType('metadata', Types::JSON);
$this->addType('page', 'integer');
$this->addType('urx', 'integer');
$this->addType('ury', 'integer');
Expand Down
39 changes: 0 additions & 39 deletions lib/Db/FileType.php

This file was deleted.

73 changes: 73 additions & 0 deletions lib/Migration/Version8000Date20240405142042.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?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;

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(),
]);
$changed = true;
}
}

$table = $schema->getTable('libresign_file_element');
if ($table->hasColumn('metadata')) {
$options = $table->getColumn('metadata');
if (!$options->getType() instanceof JsonType) {
$table->modifyColumn('metadata', [
'Type' => new JsonType(),
]);
$changed = true;
}
}

if ($changed) {
return $schema;
}

return null;
}
}
12 changes: 6 additions & 6 deletions lib/Service/FileElementService.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,19 @@ 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'];
} elseif (isset($properties['coordinates']['top'])) {
$translated['ury'] = $dimension->h - $properties['coordinates']['top'];
$translated['ury'] = $dimension['h'] - $properties['coordinates']['top'];
} else {
$translated['ury'] = 0;
}
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
10 changes: 5 additions & 5 deletions tests/psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@
<code>throw new NotLoggedInException();</code>
</InvalidThrow>
</file>
<file src="lib/Controller/SignFileController.php">
<UndefinedClass occurrences="1">
<code>SmsTransmissionException</code>
</UndefinedClass>
</file>
<file src="lib/Db/PagerFantaQueryAdapter.php">
<InvalidArgument occurrences="1">
<code>$qb</code>
Expand Down Expand Up @@ -81,6 +76,11 @@
<code>$schema-&gt;dropTable('libresign_file_user')</code>
</UndefinedDocblockClass>
</file>
<file src="lib/Migration/Version8000Date20240405142042.php">
<UndefinedClass occurrences="1">
<code>JsonType</code>
</UndefinedClass>
</file>
<file src="lib/Service/AccountService.php">
<UndefinedClass occurrences="5">
<code>$this-&gt;newUserMail</code>
Expand Down

0 comments on commit 6ffb134

Please sign in to comment.