Skip to content

Commit e8e66e0

Browse files
Andrey ErdikovAndrey Erdikov
authored andcommitted
[C0-3455] Добавил создание файлов и каталогов
1 parent bc27b03 commit e8e66e0

File tree

14 files changed

+344
-6
lines changed

14 files changed

+344
-6
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"psr/http-message": "*",
99
"ramsey/uuid": "^4.1",
1010
"ramsey/uuid-doctrine": "^1.6",
11-
"symfony/validator": "4.4.*"
11+
"symfony/validator": "4.4.*",
12+
"vich/uploader-bundle": "^1.16"
1213
},
1314
"require-dev": {
1415
"codeception/codeception": "^2.5",
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Skyeng\MarketingCmsBundle\Application\Cms\MediaFile\Service;
6+
7+
use Skyeng\MarketingCmsBundle\Application\Exception\ApplicationException;
8+
use Skyeng\MarketingCmsBundle\Domain\Entity\ValueObject\MediaFileType;
9+
use Symfony\Component\HttpFoundation\File\File;
10+
11+
class MediaFileTypeResolver
12+
{
13+
private const IMAGE_MIME_TYPES = [
14+
'image/jpeg',
15+
'image/png',
16+
'image/gif',
17+
];
18+
19+
private const PDF_MIME_TYPE = 'application/pdf';
20+
21+
private const VIDEO_MIME_TYPES = [
22+
'video/mp4',
23+
];
24+
25+
public function getMediaFileTypeByFile(File $file): MediaFileType
26+
{
27+
$mimeType = $file->getMimeType();
28+
29+
if ($mimeType === self::PDF_MIME_TYPE) {
30+
return new MediaFileType(MediaFileType::PDF_TYPE);
31+
}
32+
33+
if (in_array($mimeType, self::IMAGE_MIME_TYPES, true)) {
34+
return new MediaFileType(MediaFileType::IMAGE_TYPE);
35+
}
36+
37+
if (in_array($mimeType, self::VIDEO_MIME_TYPES, true)) {
38+
return new MediaFileType(MediaFileType::VIDEO_TYPE);
39+
}
40+
41+
throw new ApplicationException('Mime type ' . $mimeType . ' is not supported');
42+
}
43+
}

src/Domain/Entity/MediaCatalog.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ public function __construct(Id $id, string $name)
2424
$this->name = $name;
2525
}
2626

27+
public function __toString()
28+
{
29+
return $this->name;
30+
}
31+
2732
public function getId(): Id
2833
{
2934
return $this->id;
@@ -33,4 +38,9 @@ public function getName(): string
3338
{
3439
return $this->name;
3540
}
41+
42+
public function setName(string $name): void
43+
{
44+
$this->name = $name;
45+
}
3646
}

src/Domain/Entity/MediaFile.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Skyeng\MarketingCmsBundle\Domain\Entity\ValueObject\Id;
88
use Skyeng\MarketingCmsBundle\Domain\Entity\ValueObject\MediaFileStorage;
99
use Skyeng\MarketingCmsBundle\Domain\Entity\ValueObject\MediaFileType;
10+
use Symfony\Component\HttpFoundation\File\File;
1011

1112
class MediaFile
1213
{
@@ -40,6 +41,11 @@ class MediaFile
4041
*/
4142
private $storage;
4243

44+
/**
45+
* @var File|null
46+
*/
47+
private $file;
48+
4349
public function __construct(Id $id, MediaCatalog $catalog, string $title, MediaFileType $type, string $name, MediaFileStorage $storage)
4450
{
4551
$this->id = $id;
@@ -60,23 +66,58 @@ public function getCatalog(): MediaCatalog
6066
return $this->catalog;
6167
}
6268

69+
public function setCatalog(MediaCatalog $catalog): void
70+
{
71+
$this->catalog = $catalog;
72+
}
73+
6374
public function getTitle(): string
6475
{
6576
return $this->title;
6677
}
6778

79+
public function setTitle(string $title): void
80+
{
81+
$this->title = $title;
82+
}
83+
6884
public function getType(): MediaFileType
6985
{
7086
return $this->type;
7187
}
7288

89+
public function setType(MediaFileType $type): void
90+
{
91+
$this->type = $type;
92+
}
93+
7394
public function getName(): string
7495
{
7596
return $this->name;
7697
}
7798

99+
public function setName(string $name): void
100+
{
101+
$this->name = $name;
102+
}
103+
78104
public function getStorage(): MediaFileStorage
79105
{
80106
return $this->storage;
81107
}
108+
109+
public function setStorage(MediaFileStorage $storage): void
110+
{
111+
$this->storage = $storage;
112+
}
113+
114+
public function getFile(): ?File
115+
{
116+
return $this->file;
117+
}
118+
119+
public function setFile(?File $file): void
120+
{
121+
$this->file = $file;
122+
}
82123
}

