From c6e208bc34e7f8903daf8d3406419cb3afff1ea8 Mon Sep 17 00:00:00 2001 From: Dario Mehlich Date: Sat, 5 Oct 2024 02:36:08 +0200 Subject: [PATCH] fix: Filter for folders in cleanup old preview job Fixes #35936. When running `OC\Preview\BackgroundCleanupJob`, the main iteration loop in `run()` expects a folder, however, `getOldPreviewLocations()` currently does not filter by mimetype and therefore can yield a non-folder entry which causes an Exception when constructing the Folder impl. Filtering for `httpd/unix-directory`, as `getNewPreviewLocations()` already does, fixes this issue. Signed-off-by: Dario Mehlich --- lib/private/Preview/BackgroundCleanupJob.php | 14 +++++++------- tests/lib/Preview/BackgroundCleanupJobTest.php | 14 ++++++++++++-- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/private/Preview/BackgroundCleanupJob.php b/lib/private/Preview/BackgroundCleanupJob.php index 8449e73983cb7..9cee4c5c30c03 100644 --- a/lib/private/Preview/BackgroundCleanupJob.php +++ b/lib/private/Preview/BackgroundCleanupJob.php @@ -76,13 +76,13 @@ private function getOldPreviewLocations(): \Iterator { $qb->expr()->castColumn('a.name', IQueryBuilder::PARAM_INT), 'b.fileid' )) ->where( - $qb->expr()->isNull('b.fileid') - )->andWhere( - $qb->expr()->eq('a.storage', $qb->createNamedParameter($this->previewFolder->getStorageId())) - )->andWhere( - $qb->expr()->eq('a.parent', $qb->createNamedParameter($this->previewFolder->getId())) - )->andWhere( - $qb->expr()->like('a.name', $qb->createNamedParameter('__%')) + $qb->expr()->andX( + $qb->expr()->isNull('b.fileid'), + $qb->expr()->eq('a.storage', $qb->createNamedParameter($this->previewFolder->getStorageId())), + $qb->expr()->eq('a.parent', $qb->createNamedParameter($this->previewFolder->getId())), + $qb->expr()->like('a.name', $qb->createNamedParameter('__%')), + $qb->expr()->eq('a.mimetype', $qb->createNamedParameter($this->mimeTypeLoader->getId('httpd/unix-directory'))) + ) ); if (!$this->isCLI) { diff --git a/tests/lib/Preview/BackgroundCleanupJobTest.php b/tests/lib/Preview/BackgroundCleanupJobTest.php index 9c521376af5ad..7c7d458164fdf 100644 --- a/tests/lib/Preview/BackgroundCleanupJobTest.php +++ b/tests/lib/Preview/BackgroundCleanupJobTest.php @@ -191,13 +191,23 @@ public function testOldPreviews(): void { $f2 = $appdata->newFolder((string)PHP_INT_MAX - 1); $f2->newFile('foo.jpg', 'foo'); + /* + * Cleanup of OldPreviewLocations should only remove folders on AppData level, + * therefore this file should stay untouched. + */ + $appdata->getAppDataFolder()->newFile('not-a-directory', 'foo'); + $appdata = \OC::$server->getAppDataDir('preview'); - $this->assertSame(3, count($appdata->getDirectoryListing())); + $this->assertSame(4, count($appdata->getDirectoryListing())); $job = new BackgroundCleanupJob($this->timeFactory, $this->connection, $this->getRoot(), $this->mimeTypeLoader, true); $job->run([]); $appdata = \OC::$server->getAppDataDir('preview'); - $this->assertSame(0, count($appdata->getDirectoryListing())); + + // Check if the `not-a-directory` file is still present + $directoryListing = $appdata->getDirectoryListing(); + $this->assertSame(1, count($directoryListing)); + $this->assertSame('not-a-directory', $directoryListing[0]->getName()); } }