1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace VSV\GVQ_API\Image\Controllers; |
4
|
|
|
|
5
|
|
|
use League\Flysystem\Filesystem; |
6
|
|
|
use Ramsey\Uuid\UuidFactoryInterface; |
7
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
8
|
|
|
use Symfony\Component\HttpFoundation\FileBag; |
9
|
|
|
use Symfony\Component\HttpFoundation\Request; |
10
|
|
|
use Symfony\Component\HttpFoundation\Response; |
11
|
|
|
use VSV\GVQ_API\Common\ValueObjects\NotEmptyString; |
12
|
|
|
use VSV\GVQ_API\Image\Validation\UploadFileValidator; |
13
|
|
|
|
14
|
|
|
class ImageController |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var Filesystem |
18
|
|
|
*/ |
19
|
|
|
private $fileSystem; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var UploadFileValidator |
23
|
|
|
*/ |
24
|
|
|
private $imageValidator; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var UuidFactoryInterface |
28
|
|
|
*/ |
29
|
|
|
private $uuidFactory; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param Filesystem $fileSystem |
33
|
|
|
* @param UploadFileValidator $imageValidator |
34
|
|
|
* @param UuidFactoryInterface $uuidFactory |
35
|
|
|
*/ |
36
|
|
|
public function __construct( |
37
|
|
|
Filesystem $fileSystem, |
38
|
|
|
UploadFileValidator $imageValidator, |
39
|
|
|
UuidFactoryInterface $uuidFactory |
40
|
|
|
) { |
41
|
|
|
$this->fileSystem = $fileSystem; |
42
|
|
|
$this->imageValidator = $imageValidator; |
43
|
|
|
$this->uuidFactory = $uuidFactory; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param Request $request |
48
|
|
|
* @return Response |
49
|
|
|
* @throws \League\Flysystem\FileExistsException |
50
|
|
|
*/ |
51
|
|
|
public function upload(Request $request): Response |
52
|
|
|
{ |
53
|
|
|
$uploadFile = $this->guardFiles($request->files); |
54
|
|
|
|
55
|
|
|
$filename = $this->handleImage($uploadFile); |
56
|
|
|
|
57
|
|
|
$response = new Response('{"filename":"'.$filename->toNative().'"}'); |
58
|
|
|
$response->headers->set('Content-Type', 'application/json'); |
59
|
|
|
|
60
|
|
|
return $response; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param UploadedFile $uploadedFile |
65
|
|
|
* @return NotEmptyString |
66
|
|
|
* @throws \League\Flysystem\FileExistsException |
67
|
|
|
*/ |
68
|
|
|
public function handleImage(UploadedFile $uploadedFile): NotEmptyString |
69
|
|
|
{ |
70
|
|
|
$this->imageValidator->validate($uploadedFile); |
71
|
|
|
|
72
|
|
|
$uuid = $this->uuidFactory->uuid4(); |
73
|
|
|
$filename = $uuid->toString().'.'. $uploadedFile->getClientOriginalExtension(); |
74
|
|
|
|
75
|
|
|
$stream = fopen($uploadedFile->getRealPath(), 'r+'); |
76
|
|
|
$this->fileSystem->writeStream($filename, $stream); |
77
|
|
|
fclose($stream); |
78
|
|
|
|
79
|
|
|
return new NotEmptyString($filename); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param FileBag $files |
84
|
|
|
* @return UploadedFile |
85
|
|
|
*/ |
86
|
|
|
private function guardFiles(FileBag $files): UploadedFile |
87
|
|
|
{ |
88
|
|
|
if (count($files) !== 1) { |
89
|
|
|
throw new \InvalidArgumentException('Exactly one image is required.'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (empty($files->get('image'))) { |
93
|
|
|
throw new \InvalidArgumentException('Key image is required inside form-data.'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** @var UploadedFile $uploadedFile */ |
97
|
|
|
$uploadedFile = $files->get('image'); |
98
|
|
|
|
99
|
|
|
$this->imageValidator->validate($uploadedFile); |
100
|
|
|
|
101
|
|
|
return $uploadedFile; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|