1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file was created by developers working at BitBag |
5
|
|
|
* Do you need more information about us and what we do? Visit our https://bitbag.io website! |
6
|
|
|
* We are hiring developers from all over the world. Join us and start your new, exciting adventure and become part of us: https://bitbag.io/career |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace BitBag\SyliusCmsPlugin\Controller\Action\Admin; |
12
|
|
|
|
13
|
|
|
use BitBag\SyliusCmsPlugin\Entity\MediaInterface; |
14
|
|
|
use BitBag\SyliusCmsPlugin\Repository\MediaRepositoryInterface; |
15
|
|
|
use BitBag\SyliusCmsPlugin\Resolver\MediaProviderResolverInterface; |
16
|
|
|
use Sylius\Component\Core\Formatter\StringInflector; |
17
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
18
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
19
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
20
|
|
|
use Symfony\Component\HttpFoundation\Request; |
21
|
|
|
use Symfony\Component\HttpFoundation\Response; |
22
|
|
|
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; |
23
|
|
|
|
24
|
|
|
final class UploadEditorImageAction |
25
|
|
|
{ |
26
|
|
|
/** @var MediaProviderResolverInterface */ |
27
|
|
|
private $mediaProviderResolver; |
28
|
|
|
|
29
|
|
|
/** @var MediaRepositoryInterface */ |
30
|
|
|
private $mediaRepository; |
31
|
|
|
|
32
|
|
|
/** @var FactoryInterface */ |
33
|
|
|
private $mediaFactory; |
34
|
|
|
|
35
|
|
|
public function __construct( |
36
|
|
|
MediaProviderResolverInterface $mediaProviderResolver, |
37
|
|
|
MediaRepositoryInterface $mediaRepository, |
38
|
|
|
FactoryInterface $mediaFactory |
39
|
|
|
) { |
40
|
|
|
$this->mediaProviderResolver = $mediaProviderResolver; |
41
|
|
|
$this->mediaRepository = $mediaRepository; |
42
|
|
|
$this->mediaFactory = $mediaFactory; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function __invoke(Request $request): Response |
46
|
|
|
{ |
47
|
|
|
/** @var UploadedFile|null $image */ |
48
|
|
|
$image = $request->files->get('upload'); |
49
|
|
|
|
50
|
|
|
if (null === $image || !$this->isValidImage($image)) { |
51
|
|
|
throw new BadRequestHttpException(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$media = $this->createMedia($image); |
55
|
|
|
|
56
|
|
|
return new JsonResponse([ |
57
|
|
|
'uploaded' => 1, |
58
|
|
|
'fileName' => $image->getFilename(), |
59
|
|
|
'url' => $media->getPath(), |
60
|
|
|
]); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function isValidImage(UploadedFile $image): bool |
64
|
|
|
{ |
65
|
|
|
return in_array($image->getMimeType(), ['image/jpeg', 'image/png', 'image/gif', 'image/svg+xml'], true); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
private function createMedia(UploadedFile $image): MediaInterface |
69
|
|
|
{ |
70
|
|
|
/** @var MediaInterface $media */ |
71
|
|
|
$media = $this->mediaFactory->createNew(); |
72
|
|
|
$code = $this->createMediaCode(pathinfo($image->getClientOriginalName())['filename']); |
73
|
|
|
|
74
|
|
|
$media->setFile($image); |
75
|
|
|
$media->setCode($code); |
76
|
|
|
$media->setType(MediaInterface::IMAGE_TYPE); |
77
|
|
|
|
78
|
|
|
$this->mediaProviderResolver->resolveProvider($media)->upload($media); |
79
|
|
|
$this->mediaRepository->add($media); |
80
|
|
|
|
81
|
|
|
return $media; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function createMediaCode(string $name): string |
85
|
|
|
{ |
86
|
|
|
$code = StringInflector::nameToCode($name); |
87
|
|
|
$i = 0; |
88
|
|
|
|
89
|
|
|
do { |
90
|
|
|
if (0 < $i) { |
91
|
|
|
$code = $code . '_image_' . (string) $i; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
++$i; |
95
|
|
|
} while (0 < count($this->mediaRepository->findBy(['code' => $code]))); |
96
|
|
|
|
97
|
|
|
return $code; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|