‪TYPO3CMS  9.5
FileDeletionAspect.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 
22 
33 {
39  protected function ‪getFileIndexRepository()
40  {
41  return GeneralUtility::makeInstance(\‪TYPO3\CMS\Core\Resource\Index\FileIndexRepository::class);
42  }
43 
49  protected function ‪getMetaDataRepository()
50  {
51  return GeneralUtility::makeInstance(\‪TYPO3\CMS\Core\Resource\Index\MetaDataRepository::class);
52  }
53 
59  protected function ‪getProcessedFileRepository()
60  {
61  return GeneralUtility::makeInstance(\‪TYPO3\CMS\Core\Resource\ProcessedFileRepository::class);
62  }
63 
69  public function ‪removeFromRepository(‪FileInterface $fileObject)
70  {
71  // remove file from repository
72  if ($fileObject instanceof ‪File) {
73  $this->‪cleanupProcessedFiles($fileObject);
74  $this->‪cleanupCategoryReferences($fileObject);
75  $this->‪getFileIndexRepository()->‪remove($fileObject->getUid());
76  $this->‪getMetaDataRepository()->removeByFileUid($fileObject->getUid());
77 
78  // remove all references
79  GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_file_reference')
80  ->delete(
81  'sys_file_reference',
82  [
83  'uid_local' => (int)$fileObject->getUid(),
84  'table_local' => 'sys_file'
85  ]
86  );
87  } elseif ($fileObject instanceof ‪ProcessedFile) {
88  GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_file_processedfile')
89  ->delete(
90  'sys_file_processedfile',
91  [
92  'uid' => (int)$fileObject->getUid()
93  ]
94  );
95  }
96  }
97 
104  public function ‪cleanupProcessedFilesPostFileAdd(‪FileInterface $file, $targetFolder)
105  {
106  $this->‪cleanupProcessedFiles($file);
107  }
108 
115  public function ‪cleanupProcessedFilesPostFileReplace(‪FileInterface $file, $localFilePath)
116  {
117  $this->‪cleanupProcessedFiles($file);
118  }
119 
125  protected function ‪cleanupCategoryReferences(‪File $fileObject)
126  {
127  // Retrieve the file metadata uid which is different from the file uid.
128  $metadataProperties = $fileObject->‪_getMetaData();
129  $metaDataUid = $metadataProperties['_ORIG_uid'] ?? $metadataProperties['uid'];
130 
131  GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_category_record_mm')
132  ->delete(
133  'sys_category_record_mm',
134  [
135  'uid_foreign' => (int)$metaDataUid,
136  'tablenames' => 'sys_file_metadata'
137  ]
138  );
139  }
140 
146  protected function ‪cleanupProcessedFiles(‪FileInterface $fileObject)
147  {
148  // only delete processed files of File objects
149  if (!$fileObject instanceof ‪File) {
150  return;
151  }
152 
154  foreach ($this->‪getProcessedFileRepository()->findAllByOriginalFile($fileObject) as $processedFile) {
155  if ($processedFile->exists()) {
156  $processedFile->delete(true);
157  }
158  $this->‪removeFromRepository($processedFile);
159  }
160  }
161 }
‪TYPO3\CMS\Core\Resource\Processing\FileDeletionAspect\cleanupProcessedFilesPostFileAdd
‪cleanupProcessedFilesPostFileAdd(FileInterface $file, $targetFolder)
Definition: FileDeletionAspect.php:104
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:21
‪TYPO3\CMS\Core\Resource\Processing\FileDeletionAspect\cleanupProcessedFiles
‪cleanupProcessedFiles(FileInterface $fileObject)
Definition: FileDeletionAspect.php:146
‪TYPO3
‪TYPO3\CMS\Core\Resource\Processing\FileDeletionAspect
Definition: FileDeletionAspect.php:33
‪TYPO3\CMS\Core\Resource\Processing\FileDeletionAspect\getProcessedFileRepository
‪TYPO3 CMS Core Resource ProcessedFileRepository getProcessedFileRepository()
Definition: FileDeletionAspect.php:59
‪TYPO3\CMS\Core\Resource\Processing\FileDeletionAspect\getFileIndexRepository
‪TYPO3 CMS Core Resource Index FileIndexRepository getFileIndexRepository()
Definition: FileDeletionAspect.php:39
‪TYPO3\CMS\Core\Resource\Processing\FileDeletionAspect\cleanupProcessedFilesPostFileReplace
‪cleanupProcessedFilesPostFileReplace(FileInterface $file, $localFilePath)
Definition: FileDeletionAspect.php:115
‪TYPO3\CMS\Core\Resource\Processing
Definition: AbstractGraphicalTask.php:2
‪TYPO3\CMS\Core\Resource\File\_getMetaData
‪array _getMetaData()
Definition: File.php:114
‪TYPO3\CMS\Core\Resource\Processing\FileDeletionAspect\removeFromRepository
‪removeFromRepository(FileInterface $fileObject)
Definition: FileDeletionAspect.php:69
‪TYPO3\CMS\Core\Resource\Index\FileIndexRepository\remove
‪remove($fileUid)
Definition: FileIndexRepository.php:534
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:23
‪TYPO3\CMS\Core\Resource\Processing\FileDeletionAspect\cleanupCategoryReferences
‪cleanupCategoryReferences(File $fileObject)
Definition: FileDeletionAspect.php:125
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:42
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:44
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Resource\Processing\FileDeletionAspect\getMetaDataRepository
‪TYPO3 CMS Core Resource Index MetaDataRepository getMetaDataRepository()
Definition: FileDeletionAspect.php:49