-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathImageVersionShell.php
More file actions
367 lines (335 loc) · 12.4 KB
/
ImageVersionShell.php
File metadata and controls
367 lines (335 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
<?php
declare(strict_types = 1);
namespace Burzum\FileStorage\Shell;
use Cake\Console\ConsoleOptionParser;
use Cake\Console\Shell;
use Cake\Core\Configure;
use Cake\Datasource\ResultSetInterface;
use Cake\Event\Event;
use Cake\Event\EventManager;
use Cake\ORM\TableRegistry;
use Exception;
/**
* ImageShell
*
* @author Florian Krämer
* @copyright 2012 - 2020 Florian Krämer
* @license MIT
*/
class ImageVersionShell extends Shell
{
/**
* Storage Table Object
*
* @var \Cake\ORM\Table
*/
public $Table;
/**
* Limit
*
* @var int
*/
public $limit = 10;
/**
* @inheritDoc
*/
public function getOptionParser(): ConsoleOptionParser
{
$parser = parent::getOptionParser();
$parser->setDescription([
__d('file_storage', 'Shell command for generating and removing image versions.'),
]);
$parser->addOption('storageTable', [
'short' => 's',
'help' => __d('file_storage', 'The storage table for image processing you want to use.'),
'default' => 'Burzum/FileStorage.FileStorage',
]);
$parser->addOption('limit', [
'short' => 'l',
'help' => __d('file_storage', 'Limits the amount of records to be processed in one batch'),
]);
$parser->addSubcommands([
'generate' => [
'help' => __d('file_storage', '<model> <identifier> Generate a new image version'),
'parser' => [
'arguments' => [
'model' => [
'help' => __d('file_storage', 'Value of the model property of the images to generate'),
'required' => true,
],
'identifier' => [
'help' => __d('file_storage', 'The image identifier (`model` field in `file_storage` table).'),
'required' => true,
],
],
'options' => [
'adapter' => [
'short' => 'a',
'help' => __('The adapter config name to use.'),
'default' => 'Local',
],
'storageTable' => [
'short' => 's',
'help' => __d('file_storage', 'The storage table for image processing you want to use.'),
'default' => 'Burzum/FileStorage.FileStorage',
],
'limit' => [
'short' => 'l',
'help' => __d('file_storage', 'Limits the amount of records to be processed in one batch'),
],
'keep-old-versions' => [
'short' => 'k',
'help' => __d('file_storage', 'Use this switch if you do not want to overwrite existing versions.'),
'boolean' => true,
],
],
],
],
'remove' => [
'help' => __d('file_storage', '<model> <version> Remove an new image version'),
'parser' => [
'arguments' => [
'model' => [
'help' => __d('file_storage', 'Value of the model property of the images to remove'),
'required' => true,
],
'version' => [
'help' => __d('file_storage', 'Image version to remove'),
'required' => true,
],
],
'options' => [
'adapter' => [
'short' => 'a',
'help' => __('The adapter config name to use.'),
'default' => 'Local',
],
'storageTable' => [
'short' => 's',
'help' => __d('file_storage', 'The storage table for image processing you want to use.'),
'default' => 'Burzum/FileStorage.FileStorage',
],
'limit' => [
'short' => 'l',
'help' => __d('file_storage', 'Limits the amount of records to be processed in one batch'),
],
],
],
],
'regenerate' => [
'help' => __d('file_storage', '<model> Generates all image versions.'),
'parser' => [
'arguments' => [
'model' => [
'help' => __d('file_storage', 'Value of the model property of the images to generate'),
'required' => true,
],
],
'options' => [
'adapter' => [
'short' => 'a',
'help' => __('The adapter config name to use.'),
'default' => 'Local',
],
'storageTable' => [
'short' => 's',
'help' => __d('file_storage', 'The storage table for image processing you want to use.'),
'default' => 'Burzum/FileStorage.FileStorage',
],
'limit' => [
'short' => 'l',
'help' => __d('file_storage', 'Limits the amount of records to be processed in one batch'),
],
'keep-old-versions' => [
'short' => 'k',
'help' => __d('file_storage', 'Use this switch if you do not want to overwrite existing versions.'),
'boolean' => true,
],
],
],
],
]);
return $parser;
}
/**
* @inheritDoc
*/
public function startup(): void
{
parent::startup();
$storageTable = $this->params['storageTable'];
try {
$this->Table = TableRegistry::getTableLocator()->get($storageTable);
} catch (Exception $e) {
$this->abort($e->getMessage());
}
if (isset($this->params['limit'])) {
if (!is_numeric($this->params['limit'])) {
$this->abort(__d('file_storage', '--limit must be an integer!'));
}
$this->limit = (int)$this->params['limit'];
}
}
/**
* Generate all image versions.
*
* @return void
*/
public function regenerate(): void
{
$operations = Configure::read('FileStorage.imageSizes.' . $this->args[0]);
$options = [
'overwrite' => !$this->params['keep-old-versions'],
];
if (empty($operations)) {
$this->abort(__d('file_storage', 'Invalid table or version.'));
}
foreach ($operations as $version => $operation) {
try {
if ($this->command === null) {
$this->abort('No command given');
}
$this->_loop($this->command, $this->args[0], [$version => $operation], $options);
} catch (Exception $e) {
$this->abort($e->getMessage());
}
}
}
/**
* Generate a given image version.
*
* @param string $model
* @param string $version
*
* @return void
*/
public function generate(string $model, string $version): void
{
$operations = Configure::read('FileStorage.imageVariants.' . $model . '.' . $version);
$options = [
'overwrite' => !$this->params['keep-old-versions'],
];
if (empty($operations)) {
$this->out(__d('file_storage', 'Invalid table or version.'));
$this->_stop();
}
try {
$this->_loop('generate', $model, [$version => $operations], $options);
} catch (Exception $e) {
$this->abort($e->getMessage());
}
}
/**
* Remove a given image version.
*
* @param string $model
* @param string $version
*
* @return void
*/
public function remove(string $model, string $version): void
{
$operations = Configure::read('FileStorage.imageSizes.' . $model . '.' . $version);
if (empty($operations)) {
$this->out(__d('file_storage', 'Invalid table or version.'));
$this->_stop();
}
try {
$this->_loop('remove', $model, [$version => $operations]);
} catch (Exception $e) {
$this->out($e->getMessage());
$this->_stop();
}
}
/**
* Loops through image records and performs requested operation on them.
*
* @param string $action
* @param string $model
* @param array $operations
* @param array $options
*
* @return void
*/
protected function _loop(string $action, $model, array $operations = [], array $options = []): void
{
if (!in_array($action, ['generate', 'remove', 'regenerate'])) {
$this->_stop();
}
$totalImageCount = $this->_getCount($model);
if ($totalImageCount === 0) {
$this->out(__d('file_storage', 'No Images for model {0} found', $model));
$this->_stop();
}
$this->out(__d('file_storage', '{0} image file(s) will be processed' . "\n", $totalImageCount));
$offset = 0;
$limit = $this->limit;
/** @var \Phauthentic\Infrastructure\Storage\FileStorage|null $storage */
$storage = Configure::read('FileStorage.behaviorConfig.fileStorage');
if (!$storage) {
$this->abort(sprintf('Invalid adapter config `%s` provided!', $this->params['adapter']));
}
$adapter = $storage->getStorage($this->params['adapter']);
do {
$images = $this->_getRecords($model, $limit, $offset);
if ($images->count()) {
foreach ($images as $image) {
$payload = [
'entity' => $image,
'storage' => $adapter,
'operations' => $operations,
'versions' => array_keys($operations),
'table' => $this->Table,
'options' => $options,
];
if ($action === 'generate' || $action === 'regenerate') {
$Event = new Event('ImageVersion.createVersion', $this->Table, $payload);
EventManager::instance()->dispatch($Event);
}
if ($action === 'remove') {
$Event = new Event('ImageVersion.removeVersion', $this->Table, $payload);
EventManager::instance()->dispatch($Event);
}
$this->out(__('{0} processed', $image->id));
}
}
$offset += $limit;
} while ($images->count() > 0);
}
/**
* Gets the amount of images for a model in the DB.
*
* @param string $identifier
* @param array $extensions
*
* @return int
*/
protected function _getCount(string $identifier, array $extensions = ['jpg', 'png', 'jpeg']): int
{
return $this->Table
->find()
->where(['model' => $identifier])
->andWhere(['extension IN' => $extensions])
->count();
}
/**
* Gets the chunk of records for the image processing
*
* @param string $identifier
* @param int $limit
* @param int $offset
* @param array $extensions
*
* @return \Cake\Datasource\ResultSetInterface
*/
protected function _getRecords(string $identifier, int $limit, int $offset, array $extensions = ['jpg', 'png', 'jpeg']): ResultSetInterface
{
return $this->Table
->find()
->where(['model' => $identifier])
->andWhere(['extension IN' => $extensions])
->limit($limit)
->offset($offset)
->all();
}
}