-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]>
- Loading branch information
Showing
7 changed files
with
301 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
} | ||
} |