‪TYPO3CMS  10.4
ProcessedFileRepository.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 
18 use Psr\Log\LoggerAwareInterface;
19 use Psr\Log\LoggerAwareTrait;
24 
29 class ‪ProcessedFileRepository extends ‪AbstractRepository implements LoggerAwareInterface
30 {
31  use LoggerAwareTrait;
32 
40  protected ‪$objectType = ProcessedFile::class;
41 
48  protected ‪$table = 'sys_file_processedfile';
49 
56  protected ‪$tableColumns = [];
57 
61  public function ‪__construct()
62  {
63  parent::__construct();
64  }
65 
74  public function ‪createNewProcessedFileObject(‪FileInterface $originalFile, $taskType, array $configuration)
75  {
76  return GeneralUtility::makeInstance(
77  $this->objectType,
78  $originalFile,
79  $taskType,
80  $configuration
81  );
82  }
83 
88  protected function ‪createDomainObject(array $databaseRow)
89  {
90  $originalFile = $this->factory->getFileObject((int)$databaseRow['original']);
91  $originalFile->setStorage($this->factory->getStorageObject($originalFile->‪getProperty('storage')));
92  $taskType = $databaseRow['task_type'];
93  // Allow deserialization of Area class, since Area objects get serialized in configuration
94  // TODO: This should be changed to json encode and decode at some point
95  $configuration = unserialize(
96  $databaseRow['configuration'],
97  [
98  'allowed_classes' => [
99  Area::class,
100  ],
101  ]
102  );
103 
104  return GeneralUtility::makeInstance(
105  $this->objectType,
106  $originalFile,
107  $taskType,
108  $configuration,
109  $databaseRow
110  );
111  }
112 
119  public function ‪findByStorageAndIdentifier(‪ResourceStorage $storage, $identifier)
120  {
121  $processedFileObject = null;
122  if ($storage->‪hasFile($identifier)) {
123  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->table);
124  $databaseRow = $queryBuilder
125  ->select('*')
126  ->from($this->table)
127  ->where(
128  $queryBuilder->expr()->eq(
129  'storage',
130  $queryBuilder->createNamedParameter($storage->‪getUid(), \PDO::PARAM_INT)
131  ),
132  $queryBuilder->expr()->eq(
133  'identifier',
134  $queryBuilder->createNamedParameter($identifier, \PDO::PARAM_STR)
135  )
136  )
137  ->execute()
138  ->fetch();
139 
140  if ($databaseRow) {
141  $processedFileObject = $this->‪createDomainObject($databaseRow);
142  }
143  }
144  return $processedFileObject;
145  }
146 
154  public function ‪countByStorage(‪ResourceStorage $storage): int
155  {
156  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
157  ->getQueryBuilderForTable($this->table);
158  return (int)$queryBuilder
159  ->count('uid')
160  ->from($this->table)
161  ->where(
162  $queryBuilder->expr()->eq(
163  'storage',
164  $queryBuilder->createNamedParameter($storage->‪getUid(), \PDO::PARAM_INT)
165  )
166  )
167  ->execute()
168  ->fetchColumn(0);
169  }
170 
176  public function ‪add($processedFile)
177  {
178  if ($processedFile->isPersisted()) {
179  $this->‪update($processedFile);
180  } else {
181  $insertFields = $processedFile->toArray();
182  $insertFields['crdate'] = $insertFields['tstamp'] = time();
183  $insertFields = $this->‪cleanUnavailableColumns($insertFields);
184 
185  $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($this->table);
186 
187  $connection->insert(
188  $this->table,
189  $insertFields,
190  ['configuration' => ‪Connection::PARAM_LOB]
191  );
192 
193  $uid = $connection->lastInsertId($this->table);
194  $processedFile->updateProperties(['uid' => $uid]);
195  }
196  }
197 
203  public function ‪update($processedFile)
204  {
205  if ($processedFile->isPersisted()) {
206  $uid = (int)$processedFile->getUid();
207  $updateFields = $this->‪cleanUnavailableColumns($processedFile->toArray());
208  $updateFields['tstamp'] = time();
209 
210  $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($this->table);
211  $connection->update(
212  $this->table,
213  $updateFields,
214  [
215  'uid' => (int)$uid
216  ],
217  ['configuration' => ‪Connection::PARAM_LOB]
218  );
219  }
220  }
221 
229  public function ‪findOneByOriginalFileAndTaskTypeAndConfiguration(‪File $file, $taskType, array $configuration)
230  {
231  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->table);
232 
233  $databaseRow = $queryBuilder
234  ->select('*')
235  ->from($this->table)
236  ->where(
237  $queryBuilder->expr()->eq(
238  'original',
239  $queryBuilder->createNamedParameter($file->‪getUid(), \PDO::PARAM_INT)
240  ),
241  $queryBuilder->expr()->eq('task_type', $queryBuilder->createNamedParameter($taskType, \PDO::PARAM_STR)),
242  $queryBuilder->expr()->eq(
243  'configurationsha1',
244  $queryBuilder->createNamedParameter(sha1(serialize($configuration)), \PDO::PARAM_STR)
245  )
246  )
247  ->execute()
248  ->fetch();
249 
250  if (is_array($databaseRow)) {
251  $processedFile = $this->‪createDomainObject($databaseRow);
252  } else {
253  $processedFile = $this->‪createNewProcessedFileObject($file, $taskType, $configuration);
254  }
255  return $processedFile;
256  }
257 
263  public function ‪findAllByOriginalFile(‪FileInterface $file)
264  {
265  if (!$file instanceof ‪File) {
266  throw new \InvalidArgumentException('Parameter is no File object but got type "'
267  . (is_object($file) ? get_class($file) : gettype($file)) . '"', 1382006142);
268  }
269 
270  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)->getQueryBuilderForTable($this->table);
271  $result = $queryBuilder
272  ->select('*')
273  ->from($this->table)
274  ->where(
275  $queryBuilder->expr()->eq(
276  'original',
277  $queryBuilder->createNamedParameter($file->getUid(), \PDO::PARAM_INT)
278  )
279  )
280  ->execute();
281 
282  $itemList = [];
283  while ($row = $result->fetch()) {
284  $itemList[] = $this->‪createDomainObject($row);
285  }
286  return $itemList;
287  }
288 
296  public function ‪removeAll($storageUid = null)
297  {
298  $queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
299  ->getQueryBuilderForTable($this->table);
300  $where = [
301  $queryBuilder->expr()->neq('identifier', $queryBuilder->createNamedParameter('', \PDO::PARAM_STR))
302  ];
303  if ($storageUid !== null) {
304  $where[] = $queryBuilder->expr()->eq(
305  'storage',
306  $queryBuilder->createNamedParameter($storageUid, \PDO::PARAM_INT)
307  );
308  }
309  $result = $queryBuilder
310  ->select('*')
311  ->from($this->table)
312  ->where(...$where)
313  ->execute();
314 
315  $errorCount = 0;
316 
317  while ($row = $result->fetch()) {
318  if ($storageUid && (int)$storageUid !== (int)$row['storage']) {
319  continue;
320  }
321  try {
322  $file = $this->‪createDomainObject($row);
323  $file->‪getStorage()->‪setEvaluatePermissions(false);
324  $file->‪delete(true);
325  } catch (\‪Exception $e) {
326  $this->logger->error(
327  'Failed to delete file "' . $row['identifier'] . '" in storage uid ' . $row['storage'] . '.',
328  [
329  'exception' => $e
330  ]
331  );
332  ++$errorCount;
333  }
334  }
335 
336  if ($storageUid === null) {
337  // Truncate entire table if not restricted to specific storage
338  GeneralUtility::makeInstance(ConnectionPool::class)
339  ->getConnectionForTable($this->table)
340  ->truncate($this->table);
341  } else {
342  // else remove db rows of this storage only
343  GeneralUtility::makeInstance(ConnectionPool::class)
344  ->getConnectionForTable($this->table)
345  ->delete($this->table, ['storage' => $storageUid], [\PDO::PARAM_INT]);
346  }
347 
348  return $errorCount;
349  }
350 
358  protected function ‪cleanUnavailableColumns(array $data)
359  {
360  // As determining the table columns is a costly operation this is done only once during runtime and cached then
361  if (empty($this->tableColumns[$this->table])) {
362  $this->tableColumns[‪$this->table] = GeneralUtility::makeInstance(ConnectionPool::class)
363  ->getConnectionForTable($this->table)
364  ->getSchemaManager()
365  ->listTableColumns($this->table);
366  }
367 
368  return array_intersect_key($data, $this->tableColumns[$this->table]);
369  }
370 }
‪TYPO3\CMS\Core\Resource\ProcessedFileRepository
Definition: ProcessedFileRepository.php:30
‪TYPO3\CMS\Core\Resource\ResourceStorage\getUid
‪int getUid()
Definition: ResourceStorage.php:321
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:22
‪TYPO3\CMS\Core\Resource\ProcessedFileRepository\createNewProcessedFileObject
‪ProcessedFile createNewProcessedFileObject(FileInterface $originalFile, $taskType, array $configuration)
Definition: ProcessedFileRepository.php:71
‪TYPO3\CMS\Core\Resource\ProcessedFileRepository\update
‪update($processedFile)
Definition: ProcessedFileRepository.php:200
‪TYPO3\CMS\Core\Resource\AbstractRepository
Definition: AbstractRepository.php:31
‪TYPO3\CMS\Core\Imaging\ImageManipulation\Area
Definition: Area.php:23
‪TYPO3\CMS\Core\Resource\AbstractFile\setStorage
‪File setStorage(ResourceStorage $storage)
Definition: AbstractFile.php:421
‪TYPO3\CMS\Core\Resource\ProcessedFileRepository\findAllByOriginalFile
‪ProcessedFile[] findAllByOriginalFile(FileInterface $file)
Definition: ProcessedFileRepository.php:260
‪TYPO3\CMS\Core\Resource\ProcessedFileRepository\$objectType
‪string $objectType
Definition: ProcessedFileRepository.php:39
‪TYPO3\CMS\Core\Resource\ResourceInterface\getStorage
‪ResourceStorage getStorage()
‪TYPO3\CMS\Core\Resource\ProcessedFileRepository\createDomainObject
‪ProcessedFile createDomainObject(array $databaseRow)
Definition: ProcessedFileRepository.php:85
‪TYPO3\CMS\Core\Resource\ProcessedFileRepository\cleanUnavailableColumns
‪array cleanUnavailableColumns(array $data)
Definition: ProcessedFileRepository.php:355
‪TYPO3\CMS\Core\Resource\FileInterface\delete
‪bool delete()
‪TYPO3\CMS\Core\Resource\ResourceStorage\hasFile
‪bool hasFile($identifier)
Definition: ResourceStorage.php:1658
‪TYPO3\CMS\Core\Resource\ProcessedFileRepository\add
‪add($processedFile)
Definition: ProcessedFileRepository.php:173
‪TYPO3\CMS\Core\Resource\FileInterface\getProperty
‪string getProperty($key)
‪TYPO3\CMS\Core\Resource\ResourceStorage\setEvaluatePermissions
‪setEvaluatePermissions($evaluatePermissions)
Definition: ResourceStorage.php:632
‪TYPO3\CMS\Core\Resource\ProcessedFileRepository\$table
‪string $table
Definition: ProcessedFileRepository.php:46
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Core\Resource\ProcessedFileRepository\$tableColumns
‪array $tableColumns
Definition: ProcessedFileRepository.php:53
‪TYPO3\CMS\Core\Resource\ProcessedFileRepository\__construct
‪__construct()
Definition: ProcessedFileRepository.php:58
‪TYPO3\CMS\Core\Resource\AbstractFile\getUid
‪int getUid()
Definition: AbstractFile.php:202
‪TYPO3\CMS\Core\Resource
Definition: generateMimeTypes.php:52
‪TYPO3\CMS\Core\Resource\ProcessedFile
Definition: ProcessedFile.php:44
‪TYPO3\CMS\Core\Resource\AbstractRepository\removeAll
‪removeAll()
Definition: AbstractRepository.php:179
‪TYPO3\CMS\Core\Resource\Exception
Definition: Exception.php:22
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:36
‪TYPO3\CMS\Core\Resource\ResourceStorage
Definition: ResourceStorage.php:122
‪TYPO3\CMS\Core\Resource\ProcessedFileRepository\removeAll
‪int removeAll($storageUid=null)
Definition: ProcessedFileRepository.php:293
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Resource\ProcessedFileRepository\countByStorage
‪int countByStorage(ResourceStorage $storage)
Definition: ProcessedFileRepository.php:151
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Resource\ProcessedFileRepository\findOneByOriginalFileAndTaskTypeAndConfiguration
‪ProcessedFile findOneByOriginalFileAndTaskTypeAndConfiguration(File $file, $taskType, array $configuration)
Definition: ProcessedFileRepository.php:226
‪TYPO3\CMS\Core\Resource\ProcessedFileRepository\findByStorageAndIdentifier
‪ProcessedFile null findByStorageAndIdentifier(ResourceStorage $storage, $identifier)
Definition: ProcessedFileRepository.php:116
‪TYPO3\CMS\Core\Database\Connection\PARAM_LOB
‪const PARAM_LOB
Definition: Connection.php:57