‪TYPO3CMS  10.4
FileDeletionAspect.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 
29 
39 {
41  {
42  $this->‪cleanupProcessedFiles($event->‪getFile());
43  }
44 
46  {
47  $this->‪cleanupProcessedFiles($event->‪getFile());
48  }
49 
51  {
52  $this->‪removeFromRepository($event->‪getFile());
53  }
54 
60  private function ‪removeFromRepository(‪FileInterface $fileObject)
61  {
62  // remove file from repository
63  if ($fileObject instanceof ‪File) {
64  $this->‪cleanupProcessedFiles($fileObject);
65  $this->‪cleanupCategoryReferences($fileObject);
66  $this->‪getFileIndexRepository()->remove($fileObject->getUid());
67  $this->‪getMetaDataRepository()->removeByFileUid($fileObject->getUid());
68 
69  // remove all references
70  GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_file_reference')
71  ->delete(
72  'sys_file_reference',
73  [
74  'uid_local' => (int)$fileObject->getUid(),
75  'table_local' => 'sys_file'
76  ]
77  );
78  } elseif ($fileObject instanceof ‪ProcessedFile) {
79  GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_file_processedfile')
80  ->delete(
81  'sys_file_processedfile',
82  [
83  'uid' => (int)$fileObject->getUid()
84  ]
85  );
86  }
87  }
88 
94  private function ‪cleanupCategoryReferences(‪File $fileObject)
95  {
96  // Retrieve the file metadata uid which is different from the file uid.
97  $metadataProperties = $fileObject->‪getMetaData()->‪get();
98  $metaDataUid = $metadataProperties['_ORIG_uid'] ?? $metadataProperties['uid'];
99 
100  GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_category_record_mm')
101  ->delete(
102  'sys_category_record_mm',
103  [
104  'uid_foreign' => (int)$metaDataUid,
105  'tablenames' => 'sys_file_metadata'
106  ]
107  );
108  }
109 
115  private function ‪cleanupProcessedFiles(‪FileInterface $fileObject)
116  {
117  // only delete processed files of File objects
118  if (!$fileObject instanceof ‪File) {
119  return;
120  }
121 
123  foreach ($this->‪getProcessedFileRepository()->findAllByOriginalFile($fileObject) as $processedFile) {
124  if ($processedFile->exists()) {
125  $processedFile->delete(true);
126  }
127  $this->‪removeFromRepository($processedFile);
128  }
129  }
130 
132  {
133  return GeneralUtility::makeInstance(FileIndexRepository::class);
134  }
135 
137  {
138  return GeneralUtility::makeInstance(MetaDataRepository::class);
139  }
140 
142  {
143  return GeneralUtility::makeInstance(ProcessedFileRepository::class);
144  }
145 }
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository
Definition: MetaDataRepository.php:40
‪TYPO3\CMS\Core\Resource\ProcessedFileRepository
Definition: ProcessedFileRepository.php:30
‪TYPO3\CMS\Core\Resource\File\getMetaData
‪MetaDataAspect getMetaData()
Definition: File.php:356
‪TYPO3\CMS\Core\Resource\Index\FileIndexRepository
Definition: FileIndexRepository.php:45
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:22
‪TYPO3\CMS\Core\Resource\Processing\FileDeletionAspect\removeFromRepositoryAfterFileDeleted
‪removeFromRepositoryAfterFileDeleted(AfterFileDeletedEvent $event)
Definition: FileDeletionAspect.php:50
‪TYPO3\CMS\Core\Resource\Processing\FileDeletionAspect\cleanupProcessedFiles
‪cleanupProcessedFiles(FileInterface $fileObject)
Definition: FileDeletionAspect.php:115
‪TYPO3\CMS\Core\Resource\Processing\FileDeletionAspect
Definition: FileDeletionAspect.php:39
‪TYPO3\CMS\Core\Resource\Event\AfterFileAddedEvent\getFile
‪getFile()
Definition: AfterFileAddedEvent.php:45
‪TYPO3\CMS\Core\Resource\Processing
Definition: AbstractGraphicalTask.php:16
‪TYPO3\CMS\Core\Resource\Processing\FileDeletionAspect\removeFromRepository
‪removeFromRepository(FileInterface $fileObject)
Definition: FileDeletionAspect.php:60
‪TYPO3\CMS\Core\Resource\Event\AfterFileDeletedEvent\getFile
‪getFile()
Definition: AfterFileDeletedEvent.php:39
‪TYPO3\CMS\Core\Resource\Processing\FileDeletionAspect\getFileIndexRepository
‪getFileIndexRepository()
Definition: FileDeletionAspect.php:131
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Resource\MetaDataAspect\get
‪array get()
Definition: MetaDataAspect.php:76
‪TYPO3\CMS\Core\Resource\Event\AfterFileReplacedEvent\getFile
‪getFile()
Definition: AfterFileReplacedEvent.php:43
‪TYPO3\CMS\Core\Resource\Processing\FileDeletionAspect\cleanupCategoryReferences
‪cleanupCategoryReferences(File $fileObject)
Definition: FileDeletionAspect.php:94
‪TYPO3\CMS\Core\Resource\Processing\FileDeletionAspect\getMetaDataRepository
‪getMetaDataRepository()
Definition: FileDeletionAspect.php:136
‪TYPO3\CMS\Core\Resource\Processing\FileDeletionAspect\cleanupProcessedFilesPostFileReplace
‪cleanupProcessedFilesPostFileReplace(AfterFileReplacedEvent $event)
Definition: FileDeletionAspect.php:45
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:44
‪TYPO3\CMS\Core\Resource\Processing\FileDeletionAspect\getProcessedFileRepository
‪getProcessedFileRepository()
Definition: FileDeletionAspect.php:141
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Resource\Event\AfterFileReplacedEvent
Definition: AfterFileReplacedEvent.php:28
‪TYPO3\CMS\Core\Resource\Event\AfterFileDeletedEvent
Definition: AfterFileDeletedEvent.php:29
‪TYPO3\CMS\Core\Resource\Processing\FileDeletionAspect\cleanupProcessedFilesPostFileAdd
‪cleanupProcessedFilesPostFileAdd(AfterFileAddedEvent $event)
Definition: FileDeletionAspect.php:40
‪TYPO3\CMS\Core\Resource\Event\AfterFileAddedEvent
Definition: AfterFileAddedEvent.php:30