‪TYPO3CMS  ‪main
FileDeletionAspect.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 
32 
42 {
43  #[AsEventListener('delete-processed-files-after-add')]
45  {
46  $this->‪cleanupProcessedFiles($event->‪getFile());
47  }
48 
49  #[AsEventListener('delete-processed-files-after-replace')]
51  {
52  $this->‪cleanupProcessedFiles($event->‪getFile());
53  }
54 
55  #[AsEventListener('delete-processed-files-after-delete')]
57  {
58  $this->‪removeFromRepository($event->‪getFile());
59  }
60 
64  private function ‪removeFromRepository(‪FileInterface $fileObject): void
65  {
66  // remove file from repository
67  if ($fileObject instanceof ‪File) {
68  $this->‪cleanupProcessedFiles($fileObject);
69  $this->‪cleanupCategoryReferences($fileObject);
70  $this->‪getFileIndexRepository()->remove($fileObject->getUid());
71  $this->‪getMetaDataRepository()->removeByFileUid($fileObject->getUid());
72 
73  // remove all references
74  GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_file_reference')
75  ->delete(
76  'sys_file_reference',
77  [
78  'uid_local' => $fileObject->getUid(),
79  ]
80  );
81  } elseif ($fileObject instanceof ‪ProcessedFile) {
82  GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_file_processedfile')
83  ->delete(
84  'sys_file_processedfile',
85  [
86  'uid' => $fileObject->getUid(),
87  ]
88  );
89  }
90  }
91 
95  private function ‪cleanupCategoryReferences(‪File $fileObject): void
96  {
97  // Retrieve the file metadata uid which is different from the file uid.
98  $metadataProperties = $fileObject->‪getMetaData()->get();
99  $metaDataUid = (int)($metadataProperties['_ORIG_uid'] ?? $metadataProperties['uid'] ?? 0);
100 
101  if ($metaDataUid <= 0) {
102  // No metadata record exists for the given file. The file might not
103  // have been indexed or the meta data record was deleted manually.
104  return;
105  }
106 
107  GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_category_record_mm')
108  ->delete(
109  'sys_category_record_mm',
110  [
111  'uid_foreign' => $metaDataUid,
112  'tablenames' => 'sys_file_metadata',
113  ]
114  );
115  }
116 
120  private function ‪cleanupProcessedFiles(‪FileInterface $fileObject): void
121  {
122  // only delete processed files of File objects
123  if (!$fileObject instanceof ‪File) {
124  return;
125  }
126 
127  foreach ($this->‪getProcessedFileRepository()->findAllByOriginalFile($fileObject) as $processedFile) {
128  if ($processedFile->exists()) {
129  $processedFile->delete(true);
130  }
131  $this->‪removeFromRepository($processedFile);
132  }
133  }
134 
136  {
137  return GeneralUtility::makeInstance(FileIndexRepository::class);
138  }
139 
141  {
142  return GeneralUtility::makeInstance(MetaDataRepository::class);
143  }
144 
146  {
147  return GeneralUtility::makeInstance(ProcessedFileRepository::class);
148  }
149 }
‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository
Definition: MetaDataRepository.php:40
‪TYPO3\CMS\Core\Resource\ProcessedFileRepository
Definition: ProcessedFileRepository.php:39
‪TYPO3\CMS\Core\Resource\Index\FileIndexRepository
Definition: FileIndexRepository.php:44
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:26
‪TYPO3\CMS\Core\Resource\Processing\FileDeletionAspect\removeFromRepositoryAfterFileDeleted
‪removeFromRepositoryAfterFileDeleted(AfterFileDeletedEvent $event)
Definition: FileDeletionAspect.php:56
‪TYPO3\CMS\Core\Resource\Processing\FileDeletionAspect\cleanupProcessedFiles
‪cleanupProcessedFiles(FileInterface $fileObject)
Definition: FileDeletionAspect.php:120
‪TYPO3\CMS\Core\Attribute\AsEventListener
Definition: AsEventListener.php:25
‪TYPO3\CMS\Core\Resource\Processing\FileDeletionAspect
Definition: FileDeletionAspect.php:42
‪TYPO3\CMS\Core\Resource\Event\AfterFileAddedEvent\getFile
‪getFile()
Definition: AfterFileAddedEvent.php:33
‪TYPO3\CMS\Core\Resource\Processing
Definition: AbstractTask.php:18
‪TYPO3\CMS\Core\Resource\Processing\FileDeletionAspect\removeFromRepository
‪removeFromRepository(FileInterface $fileObject)
Definition: FileDeletionAspect.php:64
‪TYPO3\CMS\Core\Resource\Event\AfterFileDeletedEvent\getFile
‪getFile()
Definition: AfterFileDeletedEvent.php:32
‪TYPO3\CMS\Core\Resource\Processing\FileDeletionAspect\getFileIndexRepository
‪getFileIndexRepository()
Definition: FileDeletionAspect.php:135
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:26
‪TYPO3\CMS\Core\Resource\Event\AfterFileReplacedEvent\getFile
‪getFile()
Definition: AfterFileReplacedEvent.php:31
‪TYPO3\CMS\Core\Resource\Processing\FileDeletionAspect\cleanupCategoryReferences
‪cleanupCategoryReferences(File $fileObject)
Definition: FileDeletionAspect.php:95
‪TYPO3\CMS\Core\Resource\Processing\FileDeletionAspect\getMetaDataRepository
‪getMetaDataRepository()
Definition: FileDeletionAspect.php:140
‪TYPO3\CMS\Core\Resource\Processing\FileDeletionAspect\cleanupProcessedFilesPostFileReplace
‪cleanupProcessedFilesPostFileReplace(AfterFileReplacedEvent $event)
Definition: FileDeletionAspect.php:50
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:47
‪TYPO3\CMS\Core\Resource\File\getMetaData
‪getMetaData()
Definition: File.php:322
‪TYPO3\CMS\Core\Resource\Processing\FileDeletionAspect\getProcessedFileRepository
‪getProcessedFileRepository()
Definition: FileDeletionAspect.php:145
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪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:44
‪TYPO3\CMS\Core\Resource\Event\AfterFileAddedEvent
Definition: AfterFileAddedEvent.php:30