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 Default target size for resizing a type in bytes */ |
35
|
|
|
private $defaultResize = 450000; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Set default max file size when resizing a type |
39
|
|
|
* |
40
|
|
|
* @param int $size Size in bytes |
41
|
|
|
* @return self |
42
|
|
|
*/ |
43
|
|
|
public function setDefaultMaxResize(int $size): self |
44
|
|
|
{ |
45
|
|
|
$this->defaultResize = $size; |
46
|
|
|
|
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Get max file size when resizing a type |
52
|
|
|
* |
53
|
|
|
* @return int Size in bytes |
54
|
|
|
*/ |
55
|
|
|
public function getMaxResize(): int |
56
|
|
|
{ |
57
|
|
|
return $this->defaultResize; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Sets max allowed uploads per user |
62
|
|
|
* |
63
|
|
|
* @param int $max Max uploads |
64
|
|
|
* @return self |
65
|
|
|
*/ |
66
|
|
|
public function setMaxNumberOfUploadsPerUser(int $max): self |
67
|
|
|
{ |
68
|
|
|
$this->maxNumberOfUploads = $max; |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Gets max allowed uploads per user |
75
|
|
|
* |
76
|
|
|
* @return int max uploads per user |
77
|
|
|
*/ |
78
|
|
|
public function getMaxNumberOfUploadsPerUser(): int |
79
|
|
|
{ |
80
|
|
|
return $this->maxNumberOfUploads; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Set default max file size |
85
|
|
|
* |
86
|
|
|
* @param string|int $number bytes as int or string e.g. "3MB" |
87
|
|
|
* @return self |
88
|
|
|
*/ |
89
|
|
|
public function setDefaultMaxFileSize($number): self |
90
|
|
|
{ |
91
|
|
|
if (is_string($number)) { |
92
|
|
|
$number = (int)Text::parseFileSize($number, $this->defaultSize); |
93
|
|
|
} |
94
|
|
|
if (!is_int($number)) { |
|
|
|
|
95
|
|
|
throw new \InvalidArgumentException( |
96
|
|
|
'Max upload size isn\'t a number.', |
97
|
|
|
1561364890 |
98
|
|
|
); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$this->defaultSize = $number; |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Adds a new mime type which is allowed to be uploaded |
108
|
|
|
* |
109
|
|
|
* @param string $type mime type as in "image/jpeg" |
110
|
|
|
* @param string|int|null $size optional file size different from default file size |
111
|
|
|
* @return self |
112
|
|
|
*/ |
113
|
|
|
public function addType(string $type, $size = null): self |
114
|
|
|
{ |
115
|
|
|
if ($size === null) { |
116
|
|
|
$size = $this->defaultSize; |
117
|
|
|
} elseif (is_string($size)) { |
118
|
|
|
$size = (int)Text::parseFileSize($size, $this->defaultSize); |
119
|
|
|
} |
120
|
|
|
if (!is_int($size)) { |
|
|
|
|
121
|
|
|
throw new \InvalidArgumentException( |
122
|
|
|
'Upload size isn\'t a number.', |
123
|
|
|
1561364891 |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
$this->types[$type] = ['size' => $size ?: $this->defaultSize]; |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Gets all allowed file types as array |
133
|
|
|
* |
134
|
|
|
* @return array file types |
135
|
|
|
*/ |
136
|
|
|
public function getAllTypes(): array |
137
|
|
|
{ |
138
|
|
|
return array_keys($this->types); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Gets file size for a a mime type |
143
|
|
|
* |
144
|
|
|
* @param string $type mime-type |
145
|
|
|
* @return int file-size in bytes |
146
|
|
|
* @throws \RuntimeException if mime-type isn't set |
147
|
|
|
*/ |
148
|
|
|
public function getSize(string $type): int |
149
|
|
|
{ |
150
|
|
|
if (!$this->hasType($type)) { |
151
|
|
|
throw new \RuntimeException( |
152
|
|
|
sprintf('Upload type %s not found.', $type), |
153
|
|
|
1561357996 |
154
|
|
|
); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
return $this->types[$type]['size']; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Gets cache key |
162
|
|
|
* |
163
|
|
|
* @return string cache-key |
164
|
|
|
*/ |
165
|
|
|
public function getCacheKey(): string |
166
|
|
|
{ |
167
|
|
|
return self::CACHE_KEY; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Checks if a mime-type is registered |
172
|
|
|
* |
173
|
|
|
* @param string $type mime-type |
174
|
|
|
* @return bool is type registered |
175
|
|
|
*/ |
176
|
|
|
public function hasType(string $type): bool |
177
|
|
|
{ |
178
|
|
|
return !empty($this->types[$type]); |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|