forked from schmitzal/tinyimg
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCompressImagesCommandController.php
More file actions
113 lines (103 loc) · 4.23 KB
/
CompressImagesCommandController.php
File metadata and controls
113 lines (103 loc) · 4.23 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
<?php
namespace Schmitzal\Tinyimg\Command;
use Schmitzal\Tinyimg\Domain\Model\FileStorage;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Resource\Processing\FileDeletionAspect;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\CommandController;
use TYPO3\CMS\Extbase\Persistence\QueryResultInterface;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManager;
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
/**
* Class CompressImagesCommandController
* @package Schmitzal\Tinyimg\Command
*/
class CompressImagesCommandController extends CommandController
{
/**
* @var \Schmitzal\Tinyimg\Domain\Repository\FileStorageRepository
* @inject
*/
protected $fileStorageRepository;
/**
* @var \Schmitzal\Tinyimg\Domain\Repository\FileRepository
* @inject
*/
protected $fileRepository;
/**
* @var \TYPO3\CMS\Core\Resource\ResourceFactory
* @inject
*/
protected $resourceFactory;
/**
* @var \Schmitzal\Tinyimg\Service\CompressImageService
* @inject
*/
protected $compressImageService;
/**
* Command: compress
* @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheGroupException
* @throws \TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException
* @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
*/
public function compressCommand()
{
$settings = $this->getTypoScriptConfiguration();
/** @var FileStorage $fileStorage */
foreach ($this->fileStorageRepository->findAll() as $fileStorage) {
$excludeFolders = GeneralUtility::trimExplode(',', (string)$settings['excludeFolders'], true);
$files = $this->fileRepository->findAllNonCompressedInStorageWithLimit($fileStorage, 100, $excludeFolders);
$this->compressImages($files);
$this->clearPageCache();
}
}
/**
* @param QueryResultInterface $files
* @return void
* @throws \TYPO3\CMS\Core\Resource\Exception\FileDoesNotExistException
* @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException
* @throws \TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException
*/
protected function compressImages(QueryResultInterface $files)
{
/** @var FileDeletionAspect $fileDeletionAspect */
$fileDeletionAspect = GeneralUtility::makeInstance(FileDeletionAspect::class);
/** @var \Schmitzal\Tinyimg\Domain\Model\File $file */
foreach ($files as $file) {
if ($file instanceof \Schmitzal\Tinyimg\Domain\Model\File) {
$file = $this->resourceFactory->getFileObject($file->getUid());
if (filesize(GeneralUtility::getFileAbsFileName(urldecode($file->getPublicUrl()))) > 0) {
$this->compressImageService->initializeCompression($file);
$fileDeletionAspect->cleanupProcessedFilesPostFileReplace($file, '');
}
}
}
}
/**
* Remove all processed files, so they get generated again after being compressed
* @throws \TYPO3\CMS\Core\Cache\Exception\NoSuchCacheGroupException
*/
protected function clearPageCache()
{
/** @var CacheManager $cacheManager */
$cacheManager = GeneralUtility::makeInstance(CacheManager::class);
$cacheManager->flushCachesInGroup('pages');
}
/**
* @return array
* @throws \TYPO3\CMS\Extbase\Configuration\Exception\InvalidConfigurationTypeException
*/
protected function getTypoScriptConfiguration(): array
{
/** @var ConfigurationManager $configurationManager */
$configurationManager = $this->objectManager->get(ConfigurationManager::class);
return (array)$configurationManager->getConfiguration(
ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
'tinyimg'
);
}
}