/
MediaCommandController.php
482 lines (433 loc) · 20.4 KB
/
MediaCommandController.php
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
<?php
declare(strict_types=1);
namespace Neos\Media\Command;
/*
* This file is part of the Neos.Media package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Doctrine\Common\Collections\Collection;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Neos\Flow\Annotations as Flow;
use Neos\Flow\Cli\CommandController;
use Neos\Flow\Persistence\Exception\IllegalObjectTypeException;
use Neos\Flow\Persistence\PersistenceManagerInterface;
use Neos\Flow\Reflection\ReflectionService;
use Neos\Flow\ResourceManagement\PersistentResource;
use Neos\Media\Domain\Model\Asset;
use Neos\Media\Domain\Model\AssetCollection;
use Neos\Media\Domain\Model\AssetInterface;
use Neos\Media\Domain\Model\AssetSource\AssetSourceAwareInterface;
use Neos\Media\Domain\Model\Tag;
use Neos\Media\Domain\Model\VariantSupportInterface;
use Neos\Media\Domain\Repository\AssetRepository;
use Neos\Media\Domain\Repository\ThumbnailRepository;
use Neos\Media\Domain\Service\AssetVariantGenerator;
use Neos\Media\Domain\Service\ThumbnailService;
use Neos\Media\Domain\Strategy\AssetModelMappingStrategyInterface;
use Neos\Media\Exception\AssetServiceException;
use Neos\Media\Exception\AssetVariantGeneratorException;
use Neos\Media\Exception\ThumbnailServiceException;
use Neos\Utility\Arrays;
use Neos\Utility\Files;
/**
* @Flow\Scope("singleton")
*/
class MediaCommandController extends CommandController
{
/**
* @Flow\Inject
* @var PersistenceManagerInterface
*/
protected $persistenceManager;
/**
* @Flow\Inject(lazy=false)
* @var EntityManagerInterface
*/
protected $entityManager;
/**
* @var Connection
*/
protected $dbalConnection;
/**
* @Flow\Inject
* @var AssetRepository
*/
protected $assetRepository;
/**
* @Flow\Inject
* @var ThumbnailRepository
*/
protected $thumbnailRepository;
/**
* @Flow\Inject
* @var ThumbnailService
*/
protected $thumbnailService;
/**
* @Flow\InjectConfiguration("asyncThumbnails")
* @var bool
*/
protected $asyncThumbnails;
/**
* @Flow\Inject
* @var AssetModelMappingStrategyInterface
*/
protected $mappingStrategy;
/**
* @Flow\Inject
* @var ReflectionService
*/
protected $reflectionService;
/**
* @Flow\Inject
* @var AssetVariantGenerator
*/
protected $assetVariantGenerator;
/**
* Import resources to asset management
*
* This command detects Flow "PersistentResource"s which are not yet available as "Asset" objects and thus don't appear
* in the asset management. The type of the imported asset is determined by the file extension provided by the
* PersistentResource.
*
* @param bool $simulate If set, this command will only tell what it would do instead of doing it right away
* @param bool $quiet
* @return void
* @throws IllegalObjectTypeException
* @throws DBALException
*/
public function importResourcesCommand(bool $simulate = false, bool $quiet = false)
{
$this->initializeConnection();
$sql = '
SELECT
r.persistence_object_identifier, r.filename, r.mediatype
FROM neos_flow_resourcemanagement_persistentresource r
LEFT JOIN neos_media_domain_model_asset a
ON a.resource = r.persistence_object_identifier
LEFT JOIN neos_media_domain_model_thumbnail t
ON t.resource = r.persistence_object_identifier
WHERE a.persistence_object_identifier IS NULL AND t.persistence_object_identifier IS NULL
';
$statement = $this->dbalConnection->prepare($sql);
$statement->execute();
$resourceInfos = $statement->fetchAll();
if ($resourceInfos === []) {
!$quiet || $this->outputLine('Found no resources which need to be imported.');
$this->quit();
}
foreach ($resourceInfos as $resourceInfo) {
$resource = $this->persistenceManager->getObjectByIdentifier($resourceInfo['persistence_object_identifier'], PersistentResource::class);
if ($resource === null) {
!$quiet || $this->outputLine('Warning: PersistentResource for file "%s" seems to be corrupt. No resource object with identifier %s could be retrieved from the Persistence Manager.', [$resourceInfo['filename'], $resourceInfo['persistence_object_identifier']]);
continue;
}
if (!$resource->getStream()) {
!$quiet || $this->outputLine('Warning: PersistentResource for file "%s" seems to be corrupt. The actual data of resource %s could not be found in the resource storage.', [$resourceInfo['filename'], $resourceInfo['persistence_object_identifier']]);
continue;
}
$className = $this->mappingStrategy->map($resource, $resourceInfos);
$resourceObj = new $className($resource);
if ($simulate) {
$this->outputLine('Simulate: Adding new resource "%s" (type: %s)', [$resourceObj->getResource()->getFilename(), $className]);
} else {
$this->assetRepository->add($resourceObj);
!$quiet || $this->outputLine('Adding new resource "%s" (type: %s)', [$resourceObj->getResource()->getFilename(), $className]);
}
}
}
/**
* Remove unused assets
*
* This command iterates over all existing assets, checks their usage count and lists the assets which are not
* reported as used by any AssetUsageStrategies. The unused assets can than be removed.
*
* @param string $assetSource If specified, only assets of this asset source are considered. For example "neos" or "my-asset-management-system"
* @param bool $quiet If set, only errors will be displayed.
* @param bool $assumeYes If set, "yes" is assumed for the "shall I remove ..." dialogs
* @param string $onlyTags Comma-separated list of asset tag labels, that should be taken into account
* @param int $limit Limit the result of unused assets displayed and removed for this run.
* @param string $onlyCollections Comma-separated list of asset collection titles, that should be taken into account
* @return void
* @throws IllegalObjectTypeException
* @throws AssetServiceException
*/
public function removeUnusedCommand(string $assetSource = '', bool $quiet = false, bool $assumeYes = false, string $onlyTags = '', int $limit = null, string $onlyCollections = ''): void
{
$iterator = $this->assetRepository->findAllIterator();
$assetCount = $this->assetRepository->countAll();
$unusedAssets = [];
$tableRowsByAssetSource = [];
$unusedAssetCount = 0;
$unusedAssetsTotalSize = 0;
$filterByAssetSourceIdentifier = $assetSource;
if ($filterByAssetSourceIdentifier === '') {
!$quiet && $this->outputLine('<b>Searching for unused assets in all asset sources:</b>');
} else {
!$quiet && $this->outputLine('<b>Searching for unused assets of asset source "%s":</b>', [$filterByAssetSourceIdentifier]);
}
$assetTagsMatchFilterTags = function (Collection $assetTags, string $filterTags): bool {
$filterTagValues = Arrays::trimExplode(',', $filterTags);
$assetTagValues = [];
foreach ($assetTags as $tag) {
/** @var Tag $tag */
$assetTagValues[] = $tag->getLabel();
}
return count(array_intersect($filterTagValues, $assetTagValues)) > 0;
};
$assetCollectionsMatchFilterCollections = static function (Collection $assetCollections, string $filterCollections): bool {
$filterCollectionValues = Arrays::trimExplode(',', $filterCollections);
$assetCollectionValues = [];
foreach ($assetCollections as $assetCollection) {
/** @var AssetCollection $assetCollection */
$assetCollectionValues[] = $assetCollection->getTitle();
}
return count(array_intersect($filterCollectionValues, $assetCollectionValues)) > 0;
};
!$quiet && $this->output->progressStart($assetCount);
/** @var Asset $asset */
foreach ($this->assetRepository->iterate($iterator) as $asset) {
!$quiet && $this->output->progressAdvance(1);
if ($limit !== null && $unusedAssetCount === $limit) {
break;
}
if (!$asset instanceof Asset) {
continue;
}
if (!$asset instanceof AssetSourceAwareInterface) {
continue;
}
if ($filterByAssetSourceIdentifier !== '' && $asset->getAssetSourceIdentifier() !== $filterByAssetSourceIdentifier) {
continue;
}
if ($onlyCollections !== '' && !$assetCollectionsMatchFilterCollections($asset->getAssetCollections(), $onlyCollections)) {
continue;
}
if ($onlyTags !== '' && $assetTagsMatchFilterTags($asset->getTags(), $onlyTags) === false) {
continue;
}
if ($asset->getUsageCount() !== 0) {
continue;
}
$fileSize = str_pad(Files::bytesToSizeString($asset->getResource()->getFileSize()), 9, ' ', STR_PAD_LEFT);
$unusedAssets[] = $asset;
$tableRowsByAssetSource[$asset->getAssetSourceIdentifier()][] = [
$asset->getIdentifier(),
$asset->getResource()->getFilename(),
$fileSize
];
$unusedAssetCount++;
$unusedAssetsTotalSize += $asset->getResource()->getFileSize();
}
!$quiet && $this->output->progressFinish();
if ($unusedAssetCount === 0) {
!$quiet && $this->output->outputLine(PHP_EOL . 'No unused assets found.');
exit;
}
foreach ($tableRowsByAssetSource as $assetSourceIdentifier => $tableRows) {
!$quiet && $this->outputLine(PHP_EOL . 'Found the following unused assets from asset source <success>%s</success>: ' . PHP_EOL, [$assetSourceIdentifier]);
!$quiet && $this->output->outputTable(
$tableRows,
['Asset identifier', 'Filename', 'Size']
);
}
!$quiet && $this->outputLine(PHP_EOL . 'Total size of unused assets: %s' . PHP_EOL, [Files::bytesToSizeString($unusedAssetsTotalSize)]);
if ($assumeYes === false) {
if (!$this->output->askConfirmation(sprintf('Do you want to remove <b>%s</b> unused assets?', $unusedAssetCount))) {
exit(1);
}
}
!$quiet && $this->output->progressStart($unusedAssetCount);
foreach ($unusedAssets as $asset) {
!$quiet && $this->output->progressAdvance(1);
$this->assetRepository->remove($asset);
}
!$quiet && $this->output->progressFinish();
!$quiet && $this->output->outputLine();
}
/**
* Create thumbnails
*
* Creates thumbnail images based on the configured thumbnail presets. Optional ``preset`` parameter to only create
* thumbnails for a specific thumbnail preset configuration.
*
* Additionally accepts a ``async`` parameter determining if the created thumbnails are generated when created.
*
* @param string $preset Preset name, if not provided thumbnails are created for all presets
* @param bool $async Asynchronous generation, if not provided the setting ``Neos.Media.asyncThumbnails`` is used
* @param bool $quiet If set, only errors will be displayed.
* @return void
* @throws ThumbnailServiceException
*/
public function createThumbnailsCommand(string $preset = null, bool $async = null, bool $quiet = false)
{
$async = $async ?? $this->asyncThumbnails;
$presets = $preset !== null ? [$preset] : array_keys($this->thumbnailService->getPresets());
$presetThumbnailConfigurations = [];
foreach ($presets as $preset) {
$presetThumbnailConfigurations[] = $this->thumbnailService->getThumbnailConfigurationForPreset($preset, $async);
}
$iterator = $this->assetRepository->findAllIterator();
$imageCount = $this->assetRepository->countAll();
!$quiet && $this->output->progressStart($imageCount * count($presetThumbnailConfigurations));
foreach ($this->assetRepository->iterate($iterator) as $image) {
foreach ($presetThumbnailConfigurations as $presetThumbnailConfiguration) {
$this->thumbnailService->getThumbnail($image, $presetThumbnailConfiguration);
$this->persistenceManager->persistAll();
!$quiet && $this->output->progressAdvance(1);
}
}
!$quiet && $this->output->progressFinish();
!$quiet && $this->output->outputLine();
}
/**
* Remove thumbnails
*
* Removes all thumbnail objects and their resources. Optional ``preset`` parameter to only remove thumbnails
* matching a specific thumbnail preset configuration.
*
* @param string $preset Preset name, if provided only thumbnails matching that preset are cleared
* @param bool $quiet If set, only errors will be displayed.
* @return void
* @throws IllegalObjectTypeException
* @throws ThumbnailServiceException
*/
public function clearThumbnailsCommand(string $preset = null, bool $quiet = false)
{
if ($preset !== null) {
$thumbnailConfiguration = $this->thumbnailService->getThumbnailConfigurationForPreset($preset);
$thumbnailConfigurationHash = $thumbnailConfiguration->getHash();
$thumbnailCount = $this->thumbnailRepository->countByConfigurationHash($thumbnailConfigurationHash);
$iterator = $this->thumbnailRepository->findAllIterator($thumbnailConfigurationHash);
} else {
$thumbnailCount = $this->thumbnailRepository->countAll();
$iterator = $this->thumbnailRepository->findAllIterator();
}
!$quiet && $this->output->progressStart($thumbnailCount);
foreach ($this->thumbnailRepository->iterate($iterator) as $thumbnail) {
$this->thumbnailRepository->remove($thumbnail);
!$quiet && $this->output->progressAdvance(1);
}
!$quiet && $this->output->progressFinish();
!$quiet && $this->output->outputLine();
}
/**
* Render ungenerated thumbnails
*
* Loops over ungenerated thumbnails and renders them. Optional ``limit`` parameter to limit the amount of
* thumbnails to be rendered to avoid memory exhaustion.
*
* @param integer $limit Limit the amount of thumbnails to be rendered to avoid memory exhaustion
* @param bool $quiet If set, only errors will be displayed.
* @return void
*/
public function renderThumbnailsCommand(int $limit = null, bool $quiet = false)
{
$thumbnailCount = $this->thumbnailRepository->countUngenerated();
$iterator = $this->thumbnailRepository->findUngeneratedIterator();
!$quiet && $this->output->progressStart($limit !== null && $thumbnailCount > $limit ? $limit : $thumbnailCount);
$iteration = 0;
foreach ($this->thumbnailRepository->iterate($iterator) as $thumbnail) {
if ($thumbnail->getResource() === null) {
$this->thumbnailService->refreshThumbnail($thumbnail);
$this->persistenceManager->persistAll();
}
!$quiet && $this->output->progressAdvance(1);
if (++$iteration === $limit) {
break;
}
}
}
/**
* Render asset variants
*
* Loops over missing configured asset variants and renders them. Optional ``limit`` parameter to
* limit the amount of variants to be rendered to avoid memory exhaustion.
*
* If the re-render parameter is given, any existing variants will be rendered again, too.
*
* @param integer $limit Limit the amount of variants to be rendered to avoid memory exhaustion
* @param bool $quiet If set, only errors will be displayed.
* @param bool $recreate If set, existing asset variants will be re-generated and replaced
* @return void
* @throws AssetVariantGeneratorException
* @throws IllegalObjectTypeException
*/
public function renderVariantsCommand($limit = null, bool $quiet = false, bool $recreate = false): void
{
$resultMessage = null;
$generatedVariants = 0;
$configuredVariantsCount = 0;
$configuredPresets = $this->assetVariantGenerator->getVariantPresets();
foreach ($configuredPresets as $configuredPreset) {
$configuredVariantsCount += count($configuredPreset->variants());
}
if ($configuredVariantsCount === 0) {
$this->outputLine('There are no image variant presets configured, exiting…');
$this->quit();
}
$classNames = $this->reflectionService->getAllImplementationClassNamesForInterface(VariantSupportInterface::class);
foreach ($classNames as $className) {
/** @var AssetRepository $repository */
$repositoryClassName = $this->reflectionService->getClassSchema($className)->getRepositoryClassName();
$repository = $this->objectManager->get($repositoryClassName);
if (!method_exists($repository, 'findAssetIdentifiersWithVariants')) {
!$quiet && $this->outputLine('Repository %s does not provide findAssetIdentifiersWithVariants(), skipping…', [$repositoryClassName]);
continue;
}
$assetCount = $repository->countAll();
$variantCount = $configuredVariantsCount * $assetCount;
!$quiet && $this->outputLine('Checking up to %u variants for %s for existence…', [$variantCount, $className]);
!$quiet && $this->output->progressStart($variantCount);
$currentAsset = null;
/** @var AssetInterface $currentAsset */
foreach ($repository->findAssetIdentifiersWithVariants() as $assetIdentifier => $assetVariants) {
foreach ($configuredPresets as $presetIdentifier => $preset) {
foreach ($preset->variants() as $presetVariantName => $presetVariant) {
if ($recreate || !isset($assetVariants[$presetIdentifier][$presetVariantName])) {
$currentAsset = $repository->findByIdentifier($assetIdentifier);
$createdVariant = $recreate ? $this->assetVariantGenerator->recreateVariant($currentAsset, $presetIdentifier, $presetVariantName) : $this->assetVariantGenerator->createVariant($currentAsset, $presetIdentifier, $presetVariantName);
if ($createdVariant !== null) {
$repository->update($currentAsset);
if (++$generatedVariants % 10 === 0) {
$this->persistenceManager->persistAll();
}
if ($generatedVariants === $limit) {
$resultMessage = sprintf('Generated %u variants, exiting after reaching limit', $limit);
!$quiet && $this->output->progressFinish();
break 3;
}
}
}
!$quiet && $this->output->progressAdvance(1);
}
}
}
!$quiet && $this->output->progressFinish();
}
!$quiet && $this->outputLine();
!$quiet && $this->outputLine($resultMessage ?? sprintf('Generated %u variants', $generatedVariants));
}
/**
* Initializes the DBAL connection which is currently bound to the Doctrine Entity Manager
*
* @return void
*/
protected function initializeConnection(): void
{
if (!$this->entityManager instanceof EntityManager) {
$this->outputLine('This command only supports database connections provided by the Doctrine ORM Entity Manager.
However, the current entity manager is an instance of %s.', [get_class($this->entityManager)]);
$this->quit(1);
}
$this->dbalConnection = $this->entityManager->getConnection();
}
}