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