‪TYPO3CMS  10.4
DeleteUploadsFinisher.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
23 
32 {
33 
38  protected function ‪executeInternal()
39  {
40  $formRuntime = $this->finisherContext->getFormRuntime();
41 
42  $uploadFolders = [];
43  $elements = $formRuntime->getFormDefinition()->getRenderablesRecursively();
44  foreach ($elements as $element) {
45  if (!$element instanceof ‪FileUpload) {
46  continue;
47  }
48  $file = $formRuntime[$element->getIdentifier()];
49  if (!$file) {
50  continue;
51  }
52 
53  if ($file instanceof ‪FileReference) {
54  $file = $file->getOriginalResource();
55  }
56 
57  $folder = $file->getParentFolder();
58  $uploadFolders[$folder->getCombinedIdentifier()] = $folder;
59 
60  $file->getStorage()->deleteFile($file->getOriginalFile());
61  }
62 
63  $this->‪deleteEmptyUploadFolders($uploadFolders);
64  }
65 
72  protected function ‪deleteEmptyUploadFolders(array $folders): void
73  {
74  foreach ($folders as $folder) {
75  if ($this->‪isEmptyFolder($folder)) {
76  $folder->delete();
77  }
78  }
79  }
80 
85  protected function ‪isEmptyFolder(‪Folder $folder): bool
86  {
87  return $folder->‪getFileCount() === 0
88  && $folder->‪getStorage()->‪countFoldersInFolder($folder) === 0;
89  }
90 }
‪TYPO3\CMS\Form\Domain\Finishers\DeleteUploadsFinisher\isEmptyFolder
‪bool isEmptyFolder(Folder $folder)
Definition: DeleteUploadsFinisher.php:85
‪TYPO3\CMS\Form\Domain\Finishers\DeleteUploadsFinisher
Definition: DeleteUploadsFinisher.php:32
‪TYPO3\CMS\Core\Resource\Folder\getFileCount
‪int getFileCount(array $filterMethods=[], $recursive=false)
Definition: Folder.php:261
‪TYPO3\CMS\Form\Domain\Finishers\DeleteUploadsFinisher\executeInternal
‪executeInternal()
Definition: DeleteUploadsFinisher.php:38
‪TYPO3\CMS\Form\Domain\Finishers
Definition: AbstractFinisher.php:22
‪TYPO3\CMS\Core\Resource\Folder\getStorage
‪ResourceStorage getStorage()
Definition: Folder.php:149
‪TYPO3\CMS\Form\Domain\Finishers\DeleteUploadsFinisher\deleteEmptyUploadFolders
‪deleteEmptyUploadFolders(array $folders)
Definition: DeleteUploadsFinisher.php:72
‪TYPO3\CMS\Form\Domain\Finishers\AbstractFinisher
Definition: AbstractFinisher.php:41
‪TYPO3\CMS\Core\Resource\Folder
Definition: Folder.php:37
‪TYPO3\CMS\Extbase\Domain\Model\FileReference
Definition: FileReference.php:28
‪TYPO3\CMS\Core\Resource\ResourceStorage\countFoldersInFolder
‪int countFoldersInFolder(Folder $folder, $useFilters=true, $recursive=false)
Definition: ResourceStorage.php:2439
‪TYPO3\CMS\Form\Domain\Model\FormElements\FileUpload
Definition: FileUpload.php:28