From 88590df43b5a00f9bd3e2c30e7e1c8fc0292d455 Mon Sep 17 00:00:00 2001 From: Matthias Schuhmayer <38959016+mattamon@users.noreply.github.com> Date: Tue, 14 May 2024 13:55:31 +0200 Subject: [PATCH] Add text controller and encoder (#61) * Add Text preview controller and encoder * Apply php-cs-fixer changes * Add declare strict * Remove preview * Remove unused use statements * Add max file size exception * Add Encoder Tests --------- Co-authored-by: mattamon --- config/assets.yaml | 6 +- src/Asset/Controller/Data/TextController.php | 79 +++++++++++++++++++ src/Asset/Encoder/TextEncoder.php | 44 +++++++++++ src/Asset/Encoder/TextEncoderInterface.php | 29 +++++++ .../MaxFileSizeExceededException.php | 31 ++++++++ .../Attributes/Response/Content/DataJson.php | 42 ++++++++++ tests/Unit/Asset/Encoder/EncoderTest.php | 71 +++++++++++++++++ 7 files changed, 301 insertions(+), 1 deletion(-) create mode 100644 src/Asset/Controller/Data/TextController.php create mode 100644 src/Asset/Encoder/TextEncoder.php create mode 100644 src/Asset/Encoder/TextEncoderInterface.php create mode 100644 src/Exception/MaxFileSizeExceededException.php create mode 100644 src/OpenApi/Attributes/Response/Content/DataJson.php create mode 100644 tests/Unit/Asset/Encoder/EncoderTest.php diff --git a/config/assets.yaml b/config/assets.yaml index f980bafea..28aee31b2 100644 --- a/config/assets.yaml +++ b/config/assets.yaml @@ -13,4 +13,8 @@ services: # Hydrators Pimcore\Bundle\StudioBackendBundle\Asset\Hydrator\CustomSettingsHydratorInterface: - class: Pimcore\Bundle\StudioBackendBundle\Asset\Hydrator\CustomSettingsHydrator \ No newline at end of file + class: Pimcore\Bundle\StudioBackendBundle\Asset\Hydrator\CustomSettingsHydrator + + # Encoder + Pimcore\Bundle\StudioBackendBundle\Asset\Encoder\TextEncoderInterface: + class: Pimcore\Bundle\StudioBackendBundle\Asset\Encoder\TextEncoder \ No newline at end of file diff --git a/src/Asset/Controller/Data/TextController.php b/src/Asset/Controller/Data/TextController.php new file mode 100644 index 000000000..226e2e548 --- /dev/null +++ b/src/Asset/Controller/Data/TextController.php @@ -0,0 +1,79 @@ +value)] + #[Get( + path: self::API_PATH . '/assets/{id}/text', + operationId: 'getAssetDataTextById', + summary: 'Get asset data in text UTF8 representation by id', + security: self::SECURITY_SCHEME, + tags: [Tags::Assets->name] + )] + #[IdParameter(type: 'asset')] + #[SuccessResponse( + description: 'UTF8 encoded text data', + content: new DataJson('UTF 8 encoded text data') + )] + #[UnauthorizedResponse] + #[NotFoundResponse] + #[MethodNotAllowedResponse] + #[UnsupportedMediaTypeResponse] + #[UnprocessableContentResponse] + public function getTextData(int $id): JsonResponse + { + $element = $this->getElement($this->serviceResolver, 'asset', $id); + + return $this->jsonResponse(['data' => $this->textEncoder->encodeUTF8($element)]); + } +} diff --git a/src/Asset/Encoder/TextEncoder.php b/src/Asset/Encoder/TextEncoder.php new file mode 100644 index 000000000..bc35fb6b6 --- /dev/null +++ b/src/Asset/Encoder/TextEncoder.php @@ -0,0 +1,44 @@ +getFileSize() > self::MAX_FILE_SIZE) { + throw new MaxFileSizeExceededException(self::MAX_FILE_SIZE); + } + + return Encoding::toUTF8($element->getData()); + } +} diff --git a/src/Asset/Encoder/TextEncoderInterface.php b/src/Asset/Encoder/TextEncoderInterface.php new file mode 100644 index 000000000..a8aeb5574 --- /dev/null +++ b/src/Asset/Encoder/TextEncoderInterface.php @@ -0,0 +1,29 @@ +encoder = new TextEncoder(); + } + + public function testWrongElementType(): void + { + $element = new Document(); + + $this->expectException(InvalidElementTypeException::class); + + $this->encoder->encodeUTF8($element); + } + + /** + * @throws Exception + */ + public function testFileSizeExceeded(): void + { + $element = $this->makeEmpty(Text::class, ['getFileSize' => 2000001]); + + $this->expectException(MaxFileSizeExceededException::class); + + $this->encoder->encodeUTF8($element); + } + + /** + * @throws Exception + */ + public function testUTF8Encoding(): void + { + $element = $this->makeEmpty(Text::class, ['getData' => 'Héllö, 世界!']); + $encodedData = $this->encoder->encodeUTF8($element); + + $this->assertTrue(mb_check_encoding($encodedData, 'UTF-8')); + } +}