From 72f257222efdd4e2c4c05e52d074ee12332a528b Mon Sep 17 00:00:00 2001
From: Oliver Bartsch <bo@cedev.de>
Date: Wed, 20 Dec 2023 12:28:51 +0100
Subject: [PATCH] [BUGFIX] Prevent 'Undefined array key' in FileDeletionAspect

Properly check for valid meta data record
to prevent 'Undefined array key' error on
deleting a file.

Meta data record might not exists, because
the file might have not been indexed or
simply because the record was manually deleted.

Additionally, some code clean up is done.

Resolves: #102700
Releases: main, 12.4
Change-Id: Ibf8a0d53b40f08b60064583d5f28762c25d75937
Reviewed-on: https://review.typo3.org/c/Packages/TYPO3.CMS/+/82247
Reviewed-by: Nikita Hovratov <nikita.h@live.de>
Tested-by: Nikita Hovratov <nikita.h@live.de>
Tested-by: core-ci <typo3@b13.com>
---
 .../Processing/FileDeletionAspect.php         | 20 ++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/typo3/sysext/core/Classes/Resource/Processing/FileDeletionAspect.php b/typo3/sysext/core/Classes/Resource/Processing/FileDeletionAspect.php
index cb77a9576331..cd2d65e22b4a 100644
--- a/typo3/sysext/core/Classes/Resource/Processing/FileDeletionAspect.php
+++ b/typo3/sysext/core/Classes/Resource/Processing/FileDeletionAspect.php
@@ -55,7 +55,7 @@ final class FileDeletionAspect
     /**
      * Cleanup database record for a deleted file
      */
-    private function removeFromRepository(FileInterface $fileObject)
+    private function removeFromRepository(FileInterface $fileObject): void
     {
         // remove file from repository
         if ($fileObject instanceof File) {
@@ -69,7 +69,7 @@ final class FileDeletionAspect
                 ->delete(
                     'sys_file_reference',
                     [
-                        'uid_local' => (int)$fileObject->getUid(),
+                        'uid_local' => $fileObject->getUid(),
                     ]
                 );
         } elseif ($fileObject instanceof ProcessedFile) {
@@ -77,7 +77,7 @@ final class FileDeletionAspect
                 ->delete(
                     'sys_file_processedfile',
                     [
-                        'uid' => (int)$fileObject->getUid(),
+                        'uid' => $fileObject->getUid(),
                     ]
                 );
         }
@@ -86,17 +86,23 @@ final class FileDeletionAspect
     /**
      * Remove all category references of the deleted file.
      */
-    private function cleanupCategoryReferences(File $fileObject)
+    private function cleanupCategoryReferences(File $fileObject): void
     {
         // Retrieve the file metadata uid which is different from the file uid.
         $metadataProperties = $fileObject->getMetaData()->get();
-        $metaDataUid = $metadataProperties['_ORIG_uid'] ?? $metadataProperties['uid'];
+        $metaDataUid = (int)($metadataProperties['_ORIG_uid'] ?? $metadataProperties['uid'] ?? 0);
+
+        if ($metaDataUid <= 0) {
+            // No metadata record exists for the given file. The file might not
+            // have been indexed or the meta data record was deleted manually.
+            return;
+        }
 
         GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable('sys_category_record_mm')
             ->delete(
                 'sys_category_record_mm',
                 [
-                    'uid_foreign' => (int)$metaDataUid,
+                    'uid_foreign' => $metaDataUid,
                     'tablenames' => 'sys_file_metadata',
                 ]
             );
@@ -105,7 +111,7 @@ final class FileDeletionAspect
     /**
      * Remove all processed files that belong to the given File object
      */
-    private function cleanupProcessedFiles(FileInterface $fileObject)
+    private function cleanupProcessedFiles(FileInterface $fileObject): void
     {
         // only delete processed files of File objects
         if (!$fileObject instanceof File) {
-- 
GitLab