Skip to content

Commit 26a37f4

Browse files
committed
[TASK] Improve code readability and enhance support for file renaming
1 parent 34cce06 commit 26a37f4

2 files changed

Lines changed: 37 additions & 8 deletions

File tree

Classes/Controller/CloudinaryWebHookController.php

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use TYPO3\CMS\Core\Resource\ResourceFactory;
2424
use TYPO3\CMS\Core\Resource\ResourceStorage;
2525
use TYPO3\CMS\Core\Utility\GeneralUtility;
26+
use TYPO3\CMS\Core\Utility\PathUtility;
2627
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
2728
use Visol\Cloudinary\Events\ClearCachePageEvent;
2829
use Visol\Cloudinary\Exceptions\CloudinaryNotFoundException;
@@ -105,7 +106,7 @@ public function processAction(): ResponseInterface
105106
try {
106107
[$requestType, $publicIds] = $this->getRequestInfo($payload);
107108

108-
self::getLogger()->debug(sprintf('Start cache flushing for file "%s". ', $requestType));
109+
self::getLogger()->debug(sprintf('Start flushing cache for file action "%s". ', $requestType));
109110
$this->initializeApi();
110111

111112
foreach ($publicIds as $publicId) {
@@ -179,6 +180,9 @@ protected function flushCloudinaryCdn(string $publicId): void
179180

180181
protected function handleFileRename(string $previousFileIdentifier, string $nextFileIdentifier): void
181182
{
183+
$nextFolderIdentifier = PathUtility::dirname($nextFileIdentifier);
184+
$nextFolderIdentifierHash = sha1($this->canonicalizeAndCheckFolderIdentifier($nextFolderIdentifier));
185+
$nextFileIdentifierHash = sha1($this->canonicalizeAndCheckFileIdentifier($nextFileIdentifier));
182186
$tableName = 'sys_file';
183187
$q = $this->getQueryBuilder($tableName);
184188
$q->update($tableName)
@@ -187,19 +191,31 @@ protected function handleFileRename(string $previousFileIdentifier, string $next
187191
$q->expr()->eq('identifier', $q->expr()->literal($previousFileIdentifier))
188192
)
189193
->set('identifier', $q->expr()->literal($nextFileIdentifier), false)
194+
->set('identifier_hash', $q->expr()->literal($nextFileIdentifierHash), false)
195+
->set('folder_hash', $q->expr()->literal($nextFolderIdentifierHash), false)
196+
->setMaxResults(1)
190197
->executeStatement();
191198
}
192-
193199
protected function getFile(array $cloudinaryResource): File
194200
{
195201
$fileIdentifier = $this->cloudinaryPathService->computeFileIdentifier($cloudinaryResource);
196-
/** @var File|null $file */
197-
$file = $this->storage->getFileByIdentifier($fileIdentifier);
202+
$tableName = 'sys_file';
203+
$q = $this->getQueryBuilder($tableName);
204+
$fileRecord = $q->select('*')
205+
->from($tableName)
206+
->where(
207+
$q->expr()->eq('storage', $this->storage->getUid()),
208+
$q->expr()->eq('identifier', $q->expr()->literal($fileIdentifier))
209+
)
210+
->execute()
211+
->fetchAssociative();
198212

199-
if (!$file) {
200-
throw new Exception('No file could be fine for file identifier ' . $fileIdentifier);
213+
if (!$fileRecord) {
214+
throw new Exception('No indexed file could be fine for public id ' . $cloudinaryResource['public_id']);
201215
}
202-
return $file;
216+
217+
$resourceFactory = GeneralUtility::makeInstance(ResourceFactory::class);
218+
return $resourceFactory->getFileObject($fileRecord['uid']);
203219
}
204220

205221
protected function getRequestInfo(array $payload): array
@@ -281,11 +297,12 @@ protected function clearCachePages(File $file): void
281297
protected function findPagesWithFileReferences(File $file): array
282298
{
283299
$queryBuilder = $this->getQueryBuilder('sys_file_reference');
300+
284301
// @phpstan-ignore-next-line
285302
return $queryBuilder
286303
->select('pid')
287304
->from('sys_file_reference')
288-
->groupBy('pid') // no support for distinct
305+
//->groupBy('pid') // no support for distinct
289306
->andWhere(
290307
'pid > 0',
291308
'uid_local = ' . $file->getUid()
@@ -294,6 +311,16 @@ protected function findPagesWithFileReferences(File $file): array
294311
->fetchAllAssociative();
295312
}
296313

314+
protected function canonicalizeAndCheckFileIdentifier(string $fileIdentifier): string
315+
{
316+
return '/' . ltrim($fileIdentifier, '/');
317+
}
318+
319+
protected function canonicalizeAndCheckFolderIdentifier(string $folderPath): string
320+
{
321+
return rtrim($this->canonicalizeAndCheckFileIdentifier($folderPath), '/') . '/';
322+
}
323+
297324
/**
298325
* We only react for notification type "upload", "rename", "delete"
299326
* @see other notification types

phpstan.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ parameters:
1212
message: '#does not accept object.$#'
1313
-
1414
message: '#^Call to an undefined method object#'
15+
-
16+
message: '#^Cannot call method fetch.* on Doctrine\\DBAL\\Result\|int#'

0 commit comments

Comments
 (0)