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')); + } +}