TYPO3 CMS  TYPO3_8-7
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 = isset($metadataProperties['_ORIG_uid']) ? $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 }
static makeInstance($className,... $constructorArguments)
cleanupProcessedFilesPostFileReplace(FileInterface $file, $localFilePath)
cleanupProcessedFilesPostFileAdd(FileInterface $file, $targetFolder)