|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByTIC\MediaLibrary\Validation\Validators; |
|
4
|
|
|
|
|
5
|
|
|
use ByTIC\MediaLibrary\Validation\Constraints\FileConstraint; |
|
6
|
|
|
use ByTIC\MediaLibrary\Validation\Constraints\ImageConstraint; |
|
7
|
|
|
use Nip\Logger\Exception; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\File\UploadedFile; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Class FileValidator. |
|
12
|
|
|
*/ |
|
13
|
|
|
class FileValidator extends AbstractValidator |
|
14
|
|
|
{ |
|
15
|
|
|
public const KB_BYTES = 1000; |
|
16
|
|
|
public const MB_BYTES = 1000000; |
|
17
|
|
|
public const KIB_BYTES = 1024; |
|
18
|
|
|
public const MIB_BYTES = 1048576; |
|
19
|
|
|
|
|
20
|
|
|
private static $suffices = [ |
|
|
|
|
|
|
21
|
|
|
1 => 'bytes', |
|
22
|
|
|
self::KB_BYTES => 'kB', |
|
23
|
|
|
self::MB_BYTES => 'MB', |
|
24
|
|
|
self::KIB_BYTES => 'KiB', |
|
25
|
|
|
self::MIB_BYTES => 'MiB', |
|
26
|
|
|
]; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @return bool |
|
30
|
|
|
*/ |
|
31
|
2 |
|
protected function contraintNeedsValidation(): bool |
|
32
|
|
|
{ |
|
33
|
2 |
|
$constraint = $this->getConstraint(); |
|
34
|
|
|
|
|
35
|
2 |
|
if (null === $constraint->mimeTypes && null === $constraint->maxSize) { |
|
36
|
|
|
return false; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
2 |
|
return true; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @throws Exception |
|
44
|
|
|
*/ |
|
45
|
2 |
|
protected function doValidation() |
|
46
|
|
|
{ |
|
47
|
2 |
|
$this->validateMimeType(); |
|
48
|
2 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return void |
|
52
|
|
|
*/ |
|
53
|
2 |
|
protected function validateMimeType() |
|
54
|
|
|
{ |
|
55
|
2 |
|
$constraint = $this->getConstraint(); |
|
56
|
|
|
|
|
57
|
2 |
|
if ($constraint->mimeTypes) { |
|
58
|
|
|
$mimeTypes = (array) $constraint->mimeTypes; |
|
59
|
|
|
$mime = $this->determineMime(); |
|
|
|
|
|
|
60
|
|
|
foreach ($mimeTypes as $mimeType) { |
|
61
|
|
|
if ($mimeType === $mime) { |
|
62
|
|
|
return; |
|
63
|
|
|
} |
|
64
|
|
|
if ($discrete = strstr($mimeType, '/*', true)) { |
|
65
|
|
|
if (strstr($mime, '/', true) === $discrete) { |
|
|
|
|
|
|
66
|
|
|
return; |
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
$this->addViolation($constraint, FileConstraint::INVALID_MIME_TYPE_ERROR, []); |
|
71
|
|
|
} |
|
72
|
2 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @return string|null |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function determineMime() |
|
78
|
|
|
{ |
|
79
|
|
|
$file = $this->getValue(); |
|
80
|
|
|
if ($file instanceof UploadedFile) { |
|
|
|
|
|
|
81
|
|
|
return $file->getClientMimeType(); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @param $width |
|
87
|
|
|
* @param $height |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function validateOrientation($width, $height) |
|
90
|
|
|
{ |
|
91
|
|
|
$constraint = $this->getConstraint(); |
|
92
|
|
|
|
|
93
|
|
|
if (!$constraint->allowSquare && $width == $height) { |
|
94
|
|
|
$this->addViolation( |
|
95
|
|
|
$constraint, |
|
96
|
|
|
ImageConstraint::SQUARE_NOT_ALLOWED_ERROR, |
|
97
|
|
|
['width' => $width, 'height' => $height] |
|
98
|
|
|
); |
|
99
|
|
|
} |
|
100
|
|
|
if (!$constraint->allowLandscape && $width > $height) { |
|
101
|
|
|
$this->addViolation( |
|
102
|
|
|
$constraint, |
|
103
|
|
|
ImageConstraint::LANDSCAPE_NOT_ALLOWED_ERROR, |
|
104
|
|
|
['width' => $width, 'height' => $height] |
|
105
|
|
|
); |
|
106
|
|
|
} |
|
107
|
|
|
if (!$constraint->allowPortrait && $width < $height) { |
|
108
|
|
|
$this->addViolation( |
|
109
|
|
|
$constraint, |
|
110
|
|
|
ImageConstraint::PORTRAIT_NOT_ALLOWED_ERROR, |
|
111
|
|
|
['width' => $width, 'height' => $height] |
|
112
|
|
|
); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|