‪TYPO3CMS  10.4
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 
47  public function ‪getDirectoryStatistics(): array
48  {
49  return array_merge(
50  $this->‪statsFromTypo3temp(),
51  $this->‪statsFromStorages()
52  );
53  }
54 
55  public function ‪getStatsFromStorageByUid(int $storageUid): array
56  {
57  if ($storageUid === 0) {
58  return $this->‪statsFromTypo3tempProcessed();
59  }
60 
61  $storage = $this->storageRepository->findByUid($storageUid);
62  return $this->‪getStatsFromStorage($storage);
63  }
64 
71  protected function ‪statsFromTypo3temp(): array
72  {
73  $stats = [];
74  $typo3TempAssetsPath = '/typo3temp/assets/';
75  $basePath = ‪Environment::getPublicPath() . $typo3TempAssetsPath;
76  if (is_dir($basePath)) {
77  $dirFinder = new Finder();
78  $dirsInAssets = $dirFinder->directories()->in($basePath)->depth(0)->sortByName();
79  foreach ($dirsInAssets as $dirInAssets) {
81  $fileFinder = new Finder();
82  $fileCount = $fileFinder->files()->in($dirInAssets->getPathname())->count();
83  $folderName = $dirInAssets->getFilename();
84  $stat = [
85  'directory' => $typo3TempAssetsPath . $folderName,
86  'numberOfFiles' => $fileCount,
87  ];
88  if ($folderName === '_processed_') {
89  // The processed file storage for legacy files (eg. TCA type=group internal_type=file)
90  // gets the storageUid set, so this one can be removed via FAL functionality
91  $stat['storageUid'] = 0;
92  }
93  $stats[] = $stat;
94  }
95  }
96  return $stats;
97  }
98 
104  protected function ‪statsFromTypo3tempProcessed(): array
105  {
106  $typo3TempProcessedAssetsPath = '/typo3temp/assets/_processed_/';
107 
108  $stats = [
109  'storageUid' => 0,
110  'directory' => $typo3TempProcessedAssetsPath,
111  ];
112 
113  $basePath = ‪Environment::getPublicPath() . $typo3TempProcessedAssetsPath;
114  if (is_dir($basePath)) {
115  $fileFinder = new Finder();
116  $stats['numberOfFiles'] = $fileFinder->files()->in($basePath)->count();
117  } else {
118  $stats['numberOfFiles'] = 0;
119  }
120 
121  return $stats;
122  }
123 
129  protected function ‪statsFromStorages(): array
130  {
131  $stats = [];
132  $storages = $this->storageRepository->findAll();
133  foreach ($storages as $storage) {
134  if ($storage->isOnline()) {
135  $stats[] = $this->‪getStatsFromStorage($storage);
136  }
137  }
138  return $stats;
139  }
140 
141  protected function ‪getStatsFromStorage(‪ResourceStorage $storage): array
142  {
143  $storageConfiguration = $storage->‪getConfiguration();
144  $storageBasePath = rtrim($storageConfiguration['basePath'], '/');
145  $processedPath = '/' . $storageBasePath . $storage->‪getProcessingFolder()->‪getIdentifier();
146  $numberOfFiles = $this->processedFileRepository->countByStorage($storage);
147 
148  return [
149  'directory' => $processedPath,
150  'numberOfFiles' => $numberOfFiles,
151  'storageUid' => $storage->‪getUid()
152  ];
153  }
154 
161  public function ‪clearProcessedFiles(int $storageUid): int
162  {
163  $repository = GeneralUtility::makeInstance(ProcessedFileRepository::class);
164  return $repository->removeAll($storageUid);
165  }
166 
174  public function ‪clearAssetsFolder(string $folderName)
175  {
176  $basePath = ‪Environment::getPublicPath() . $folderName;
177  if (empty($folderName)
178  || !GeneralUtility::isAllowedAbsPath($basePath)
179  || strpos($folderName, '/typo3temp/assets/') !== 0
180  ) {
181  throw new \RuntimeException(
182  'Path to folder ' . $folderName . ' not allowed.',
183  1501781453
184  );
185  }
186  if (!is_dir($basePath)) {
187  throw new \RuntimeException(
188  'Folder path ' . $basePath . ' does not exist or is no directory.',
189  1501781454
190  );
191  }
192 
193  // first remove directories
194  foreach ((new Finder())->directories()->in($basePath)->depth(0) as $directory) {
196  ‪GeneralUtility::rmdir($directory->getPathname(), true);
197  }
198 
199  // then remove files directly in the main dir
200  foreach ((new Finder())->files()->in($basePath)->depth(0) as $file) {
202  $path = $file->getPathname();
203  @unlink($path);
204  }
205  return true;
206  }
207 }
‪TYPO3\CMS\Core\Resource\ProcessedFileRepository
Definition: ProcessedFileRepository.php:30
‪TYPO3\CMS\Install\Service\Typo3tempFileService\statsFromTypo3temp
‪array statsFromTypo3temp()
Definition: Typo3tempFileService.php:71
‪TYPO3\CMS\Core\Resource\ResourceStorage\getUid
‪int getUid()
Definition: ResourceStorage.php:321
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static string getPublicPath()
Definition: Environment.php:180
‪TYPO3\CMS\Install\Service\Typo3tempFileService\$storageRepository
‪$storageRepository
Definition: Typo3tempFileService.php:34
‪TYPO3\CMS\Install\Service\Typo3tempFileService\statsFromTypo3tempProcessed
‪array statsFromTypo3tempProcessed()
Definition: Typo3tempFileService.php:104
‪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:2738
‪TYPO3\CMS\Install\Service\Typo3tempFileService\getStatsFromStorageByUid
‪getStatsFromStorageByUid(int $storageUid)
Definition: Typo3tempFileService.php:55
‪TYPO3\CMS\Install\Service\Typo3tempFileService
Definition: Typo3tempFileService.php:32
‪TYPO3\CMS\Install\Service\Typo3tempFileService\clearProcessedFiles
‪int clearProcessedFiles(int $storageUid)
Definition: Typo3tempFileService.php:161
‪TYPO3\CMS\Install\Service\Typo3tempFileService\__construct
‪__construct(ProcessedFileRepository $processedFileRepository, StorageRepository $storageRepository)
Definition: Typo3tempFileService.php:36
‪TYPO3\CMS\Core\Resource\StorageRepository
Definition: StorageRepository.php:31
‪TYPO3\CMS\Core\Resource\ResourceStorage\getConfiguration
‪array getConfiguration()
Definition: ResourceStorage.php:259
‪TYPO3\CMS\Install\Service\Typo3tempFileService\getDirectoryStatistics
‪array getDirectoryStatistics()
Definition: Typo3tempFileService.php:47
‪TYPO3\CMS\Install\Service\Typo3tempFileService\getStatsFromStorage
‪getStatsFromStorage(ResourceStorage $storage)
Definition: Typo3tempFileService.php:141
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:122
‪TYPO3\CMS\Install\Service\Typo3tempFileService\statsFromStorages
‪array statsFromStorages()
Definition: Typo3tempFileService.php:129
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Core\Utility\GeneralUtility\rmdir
‪static bool rmdir($path, $removeNonEmpty=false)
Definition: GeneralUtility.php:2075
‪TYPO3\CMS\Core\Resource\Folder\getIdentifier
‪string getIdentifier()
Definition: Folder.php:160
‪TYPO3\CMS\Install\Service\Typo3tempFileService\clearAssetsFolder
‪bool clearAssetsFolder(string $folderName)
Definition: Typo3tempFileService.php:174
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Install\Service
Definition: ClearCacheService.php:16