‪TYPO3CMS  9.5
Typo3tempFileService.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
17 use Symfony\Component\Finder\Finder;
18 use Symfony\Component\Finder\SplFileInfo;
23 
30 {
36  public function ‪getDirectoryStatistics(): array
37  {
38  return array_merge(
39  $this->‪statsFromTypo3temp(),
40  $this->‪statsFromStorages()
41  );
42  }
43 
50  protected function ‪statsFromTypo3temp(): array
51  {
52  $stats = [];
53  $typo3TempAssetsPath = '/typo3temp/assets/';
54  $basePath = ‪Environment::getPublicPath() . $typo3TempAssetsPath;
55  if (is_dir($basePath)) {
56  $dirFinder = new Finder();
57  $dirsInAssets = $dirFinder->directories()->in($basePath)->depth(0)->sortByName();
58  foreach ($dirsInAssets as $dirInAssets) {
60  $fileFinder = new Finder();
61  $fileCount = $fileFinder->files()->in($dirInAssets->getPathname())->count();
62  $folderName = $dirInAssets->getFilename();
63  $stat = [
64  'directory' => $typo3TempAssetsPath . $folderName,
65  'numberOfFiles' => $fileCount,
66  ];
67  if ($folderName === '_processed_') {
68  // The processed file storage for legacy files (eg. TCA type=group internal_type=file)
69  // gets the storageUid set, so this one can be removed via FAL functionality
70  $stat['storageUid'] = 0;
71  }
72  $stats[] = $stat;
73  }
74  }
75  return $stats;
76  }
77 
83  protected function ‪statsFromStorages(): array
84  {
85  $stats = [];
86  $processedFileRepository = GeneralUtility::makeInstance(ProcessedFileRepository::class);
87  $storages = GeneralUtility::makeInstance(StorageRepository::class)->findAll();
88  foreach ($storages as $storage) {
89  if ($storage->isOnline()) {
90  $storageConfiguration = $storage->getConfiguration();
91  $storageBasePath = rtrim($storageConfiguration['basePath'], '/');
92  $processedPath = '/' . $storageBasePath . $storage->getProcessingFolder()->getIdentifier();
93  $numberOfFiles = $processedFileRepository->countByStorage($storage);
94  $stats[] = [
95  'directory' => $processedPath,
96  'numberOfFiles' => $numberOfFiles,
97  'storageUid' => $storage->getUid()
98  ];
99  }
100  }
101  return $stats;
102  }
103 
110  public function ‪clearProcessedFiles(int $storageUid): int
111  {
112  $repository = GeneralUtility::makeInstance(ProcessedFileRepository::class);
113  return $repository->removeAll($storageUid);
114  }
115 
123  public function ‪clearAssetsFolder(string $folderName)
124  {
125  $basePath = ‪Environment::getPublicPath() . $folderName;
126  if (empty($folderName)
127  || !GeneralUtility::isAllowedAbsPath($basePath)
128  || strpos($folderName, '/typo3temp/assets/') !== 0
129  ) {
130  throw new \RuntimeException(
131  'Path to folder ' . $folderName . ' not allowed.',
132  1501781453
133  );
134  }
135  if (!is_dir($basePath)) {
136  throw new \RuntimeException(
137  'Folder path ' . $basePath . ' does not exist or is no directory.',
138  1501781454
139  );
140  }
141 
142  // first remove directories
143  foreach ((new Finder())->directories()->in($basePath)->depth(0) as $directory) {
145  GeneralUtility::rmdir($directory->getPathname(), true);
146  }
147 
148  // then remove files directly in the main dir
149  foreach ((new Finder())->files()->in($basePath)->depth(0) as $file) {
151  $path = $file->getPathname();
152  @unlink($path);
153  }
154  return true;
155  }
156 }
‪TYPO3\CMS\Core\Resource\ProcessedFileRepository
Definition: ProcessedFileRepository.php:29
‪TYPO3\CMS\Install\Service\Typo3tempFileService\statsFromTypo3temp
‪array statsFromTypo3temp()
Definition: Typo3tempFileService.php:50
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static string getPublicPath()
Definition: Environment.php:153
‪TYPO3\CMS\Install\Service\Typo3tempFileService
Definition: Typo3tempFileService.php:30
‪TYPO3\CMS\Install\Service\Typo3tempFileService\clearProcessedFiles
‪int clearProcessedFiles(int $storageUid)
Definition: Typo3tempFileService.php:110
‪TYPO3\CMS\Core\Resource\StorageRepository
Definition: StorageRepository.php:29
‪TYPO3\CMS\Install\Service\Typo3tempFileService\getDirectoryStatistics
‪array getDirectoryStatistics()
Definition: Typo3tempFileService.php:36
‪TYPO3\CMS\Install\Service\Typo3tempFileService\statsFromStorages
‪array statsFromStorages()
Definition: Typo3tempFileService.php:83
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:39
‪TYPO3\CMS\Install\Service\Typo3tempFileService\clearAssetsFolder
‪bool clearAssetsFolder(string $folderName)
Definition: Typo3tempFileService.php:123
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Install\Service
Definition: ClearCacheService.php:2