TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
FileDeletionAspect.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Core\Resource\Processing;
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 
70  public function removeFromRepository(FileInterface $fileObject)
71  {
72  // remove file from repository
73  if ($fileObject instanceof File) {
74  $this->cleanupProcessedFiles($fileObject);
75  $this->cleanupCategoryReferences($fileObject);
76  $this->getFileIndexRepository()->remove($fileObject->getUid());
77  $this->getMetaDataRepository()->removeByFileUid($fileObject->getUid());
78 
79  // remove all references
80  GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_file_reference')
81  ->delete(
82  'sys_file_reference',
83  [
84  'uid_local' => (int)$fileObject->getUid(),
85  'table_local' => 'sys_file'
86  ]
87  );
88  } elseif ($fileObject instanceof ProcessedFile) {
89  GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_file_processedfile')
90  ->delete(
91  'sys_file_processedfile',
92  [
93  'uid' => (int)$fileObject->getUid()
94  ]
95  );
96  }
97  }
98 
105  public function cleanupProcessedFilesPostFileAdd(FileInterface $file, $targetFolder)
106  {
107  $this->cleanupProcessedFiles($file);
108  }
109 
116  public function cleanupProcessedFilesPostFileReplace(FileInterface $file, $localFilePath)
117  {
118  $this->cleanupProcessedFiles($file);
119  }
120 
127  protected function cleanupCategoryReferences(File $fileObject)
128  {
129  // Retrieve the file metadata uid which is different from the file uid.
130  $metadataProperties = $fileObject->_getMetaData();
131  $metaDataUid = isset($metadataProperties['_ORIG_uid']) ? $metadataProperties['_ORIG_uid'] : $metadataProperties['uid'];
132 
133  GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_category_record_mm')
134  ->delete(
135  'sys_category_record_mm',
136  [
137  'uid_foreign' => (int)$metaDataUid,
138  'tablenames' => 'sys_file_metadata'
139  ]
140  );
141  }
142 
149  protected function cleanupProcessedFiles(FileInterface $fileObject)
150  {
151  // only delete processed files of File objects
152  if (!$fileObject instanceof File) {
153  return;
154  }
155 
157  foreach ($this->getProcessedFileRepository()->findAllByOriginalFile($fileObject) as $processedFile) {
158  if ($processedFile->exists()) {
159  $processedFile->delete(true);
160  }
161  $this->removeFromRepository($processedFile);
162  }
163  }
164 }
cleanupProcessedFilesPostFileReplace(FileInterface $file, $localFilePath)
cleanupProcessedFilesPostFileAdd(FileInterface $file, $targetFolder)
static makeInstance($className,...$constructorArguments)