Skip to content

Commit

Permalink
feat: Preview processor
Browse files Browse the repository at this point in the history
  • Loading branch information
theus77 committed Oct 24, 2023
1 parent 9309e19 commit 9be419b
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->variableNode('store_data_services')->defaultValue([])->end()
->booleanNode('profiler')->defaultFalse()->end()
->scalarNode('hash_algo')->defaultValue('sha1')->end()
->scalarNode('preview_url_pattern')->defaultValue('')->end()
->scalarNode('backend_url')->defaultValue(null)->end()
->scalarNode('backend_api_key')->defaultValue(null)->end()
->scalarNode('backend_api_verify')->defaultValue(true)->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function load(array $configs, ContainerBuilder $container): void
}

$container->setParameter('ems_common.hash_algo', $config['hash_algo']);
$container->setParameter('ems_common.preview_url_pattern', $config['preview_url_pattern']);
$container->setParameter('ems_common.backend_url', $config['backend_url']);
$container->setParameter('ems_common.backend_api_key', $config['backend_api_key']);
$container->setParameter('ems_common.backend_api_verify', $config['backend_api_verify']);
Expand Down
6 changes: 6 additions & 0 deletions EMS/common-bundle/src/Resources/config/storage.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@
<argument type="service" id="ems_common.helper.cache" />
<argument type="string">%kernel.project_dir%</argument>
<argument type="service" id="file_locator" />
<argument type="service" id="ems_common.storage.preview"/>
<tag name="monolog.logger" channel="ems_common"/>
</service>

<service id="ems_common.storage.preview" class="EMS\CommonBundle\Storage\Processor\Preview">
<argument type="service" id="Symfony\Contracts\HttpClient\HttpClientInterface" />
<argument type="string">%ems_common.preview_url_pattern%</argument>
</service>

<service id="ems_common.storage.factory.fs" class="EMS\CommonBundle\Storage\Factory\FileSystemFactory">
<argument type="service" id="logger" />
<argument type="string">%kernel.project_dir%</argument>
Expand Down
39 changes: 39 additions & 0 deletions EMS/common-bundle/src/Storage/Processor/Preview.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace EMS\CommonBundle\Storage\Processor;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Contracts\HttpClient\HttpClientInterface;

class Preview
{
public function __construct(
private readonly HttpClientInterface $client,
private readonly string $urlPattern,
) {
}

public function generate(Config $config, string $cacheFilename, string $filename): string
{
if (!\file_exists(\dirname($cacheFilename))) {
\mkdir(\dirname($cacheFilename), 0777, true);
}
$url = \str_replace([
'%hash%',
'%filename%',
'%parameters%',
], [
$config->getAssetHash(),
$filename,
$config->getParameters(),
], $this->urlPattern);
\file_put_contents($cacheFilename, $this->client->request(
Request::METHOD_GET,
$url,
)->getContent());

return $cacheFilename;
}
}
13 changes: 11 additions & 2 deletions EMS/common-bundle/src/Storage/Processor/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public function __construct(
private readonly Cache $cacheHelper,
private readonly string $projectDir,
private readonly FileLocator $fileLocator,
private readonly Preview $previewManager,
) {
}

Expand Down Expand Up @@ -111,7 +112,7 @@ public function configFactory(string $hash, array $configArray): Config
return new Config($this->storageManager, $hash, $configHash, $configArray);
}

private function generateStream(Config $config, string $cacheFilename): StreamInterface
private function generateStream(Config $config, string $cacheFilename, string $filename): StreamInterface
{
$file = null;
if (!$config->isCacheableResult()) {
Expand All @@ -127,6 +128,14 @@ private function generateStream(Config $config, string $cacheFilename): StreamIn

return new Stream($resource);
}
if ('preview' === $config->getConfigType()) {
$resource = \fopen($this->previewManager->generate($config, $cacheFilename, $filename), 'r');
if (false === $resource) {
throw new \Exception('It was not possible to open the generated preview');
}

return new Stream($resource);
}

if ('zip' === $config->getConfigType()) {
return $this->generateZip($config);
Expand Down Expand Up @@ -226,7 +235,7 @@ public function getStream(Config $config, string $filename, bool $noCache = fals
}
}

return $this->generateStream($config, $cacheFilename);
return $this->generateStream($config, $cacheFilename, $filename);
}

private function getResponseFromStreamInterface(StreamInterface $stream, Request $request): StreamedResponse
Expand Down
2 changes: 2 additions & 0 deletions elasticms-admin/config/packages/ems_common.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ parameters:
env(EMS_WEBALIZE_REMOVABLE_REGEX): '/([^a-zA-Z0-9\_\|\ \-\.])|(\.$)/'
env(EMS_WEBALIZE_DASHABLE_REGEX): '/[\/\_\|\ \-]+/'
env(EMS_EXCLUDED_CONTENT_TYPES): '[]'
env(EMS_PREVIEW_URL_PATTERN): ''

ems_common:
hash_algo: '%env(string:EMS_HASH_ALGO)%'
Expand All @@ -46,6 +47,7 @@ ems_common:
backend_api_key: '%env(string:EMS_BACKEND_API_KEY)%'
backend_api_verify: '%env(bool:EMS_BACKEND_API_VERIFY)%'
excluded_content_types: '%env(json:EMS_EXCLUDED_CONTENT_TYPES)%'
preview_url_pattern: '%env(string:EMS_PREVIEW_URL_PATTERN)%'
webalize:
removable_regex: '%env(string:EMS_WEBALIZE_REMOVABLE_REGEX)%'
dashable_regex: '%env(string:EMS_WEBALIZE_DASHABLE_REGEX)%'
Expand Down

0 comments on commit 9be419b

Please sign in to comment.