Skip to content

Commit

Permalink
Add text controller and encoder (#61)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
mattamon and mattamon authored May 14, 2024
1 parent 88f875b commit 88590df
Show file tree
Hide file tree
Showing 7 changed files with 301 additions and 1 deletion.
6 changes: 5 additions & 1 deletion config/assets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ services:

# Hydrators
Pimcore\Bundle\StudioBackendBundle\Asset\Hydrator\CustomSettingsHydratorInterface:
class: Pimcore\Bundle\StudioBackendBundle\Asset\Hydrator\CustomSettingsHydrator
class: Pimcore\Bundle\StudioBackendBundle\Asset\Hydrator\CustomSettingsHydrator

# Encoder
Pimcore\Bundle\StudioBackendBundle\Asset\Encoder\TextEncoderInterface:
class: Pimcore\Bundle\StudioBackendBundle\Asset\Encoder\TextEncoder
79 changes: 79 additions & 0 deletions src/Asset/Controller/Data/TextController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Asset\Controller\Data;

use OpenApi\Attributes\Get;
use Pimcore\Bundle\StaticResolverBundle\Models\Element\ServiceResolverInterface;
use Pimcore\Bundle\StudioBackendBundle\Asset\Encoder\TextEncoderInterface;
use Pimcore\Bundle\StudioBackendBundle\Controller\AbstractApiController;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Parameters\Path\IdParameter;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Content\DataJson;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Error\MethodNotAllowedResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Error\NotFoundResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Error\UnauthorizedResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Error\UnprocessableContentResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Error\UnsupportedMediaTypeResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\SuccessResponse;
use Pimcore\Bundle\StudioBackendBundle\OpenApi\Config\Tags;
use Pimcore\Bundle\StudioBackendBundle\Util\Traits\ElementProviderTrait;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Serializer\SerializerInterface;

/**
* @internal
*/
final class TextController extends AbstractApiController
{
use ElementProviderTrait;

public function __construct(
SerializerInterface $serializer,
private readonly ServiceResolverInterface $serviceResolver,
private readonly TextEncoderInterface $textEncoder,

) {
parent::__construct($serializer);
}

#[Route('/assets/{id}/text', name: 'pimcore_studio_api_get_asset_data_text', methods: ['GET'])]
//#[IsGranted('STUDIO_API')]
//#[IsGranted(UserPermissions::ASSETS->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)]);
}
}
44 changes: 44 additions & 0 deletions src/Asset/Encoder/TextEncoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Asset\Encoder;

use ForceUTF8\Encoding;
use Pimcore\Bundle\StudioBackendBundle\Exception\InvalidElementTypeException;
use Pimcore\Bundle\StudioBackendBundle\Exception\MaxFileSizeExceededException;
use Pimcore\Model\Asset\Text;
use Pimcore\Model\Element\ElementInterface;

final class TextEncoder implements TextEncoderInterface
{
private const MAX_FILE_SIZE = 2000000;

/**
* @throws InvalidElementTypeException|MaxFileSizeExceededException
*/
public function encodeUTF8(ElementInterface $element): string
{
if (!$element instanceof Text) {
throw new InvalidElementTypeException('Element must be an instance of Text');
}

if ($element->getFileSize() > self::MAX_FILE_SIZE) {
throw new MaxFileSizeExceededException(self::MAX_FILE_SIZE);
}

return Encoding::toUTF8($element->getData());
}
}
29 changes: 29 additions & 0 deletions src/Asset/Encoder/TextEncoderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Asset\Encoder;

use Pimcore\Bundle\StudioBackendBundle\Exception\InvalidElementTypeException;
use Pimcore\Bundle\StudioBackendBundle\Exception\MaxFileSizeExceededException;
use Pimcore\Model\Element\ElementInterface;

interface TextEncoderInterface
{
/**
* @throws InvalidElementTypeException|MaxFileSizeExceededException
*/
public function encodeUTF8(ElementInterface $element): string;
}
31 changes: 31 additions & 0 deletions src/Exception/MaxFileSizeExceededException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Exception;

/**
* @internal
*/
final class MaxFileSizeExceededException extends AbstractApiException
{
public function __construct(int|float $maxFileSize)
{
parent::__construct(
413,
sprintf('Max file size of %d bytes exceeded', $maxFileSize)
);
}
}
42 changes: 42 additions & 0 deletions src/OpenApi/Attributes/Response/Content/DataJson.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\OpenApi\Attributes\Response\Content;

use OpenApi\Attributes\JsonContent;
use OpenApi\Attributes\Property;

/**
* @internal
*/
final class DataJson extends JsonContent
{
public function __construct(string $description = '')
{
parent::__construct(
properties: [
new Property(
'data',
title: 'data',
description: $description,
type: 'string',
example: 'Test content'
),
],
type: 'object',
);
}
}
71 changes: 71 additions & 0 deletions tests/Unit/Asset/Encoder/EncoderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php
declare(strict_types=1);

/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/

namespace Pimcore\Bundle\StudioBackendBundle\Tests\Unit\Asset\Encoder;

use Codeception\Test\Unit;
use Exception;
use Pimcore\Bundle\StudioBackendBundle\Asset\Encoder\TextEncoder;
use Pimcore\Bundle\StudioBackendBundle\Asset\Encoder\TextEncoderInterface;
use Pimcore\Bundle\StudioBackendBundle\Exception\InvalidElementTypeException;
use Pimcore\Bundle\StudioBackendBundle\Exception\MaxFileSizeExceededException;
use Pimcore\Model\Asset\Document;
use Pimcore\Model\Asset\Text;

/**
* @internal
*/
final class EncoderTest extends Unit
{
private TextEncoderInterface $encoder;

public function _before(): void
{
$this->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'));
}
}

0 comments on commit 88590df

Please sign in to comment.