|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Saito - The Threaded Web Forum |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copyright (c) the Saito Project Developers |
|
9
|
|
|
* @link https://github.com/Schlaefer/Saito |
|
10
|
|
|
* @license http://opensource.org/licenses/MIT |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace ImageUploader\Lib; |
|
14
|
|
|
|
|
15
|
|
|
use Cake\Utility\Text; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Configuration class for the uploader |
|
19
|
|
|
*/ |
|
20
|
|
|
class UploaderConfig |
|
21
|
|
|
{ |
|
22
|
|
|
/** @var string key used for CakePHP upload cache */ |
|
23
|
|
|
private const CACHE_KEY = 'uploadsThumbnails'; |
|
24
|
|
|
|
|
25
|
|
|
/** @var int max upload size in bytes */ |
|
26
|
|
|
private $defaultSize = 2000000; |
|
27
|
|
|
|
|
28
|
|
|
/** @var int max number of allowed uploads per user */ |
|
29
|
|
|
private $maxNumberOfUploads = 10; |
|
30
|
|
|
|
|
31
|
|
|
/** @var array allowed mime types */ |
|
32
|
|
|
private $types = []; |
|
33
|
|
|
|
|
34
|
|
|
/** @var int Threshold (and rough target) for resizing images in bytes */ |
|
35
|
|
|
private $defaultResize = 650000; |
|
36
|
|
|
|
|
37
|
|
|
/** @var int Jpeg compression factor between 0 and 100 */ |
|
38
|
|
|
private $jpegCompression = 92; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Set image compression quality |
|
42
|
|
|
* |
|
43
|
|
|
* @param int $quality Integer between 0 and 100 |
|
44
|
|
|
* @throws \InvalidArgumentException if quality isn't valid. |
|
45
|
|
|
* @return self |
|
46
|
|
|
*/ |
|
47
|
|
|
public function setImageCompressionQuality(int $quality): self |
|
48
|
|
|
{ |
|
49
|
|
|
if ($quality < 0 || $quality > 100) { |
|
50
|
|
|
throw new \InvalidArgumentException( |
|
51
|
|
|
"Image compression quality must be between 0 and 100.", |
|
52
|
|
|
1596204082 |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
$this->setJpegCompressionFactor($quality); |
|
56
|
|
|
|
|
57
|
|
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Set JPEG compression quality |
|
62
|
|
|
* |
|
63
|
|
|
* @param int $compression Number between 0 and 100 |
|
64
|
|
|
* @return self |
|
65
|
|
|
* @throws \InvalidArgumentException if compression factor isn't valid. |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function setJpegCompressionFactor(int $compression): self |
|
68
|
|
|
{ |
|
69
|
|
|
if ($compression < 0 || $compression > 100) { |
|
70
|
|
|
throw new \InvalidArgumentException( |
|
71
|
|
|
"Jpeg compression factor must be between 0 and 100.", |
|
72
|
|
|
1596187723 |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
$this->jpegCompression = $compression; |
|
76
|
|
|
|
|
77
|
|
|
return $this; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Get Jpeg compression factor |
|
82
|
|
|
* |
|
83
|
|
|
* @return int |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getJpegCompressionFactor(): int |
|
86
|
|
|
{ |
|
87
|
|
|
return $this->jpegCompression; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Set default max file size when resizing a type |
|
92
|
|
|
* |
|
93
|
|
|
* @param string|int $size Size in bytes as int or string e.g. "3MB" |
|
94
|
|
|
* @return self |
|
95
|
|
|
* @throws \InvalidArgumentException if size isn't valid. |
|
96
|
|
|
*/ |
|
97
|
|
|
public function setDefaultMaxResize($size): self |
|
98
|
|
|
{ |
|
99
|
|
|
if (is_string($size)) { |
|
100
|
|
|
$size = (int)Text::parseFileSize($size, $this->defaultResize); |
|
101
|
|
|
} |
|
102
|
|
|
if (!is_int($size)) { |
|
|
|
|
|
|
103
|
|
|
throw new \InvalidArgumentException( |
|
104
|
|
|
'Default max resize isn\'t a number.', |
|
105
|
|
|
1596199482 |
|
106
|
|
|
); |
|
107
|
|
|
} |
|
108
|
|
|
$this->defaultResize = $size; |
|
109
|
|
|
|
|
110
|
|
|
return $this; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Get max file size when resizing a type |
|
115
|
|
|
* |
|
116
|
|
|
* @return int Size in bytes |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getMaxResize(): int |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->defaultResize; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Sets max allowed uploads per user |
|
125
|
|
|
* |
|
126
|
|
|
* @param int $max Max uploads |
|
127
|
|
|
* @return self |
|
128
|
|
|
*/ |
|
129
|
|
|
public function setMaxNumberOfUploadsPerUser(int $max): self |
|
130
|
|
|
{ |
|
131
|
|
|
$this->maxNumberOfUploads = $max; |
|
132
|
|
|
|
|
133
|
|
|
return $this; |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Gets max allowed uploads per user |
|
138
|
|
|
* |
|
139
|
|
|
* @return int max uploads per user |
|
140
|
|
|
*/ |
|
141
|
|
|
public function getMaxNumberOfUploadsPerUser(): int |
|
142
|
|
|
{ |
|
143
|
|
|
return $this->maxNumberOfUploads; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
/** |
|
147
|
|
|
* Set default max file size |
|
148
|
|
|
* |
|
149
|
|
|
* @param string|int $number bytes as int or string e.g. "3MB" |
|
150
|
|
|
* @return self |
|
151
|
|
|
*/ |
|
152
|
|
|
public function setDefaultMaxFileSize($number): self |
|
153
|
|
|
{ |
|
154
|
|
|
if (is_string($number)) { |
|
155
|
|
|
$number = (int)Text::parseFileSize($number, $this->defaultSize); |
|
156
|
|
|
} |
|
157
|
|
|
if (!is_int($number)) { |
|
|
|
|
|
|
158
|
|
|
throw new \InvalidArgumentException( |
|
159
|
|
|
'Max upload size isn\'t a number.', |
|
160
|
|
|
1561364890 |
|
161
|
|
|
); |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
$this->defaultSize = $number; |
|
165
|
|
|
|
|
166
|
|
|
return $this; |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Adds a new mime type which is allowed to be uploaded |
|
171
|
|
|
* |
|
172
|
|
|
* @param string $type mime type as in "image/jpeg" |
|
173
|
|
|
* @param string|int|null $size optional file size different from default file size |
|
174
|
|
|
* @return self |
|
175
|
|
|
*/ |
|
176
|
|
|
public function addType(string $type, $size = null): self |
|
177
|
|
|
{ |
|
178
|
|
|
if ($size === null) { |
|
179
|
|
|
$size = $this->defaultSize; |
|
180
|
|
|
} elseif (is_string($size)) { |
|
181
|
|
|
$size = (int)Text::parseFileSize($size, $this->defaultSize); |
|
182
|
|
|
} |
|
183
|
|
|
if (!is_int($size)) { |
|
|
|
|
|
|
184
|
|
|
throw new \InvalidArgumentException( |
|
185
|
|
|
'Upload size isn\'t a number.', |
|
186
|
|
|
1561364891 |
|
187
|
|
|
); |
|
188
|
|
|
} |
|
189
|
|
|
$this->types[$type] = ['size' => $size ?: $this->defaultSize]; |
|
190
|
|
|
|
|
191
|
|
|
return $this; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Gets all allowed file types as array |
|
196
|
|
|
* |
|
197
|
|
|
* @return array file types |
|
198
|
|
|
*/ |
|
199
|
|
|
public function getAllTypes(): array |
|
200
|
|
|
{ |
|
201
|
|
|
return array_keys($this->types); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Gets file size for a a mime type |
|
206
|
|
|
* |
|
207
|
|
|
* @param string $type mime-type |
|
208
|
|
|
* @return int file-size in bytes |
|
209
|
|
|
* @throws \RuntimeException if mime-type isn't set |
|
210
|
|
|
*/ |
|
211
|
|
|
public function getSize(string $type): int |
|
212
|
|
|
{ |
|
213
|
|
|
if (!$this->hasType($type)) { |
|
214
|
|
|
throw new \RuntimeException( |
|
215
|
|
|
sprintf('Upload type %s not found.', $type), |
|
216
|
|
|
1561357996 |
|
217
|
|
|
); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
return $this->types[$type]['size']; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* Gets cache key |
|
225
|
|
|
* |
|
226
|
|
|
* @return string cache-key |
|
227
|
|
|
*/ |
|
228
|
|
|
public function getCacheKey(): string |
|
229
|
|
|
{ |
|
230
|
|
|
return self::CACHE_KEY; |
|
231
|
|
|
} |
|
232
|
|
|
|
|
233
|
|
|
/** |
|
234
|
|
|
* Checks if a mime-type is registered |
|
235
|
|
|
* |
|
236
|
|
|
* @param string $type mime-type |
|
237
|
|
|
* @return bool is type registered |
|
238
|
|
|
*/ |
|
239
|
|
|
public function hasType(string $type): bool |
|
240
|
|
|
{ |
|
241
|
|
|
return !empty($this->types[$type]); |
|
242
|
|
|
} |
|
243
|
|
|
} |
|
244
|
|
|
|