src/Domain/Entity/ValueObject/MediaFileStorage.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
class MediaFileStorage
1111
{
12-
public const S3_STORAGE = 's3';
13-
public const LOCAL_STORAGE = 'local';
12+
public const S3_STORAGE = 'uploads.s3';
13+
public const NFS_STORAGE = 'uploads.nfs';
1414

1515
public const AVAILABLE_STORAGES = [
1616
self::S3_STORAGE => self::S3_STORAGE,
17-
self::LOCAL_STORAGE => self::LOCAL_STORAGE,
17+
self::NFS_STORAGE => self::NFS_STORAGE,
1818
];
1919

2020
use ValueObjectTrait;
@@ -31,8 +31,8 @@ public function isS3(): bool
3131
return $this->value === self::S3_STORAGE;
3232
}
3333

34-
public function isLocal(): bool
34+
public function isNfs(): bool
3535
{
36-
return $this->value === self::LOCAL_STORAGE;
36+
return $this->value === self::NFS_STORAGE;
3737
}
3838
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Skyeng\MarketingCmsBundle\EventListener;
6+
7+
use Doctrine\Persistence\Event\LifecycleEventArgs;
8+
use Skyeng\MarketingCmsBundle\Application\Cms\MediaFile\Service\MediaFileTypeResolver;
9+
use Skyeng\MarketingCmsBundle\Domain\Entity\MediaFile;
10+
use Skyeng\MarketingCmsBundle\Domain\Entity\ValueObject\MediaFileStorage;
11+
12+
class MediaFileUpdatedListener
13+
{
14+
/**
15+
* @var MediaFileTypeResolver
16+
*/
17+
private $mediaFileTypeResolver;
18+
19+
/**
20+
* @var string
21+
*/
22+
private $mediaUploadDestination;
23+
24+
public function __construct(MediaFileTypeResolver $mediaFileTypeResolver, string $mediaUploadDestination)
25+
{
26+
$this->mediaFileTypeResolver = $mediaFileTypeResolver;
27+
$this->mediaUploadDestination = $mediaUploadDestination;
28+
}
29+
30+
public function postUpdate(LifecycleEventArgs $args): void
31+
{
32+
$this->handleEventArgs($args);
33+
}
34+
35+
public function postPersist(LifecycleEventArgs $args): void
36+
{
37+
$this->handleEventArgs($args);
38+
}
39+
40+
private function handleEventArgs(LifecycleEventArgs $args): void
41+
{
42+
$object = $args->getObject();
43+
44+
if (!$object instanceof MediaFile) {
45+
return;
46+
}
47+
48+
$object->setType($this->mediaFileTypeResolver->getMediaFileTypeByFile($object->getFile()));
49+
$object->setStorage(new MediaFileStorage($this->mediaUploadDestination));
50+
}
51+
}

src/Infrastructure/DependencyInjection/MarketingCmsExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ private function prependPackagesConfigs(ContainerBuilder $container): void
3333
$configsFiles = [
3434
'doctrine.yaml',
3535
'nelmio_api_doc.yaml',
36+
'vich_uploader.yaml',
3637
];
3738

3839
foreach ($configsFiles as $configFile) {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Skyeng\MarketingCmsBundle\Infrastructure\Symfony\Form\Fields;
6+
7+
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Field\FieldInterface;
8+
use EasyCorp\Bundle\EasyAdminBundle\Field\FieldTrait;
9+
use Vich\UploaderBundle\Form\Type\VichFileType;
10+
11+
class VichFileField implements FieldInterface
12+
{
13+
use FieldTrait;
14+
15+
public static function new(string $propertyName, ?string $label = null)
16+
{
17+
return (new self())
18+
->setProperty($propertyName)
19+
->setTemplatePath('')
20+
->setLabel($label)
21+
->setFormType(VichFileType::class);
22+
}
23+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
vich_uploader:
2+
mappings:
3+
media_files:
4+
upload_destination: 'uploads.s3'
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
vich_uploader:
2+
db_driver: orm
3+
storage: flysystem
4+
5+
mappings:
6+
media_files:
7+
uri_prefix: '%app.uploads_base_url%'
8+
upload_destination: 'uploads.nfs'

0 commit comments

Comments
 (0)