Skip to content

Commit

Permalink
fix: Filter for folders in cleanup old preview job
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
hammer065 committed Oct 5, 2024
1 parent 0801ef8 commit c6e208b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
14 changes: 7 additions & 7 deletions lib/private/Preview/BackgroundCleanupJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 12 additions & 2 deletions tests/lib/Preview/BackgroundCleanupJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit c6e208b

Please sign in to comment.