@@ -20,6 +20,7 @@ class DeleteAssetsByPaths implements DeleteAssetsByPathsInterface
2020{
2121 private const TABLE_MEDIA_GALLERY_ASSET = 'media_gallery_asset ' ;
2222 private const MEDIA_GALLERY_ASSET_PATH = 'path ' ;
23+ private const MEDIA_GALLERY_ASSET_ID = 'id ' ;
2324
2425 /**
2526 * @var ResourceConnection
@@ -78,13 +79,39 @@ public function execute(array $paths): void
7879 * Delete assets from database based on the first part (beginning) of the path
7980 *
8081 * @param string $path
82+ * @throws \Zend_Db_Statement_Exception
8183 */
8284 private function deleteAssetsByDirectoryPath (string $ path ): void
85+ {
86+ /** @var AdapterInterface $connection */
87+ $ connection = $ this ->resourceConnection ->getConnection ();
88+
89+ $ select = $ connection ->select ()
90+ ->from ($ this ->resourceConnection ->getTableName (self ::TABLE_MEDIA_GALLERY_ASSET ))
91+ ->where (self ::MEDIA_GALLERY_ASSET_PATH . ' LIKE ? ' , $ path . '% ' );
92+
93+ $ assets = $ connection ->query ($ select )->fetchAll ();
94+
95+ // Filter out assets with mixed case that doesn't match the paths
96+ foreach ($ assets as $ asset ) {
97+ if (str_starts_with ($ asset [self ::MEDIA_GALLERY_ASSET_PATH ], $ path )) {
98+ $ this ->deleteAssetById ((int )$ asset [self ::MEDIA_GALLERY_ASSET_ID ]);
99+ }
100+ }
101+ }
102+
103+ /**
104+ * Delete assets from database by asset id
105+ *
106+ * @param int $id
107+ * @return void
108+ */
109+ private function deleteAssetById (int $ id ): void
83110 {
84111 /** @var AdapterInterface $connection */
85112 $ connection = $ this ->resourceConnection ->getConnection ();
86113 $ tableName = $ this ->resourceConnection ->getTableName (self ::TABLE_MEDIA_GALLERY_ASSET );
87- $ connection ->delete ($ tableName , [self ::MEDIA_GALLERY_ASSET_PATH . ' LIKE ? ' => $ path . ' % ' ]);
114+ $ connection ->delete ($ tableName , [self ::MEDIA_GALLERY_ASSET_ID . ' = ? ' => $ id ]);
88115 }
89116
90117 /**
0 commit comments