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