‪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 {
33  public function ‪__construct(
34  private readonly ‪ProcessedFileRepository $processedFileRepository,
35  private readonly ‪StorageRepository $storageRepository
36  ) {}
37 
41  public function ‪getDirectoryStatistics(): array
42  {
43  return array_merge(
44  $this->‪statsFromTypo3temp(),
45  $this->‪statsFromStorages()
46  );
47  }
48 
49  public function ‪getStatsFromStorageByUid(int $storageUid): array
50  {
51  if ($storageUid === 0) {
52  return $this->‪statsFromTypo3tempProcessed();
53  }
54 
55  $storage = $this->storageRepository->findByUid($storageUid);
56  return $this->‪getStatsFromStorage($storage);
57  }
58 
63  protected function ‪statsFromTypo3temp(): array
64  {
65  $stats = [];
66  $typo3TempAssetsPath = '/typo3temp/assets/';
67  $basePath = ‪Environment::getPublicPath() . $typo3TempAssetsPath;
68  if (is_dir($basePath)) {
69  $dirFinder = new Finder();
70  $dirsInAssets = $dirFinder->directories()->in($basePath)->depth(0)->sortByName();
71  foreach ($dirsInAssets as $dirInAssets) {
73  $fileFinder = new Finder();
74  $fileCount = $fileFinder->files()->in($dirInAssets->getPathname())->count();
75  $folderName = $dirInAssets->getFilename();
76  $stat = [
77  'directory' => $typo3TempAssetsPath . $folderName,
78  'numberOfFiles' => $fileCount,
79  ];
80  if ($folderName === '_processed_') {
81  // The processed file storage for legacy files (eg. TCA type=group internal_type=file)
82  // gets the storageUid set, so this one can be removed via FAL functionality
83  $stat['storageUid'] = 0;
84  }
85  $stats[] = $stat;
86  }
87  }
88  return $stats;
89  }
90 
94  protected function ‪statsFromTypo3tempProcessed(): array
95  {
96  $typo3TempProcessedAssetsPath = '/typo3temp/assets/_processed_/';
97 
98  $stats = [
99  'storageUid' => 0,
100  'directory' => $typo3TempProcessedAssetsPath,
101  ];
102 
103  $basePath = ‪Environment::getPublicPath() . $typo3TempProcessedAssetsPath;
104  if (is_dir($basePath)) {
105  $fileFinder = new Finder();
106  $stats['numberOfFiles'] = $fileFinder->files()->in($basePath)->count();
107  } else {
108  $stats['numberOfFiles'] = 0;
109  }
110 
111  return $stats;
112  }
113 
117  protected function ‪statsFromStorages(): array
118  {
119  $stats = [];
120  $storages = $this->storageRepository->findAll();
121  foreach ($storages as $storage) {
122  if ($storage->isOnline()) {
123  $stats[] = $this->‪getStatsFromStorage($storage);
124  }
125  }
126  return $stats;
127  }
128 
129  protected function ‪getStatsFromStorage(‪ResourceStorage $storage): array
130  {
131  $storageConfiguration = $storage->‪getConfiguration();
132  $storageBasePath = rtrim($storageConfiguration['basePath'], '/');
133  $processedPath = '/' . $storageBasePath . $storage->‪getProcessingFolder()->‪getIdentifier();
134  $numberOfFiles = $this->processedFileRepository->countByStorage($storage);
135 
136  return [
137  'directory' => $processedPath,
138  'numberOfFiles' => $numberOfFiles,
139  'storageUid' => $storage->‪getUid(),
140  ];
141  }
142 
149  public function ‪clearProcessedFiles(int $storageUid): int
150  {
151  return $this->processedFileRepository->removeAll($storageUid);
152  }
153 
160  public function ‪clearAssetsFolder(string $folderName)
161  {
162  $basePath = ‪Environment::getPublicPath() . $folderName;
163  if (empty($folderName)
164  || !GeneralUtility::isAllowedAbsPath($basePath)
165  || !str_starts_with($folderName, '/typo3temp/assets/')
166  ) {
167  throw new \RuntimeException(
168  'Path to folder ' . $folderName . ' not allowed.',
169  1501781453
170  );
171  }
172  if (!is_dir($basePath)) {
173  throw new \RuntimeException(
174  'Folder path ' . $basePath . ' does not exist or is no directory.',
175  1501781454
176  );
177  }
178 
179  // first remove directories
180  foreach ((new Finder())->directories()->in($basePath)->depth(0) as $directory) {
182  ‪GeneralUtility::rmdir($directory->getPathname(), true);
183  }
184 
185  // then remove files directly in the main dir
186  foreach ((new Finder())->files()->in($basePath)->depth(0) as $file) {
188  $path = $file->getPathname();
189  @unlink($path);
190  }
191  return true;
192  }
193 }
‪TYPO3\CMS\Core\Resource\ProcessedFileRepository
Definition: ProcessedFileRepository.php:39
‪TYPO3\CMS\Core\Resource\ResourceStorage\getUid
‪int getUid()
Definition: ResourceStorage.php:338
‪TYPO3\CMS\Core\Resource\ResourceStorage\getProcessingFolder
‪Folder getProcessingFolder(File $file=null)
Definition: ResourceStorage.php:2774
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static getPublicPath()
Definition: Environment.php:187
‪TYPO3\CMS\Install\Service\Typo3tempFileService\getStatsFromStorageByUid
‪getStatsFromStorageByUid(int $storageUid)
Definition: Typo3tempFileService.php:49
‪TYPO3\CMS\Install\Service\Typo3tempFileService
Definition: Typo3tempFileService.php:32
‪TYPO3\CMS\Install\Service\Typo3tempFileService\clearProcessedFiles
‪int clearProcessedFiles(int $storageUid)
Definition: Typo3tempFileService.php:149
‪TYPO3\CMS\Install\Service\Typo3tempFileService\statsFromTypo3temp
‪statsFromTypo3temp()
Definition: Typo3tempFileService.php:63
‪TYPO3\CMS\Install\Service\Typo3tempFileService\__construct
‪__construct(private readonly ProcessedFileRepository $processedFileRepository, private readonly StorageRepository $storageRepository)
Definition: Typo3tempFileService.php:33
‪TYPO3\CMS\Core\Resource\StorageRepository
Definition: StorageRepository.php:38
‪TYPO3\CMS\Core\Utility\GeneralUtility\rmdir
‪static bool rmdir(string $path, bool $removeNonEmpty=false)
Definition: GeneralUtility.php:1702
‪TYPO3\CMS\Core\Resource\ResourceStorage\getConfiguration
‪array getConfiguration()
Definition: ResourceStorage.php:279
‪TYPO3\CMS\Install\Service\Typo3tempFileService\statsFromTypo3tempProcessed
‪statsFromTypo3tempProcessed()
Definition: Typo3tempFileService.php:94
‪TYPO3\CMS\Install\Service\Typo3tempFileService\getDirectoryStatistics
‪getDirectoryStatistics()
Definition: Typo3tempFileService.php:41
‪TYPO3\CMS\Install\Service\Typo3tempFileService\getStatsFromStorage
‪getStatsFromStorage(ResourceStorage $storage)
Definition: Typo3tempFileService.php:129
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:129
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Core\Resource\Folder\getIdentifier
‪non empty string getIdentifier()
Definition: Folder.php:150
‪TYPO3\CMS\Install\Service\Typo3tempFileService\clearAssetsFolder
‪bool clearAssetsFolder(string $folderName)
Definition: Typo3tempFileService.php:160
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Install\Service
Definition: ClearCacheService.php:16
‪TYPO3\CMS\Install\Service\Typo3tempFileService\statsFromStorages
‪statsFromStorages()
Definition: Typo3tempFileService.php:117