‪TYPO3CMS  ‪main
Typo3tempFileService.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
18 use Symfony\Component\Finder\Finder;
19 use Symfony\Component\Finder\SplFileInfo;
25 
32 {
35 
37  {
38  $this->processedFileRepository = ‪$processedFileRepository;
39  $this->storageRepository = ‪$storageRepository;
40  }
41 
45  public function ‪getDirectoryStatistics(): array
46  {
47  return array_merge(
48  $this->‪statsFromTypo3temp(),
49  $this->‪statsFromStorages()
50  );
51  }
52 
53  public function ‪getStatsFromStorageByUid(int $storageUid): array
54  {
55  if ($storageUid === 0) {
56  return $this->‪statsFromTypo3tempProcessed();
57  }
58 
59  $storage = $this->storageRepository->findByUid($storageUid);
60  return $this->‪getStatsFromStorage($storage);
61  }
62 
67  protected function ‪statsFromTypo3temp(): array
68  {
69  $stats = [];
70  $typo3TempAssetsPath = '/typo3temp/assets/';
71  $basePath = ‪Environment::getPublicPath() . $typo3TempAssetsPath;
72  if (is_dir($basePath)) {
73  $dirFinder = new Finder();
74  $dirsInAssets = $dirFinder->directories()->in($basePath)->depth(0)->sortByName();
75  foreach ($dirsInAssets as $dirInAssets) {
77  $fileFinder = new Finder();
78  $fileCount = $fileFinder->files()->in($dirInAssets->getPathname())->count();
79  $folderName = $dirInAssets->getFilename();
80  $stat = [
81  'directory' => $typo3TempAssetsPath . $folderName,
82  'numberOfFiles' => $fileCount,
83  ];
84  if ($folderName === '_processed_') {
85  // The processed file storage for legacy files (eg. TCA type=group internal_type=file)
86  // gets the storageUid set, so this one can be removed via FAL functionality
87  $stat['storageUid'] = 0;
88  }
89  $stats[] = $stat;
90  }
91  }
92  return $stats;
93  }
94 
98  protected function ‪statsFromTypo3tempProcessed(): array
99  {
100  $typo3TempProcessedAssetsPath = '/typo3temp/assets/_processed_/';
101 
102  $stats = [
103  'storageUid' => 0,
104  'directory' => $typo3TempProcessedAssetsPath,
105  ];
106 
107  $basePath = ‪Environment::getPublicPath() . $typo3TempProcessedAssetsPath;
108  if (is_dir($basePath)) {
109  $fileFinder = new Finder();
110  $stats['numberOfFiles'] = $fileFinder->files()->in($basePath)->count();
111  } else {
112  $stats['numberOfFiles'] = 0;
113  }
114 
115  return $stats;
116  }
117 
121  protected function ‪statsFromStorages(): array
122  {
123  $stats = [];
124  $storages = $this->storageRepository->findAll();
125  foreach ($storages as $storage) {
126  if ($storage->isOnline()) {
127  $stats[] = $this->‪getStatsFromStorage($storage);
128  }
129  }
130  return $stats;
131  }
132 
133  protected function ‪getStatsFromStorage(‪ResourceStorage $storage): array
134  {
135  $storageConfiguration = $storage->‪getConfiguration();
136  $storageBasePath = rtrim($storageConfiguration['basePath'], '/');
137  $processedPath = '/' . $storageBasePath . $storage->‪getProcessingFolder()->‪getIdentifier();
138  $numberOfFiles = $this->processedFileRepository->countByStorage($storage);
139 
140  return [
141  'directory' => $processedPath,
142  'numberOfFiles' => $numberOfFiles,
143  'storageUid' => $storage->‪getUid(),
144  ];
145  }
146 
153  public function ‪clearProcessedFiles(int $storageUid): int
154  {
155  $repository = GeneralUtility::makeInstance(ProcessedFileRepository::class);
156  return $repository->removeAll($storageUid);
157  }
158 
165  public function ‪clearAssetsFolder(string $folderName)
166  {
167  $basePath = ‪Environment::getPublicPath() . $folderName;
168  if (empty($folderName)
169  || !GeneralUtility::isAllowedAbsPath($basePath)
170  || !str_starts_with($folderName, '/typo3temp/assets/')
171  ) {
172  throw new \RuntimeException(
173  'Path to folder ' . $folderName . ' not allowed.',
174  1501781453
175  );
176  }
177  if (!is_dir($basePath)) {
178  throw new \RuntimeException(
179  'Folder path ' . $basePath . ' does not exist or is no directory.',
180  1501781454
181  );
182  }
183 
184  // first remove directories
185  foreach ((new Finder())->directories()->in($basePath)->depth(0) as $directory) {
187  ‪GeneralUtility::rmdir($directory->getPathname(), true);
188  }
189 
190  // then remove files directly in the main dir
191  foreach ((new Finder())->files()->in($basePath)->depth(0) as $file) {
193  $path = $file->getPathname();
194  @unlink($path);
195  }
196  return true;
197  }
198 }
‪TYPO3\CMS\Core\Resource\ProcessedFileRepository
Definition: ProcessedFileRepository.php:31
‪TYPO3\CMS\Core\Resource\ResourceStorage\getUid
‪int getUid()
Definition: ResourceStorage.php:328
‪TYPO3\CMS\Install\Service\Typo3tempFileService\$storageRepository
‪$storageRepository
Definition: Typo3tempFileService.php:34
‪TYPO3\CMS\Install\Service\Typo3tempFileService\$processedFileRepository
‪$processedFileRepository
Definition: Typo3tempFileService.php:33
‪TYPO3\CMS\Core\Resource\ResourceStorage\getProcessingFolder
‪Folder getProcessingFolder(File $file=null)
Definition: ResourceStorage.php:2688
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static getPublicPath()
Definition: Environment.php:187
‪TYPO3\CMS\Install\Service\Typo3tempFileService\getStatsFromStorageByUid
‪getStatsFromStorageByUid(int $storageUid)
Definition: Typo3tempFileService.php:53
‪TYPO3\CMS\Install\Service\Typo3tempFileService
Definition: Typo3tempFileService.php:32
‪TYPO3\CMS\Install\Service\Typo3tempFileService\clearProcessedFiles
‪int clearProcessedFiles(int $storageUid)
Definition: Typo3tempFileService.php:153
‪TYPO3\CMS\Install\Service\Typo3tempFileService\statsFromTypo3temp
‪statsFromTypo3temp()
Definition: Typo3tempFileService.php:67
‪TYPO3\CMS\Install\Service\Typo3tempFileService\__construct
‪__construct(ProcessedFileRepository $processedFileRepository, StorageRepository $storageRepository)
Definition: Typo3tempFileService.php:36
‪TYPO3\CMS\Core\Resource\StorageRepository
Definition: StorageRepository.php:38
‪TYPO3\CMS\Core\Resource\ResourceStorage\getConfiguration
‪array getConfiguration()
Definition: ResourceStorage.php:269
‪TYPO3\CMS\Install\Service\Typo3tempFileService\statsFromTypo3tempProcessed
‪statsFromTypo3tempProcessed()
Definition: Typo3tempFileService.php:98
‪TYPO3\CMS\Install\Service\Typo3tempFileService\getDirectoryStatistics
‪getDirectoryStatistics()
Definition: Typo3tempFileService.php:45
‪TYPO3\CMS\Install\Service\Typo3tempFileService\getStatsFromStorage
‪getStatsFromStorage(ResourceStorage $storage)
Definition: Typo3tempFileService.php:133
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:127
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Core\Utility\GeneralUtility\rmdir
‪static bool rmdir($path, $removeNonEmpty=false)
Definition: GeneralUtility.php:1806
‪TYPO3\CMS\Core\Resource\Folder\getIdentifier
‪string getIdentifier()
Definition: Folder.php:159
‪TYPO3\CMS\Install\Service\Typo3tempFileService\clearAssetsFolder
‪bool clearAssetsFolder(string $folderName)
Definition: Typo3tempFileService.php:165
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:51
‪TYPO3\CMS\Install\Service
Definition: ClearCacheService.php:16
‪TYPO3\CMS\Install\Service\Typo3tempFileService\statsFromStorages
‪statsFromStorages()
Definition: Typo3tempFileService.php:121