2323use TYPO3 \CMS \Core \Resource \ResourceFactory ;
2424use TYPO3 \CMS \Core \Resource \ResourceStorage ;
2525use TYPO3 \CMS \Core \Utility \GeneralUtility ;
26+ use TYPO3 \CMS \Core \Utility \PathUtility ;
2627use TYPO3 \CMS \Extbase \Mvc \Controller \ActionController ;
2728use Visol \Cloudinary \Events \ClearCachePageEvent ;
2829use 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
0 commit comments