Skip to content

Commit fbd71df

Browse files
committed
refactor: media file storage
1 parent a6fdd88 commit fbd71df

File tree

80 files changed

+1078
-2118
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+1078
-2118
lines changed

Action/MediaCropActionType.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function createViewData(array $options, Data $data, object $resource = nu
1616
public function configureOptions(OptionsResolver $resolver): void
1717
{
1818
$resolver->setDefaults([
19-
'model' => 'MediaCropAction'
19+
'model' => 'MediaCropAction',
2020
]);
2121

2222
$resolver->setRequired([

Action/MediaDownloadActionType.php

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Enhavo\Bundle\MediaBundle\Action;
4+
5+
use Enhavo\Bundle\ApiBundle\Data\Data;
6+
use Enhavo\Bundle\ResourceBundle\Action\AbstractActionType;
7+
use Symfony\Component\OptionsResolver\OptionsResolver;
8+
9+
class MediaDownloadActionType extends AbstractActionType
10+
{
11+
public function createViewData(array $options, Data $data, object $resource = null): void
12+
{
13+
14+
}
15+
16+
public function configureOptions(OptionsResolver $resolver): void
17+
{
18+
$resolver->setDefaults([
19+
'label' => 'media.form.action.download',
20+
'translation_domain' => 'EnhavoMediaBundle',
21+
'icon' => 'cloud_download',
22+
'component' => 'action-media-form',
23+
'model' => 'MediaDownloadAction',
24+
]);
25+
}
26+
27+
public static function getName(): ?string
28+
{
29+
return 'media_download';
30+
}
31+
}

Action/MediaFormatActionType.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Enhavo\Bundle\MediaBundle\Action;
4+
5+
use Enhavo\Bundle\ApiBundle\Data\Data;
6+
use Enhavo\Bundle\ResourceBundle\Action\AbstractActionType;
7+
use Symfony\Component\Form\FormInterface;
8+
use Symfony\Component\OptionsResolver\OptionsResolver;
9+
10+
class MediaFormatActionType extends AbstractActionType
11+
{
12+
public function createViewData(array $options, Data $data, object $resource = null): void
13+
{
14+
$formats = [];
15+
if ($resource instanceof FormInterface) {
16+
$options = $resource->getConfig()->getOptions();
17+
if (isset($options['formats'])) {
18+
$formats = $options['formats'];
19+
}
20+
}
21+
22+
$data->set('formats', $formats);
23+
}
24+
25+
public function configureOptions(OptionsResolver $resolver): void
26+
{
27+
$resolver->setDefaults([
28+
'label' => 'media.form.action.format',
29+
'translation_domain' => 'EnhavoMediaBundle',
30+
'icon' => 'crop',
31+
'component' => 'action-media-format',
32+
'model' => 'MediaFormatAction'
33+
]);
34+
}
35+
36+
public static function getName(): ?string
37+
{
38+
return 'media_format';
39+
}
40+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Enhavo\Bundle\MediaBundle\Checksum;
4+
5+
use Enhavo\Bundle\MediaBundle\Content\ContentInterface;
6+
7+
interface ChecksumGeneratorInterface
8+
{
9+
public function getChecksum(ContentInterface $content): string;
10+
}

Checksum/Md5ChecksumGenerator.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Enhavo\Bundle\MediaBundle\Checksum;
4+
5+
use Enhavo\Bundle\MediaBundle\Content\ContentInterface;
6+
7+
class Md5ChecksumGenerator implements ChecksumGeneratorInterface
8+
{
9+
public function getChecksum(ContentInterface $content): string
10+
{
11+
return md5($content->getContent());
12+
}
13+
}

Checksum/Sha256ChecksumGenerator.php

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Enhavo\Bundle\MediaBundle\Checksum;
4+
5+
use Enhavo\Bundle\MediaBundle\Content\ContentInterface;
6+
7+
class Sha256ChecksumGenerator implements ChecksumGeneratorInterface
8+
{
9+
public function getChecksum(ContentInterface $content): string
10+
{
11+
return hash('sha256', $content->getContent());
12+
}
13+
}

Command/UpdateFileCommand.php

-79
This file was deleted.

Content/CopyContent.php

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: gseidel
5+
* Date: 27.08.17
6+
* Time: 10:29
7+
*/
8+
9+
namespace Enhavo\Bundle\MediaBundle\Content;
10+
11+
use Enhavo\Bundle\MediaBundle\Exception\FileException;
12+
use Symfony\Component\Filesystem\Filesystem;
13+
14+
class CopyContent extends AbstractContent
15+
{
16+
private string $path;
17+
18+
public function __construct(ContentInterface $content)
19+
{
20+
$this->path = tempnam(sys_get_temp_dir(), 'Content');
21+
(new Filesystem())->copy($content->getFilePath(), $this->path, true);
22+
}
23+
24+
public function getContent()
25+
{
26+
if (!file_exists($this->path)) {
27+
throw new FileException(sprintf('File could not be found on path "%s"', $this->path));
28+
}
29+
return file_get_contents($this->path);
30+
}
31+
32+
public function getFilePath()
33+
{
34+
return $this->path;
35+
}
36+
}

DependencyInjection/Compiler/MediaCompilerPass.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,36 @@
88

99
namespace Enhavo\Bundle\MediaBundle\DependencyInjection\Compiler;
1010

11+
use Enhavo\Bundle\MediaBundle\Checksum\ChecksumGeneratorInterface;
1112
use Enhavo\Bundle\MediaBundle\FileNotFound\FileNotFoundHandlerInterface;
1213
use Enhavo\Bundle\MediaBundle\GarbageCollection\GarbageCollector;
1314
use Enhavo\Bundle\MediaBundle\GarbageCollection\GarbageCollectorInterface;
15+
use Enhavo\Bundle\MediaBundle\Storage\StorageInterface;
1416
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
1517
use Symfony\Component\DependencyInjection\ContainerBuilder;
1618

1719
class MediaCompilerPass implements CompilerPassInterface
1820
{
1921
public function process(ContainerBuilder $container)
2022
{
21-
$this->createProviderAlias($container);
23+
$this->createChecksumGeneratorAlias($container);
2224
$this->createStorageAlias($container);
2325
$this->createCacheAlias($container);
2426
$this->injectGarbageCollectorVoters($container);
2527
$this->addGarbageCollectorAlias($container);
2628
$this->addFileNotFoundHandlerAlias($container);
2729
}
2830

29-
private function createProviderAlias(ContainerBuilder $container): void
31+
private function createChecksumGeneratorAlias(ContainerBuilder $container): void
3032
{
31-
$providerServiceName = $container->getParameter('enhavo_media.provider');
32-
$container->setAlias('enhavo_media.provider', $providerServiceName);
33+
$providerServiceName = $container->getParameter('enhavo_media.checksum_generator');
34+
$container->setAlias(ChecksumGeneratorInterface::class, $providerServiceName);
3335
}
3436

3537
private function createStorageAlias(ContainerBuilder $container): void
3638
{
3739
$providerServiceName = $container->getParameter('enhavo_media.storage');
38-
$container->setAlias('enhavo_media.storage', $providerServiceName);
40+
$container->setAlias(StorageInterface::class, $providerServiceName);
3941
}
4042

4143
private function createCacheAlias(ContainerBuilder $container): void

0 commit comments

Comments
 (0)