Skip to content

Commit b540da6

Browse files
hammer065come-nc
authored andcommitted
fix(preview): 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 <[email protected]>
1 parent b6f8b51 commit b540da6

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

lib/private/Preview/BackgroundCleanupJob.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,13 @@ private function getOldPreviewLocations(): \Iterator {
8989
$qb->expr()->castColumn('a.name', IQueryBuilder::PARAM_INT), 'b.fileid'
9090
))
9191
->where(
92-
$qb->expr()->isNull('b.fileid')
93-
)->andWhere(
94-
$qb->expr()->eq('a.parent', $qb->createNamedParameter($this->previewFolder->getId()))
95-
)->andWhere(
96-
$qb->expr()->like('a.name', $qb->createNamedParameter('__%'))
92+
$qb->expr()->andX(
93+
$qb->expr()->isNull('b.fileid'),
94+
$qb->expr()->eq('a.storage', $qb->createNamedParameter($this->previewFolder->getStorageId())),
95+
$qb->expr()->eq('a.parent', $qb->createNamedParameter($this->previewFolder->getId())),
96+
$qb->expr()->like('a.name', $qb->createNamedParameter('__%')),
97+
$qb->expr()->eq('a.mimetype', $qb->createNamedParameter($this->mimeTypeLoader->getId('httpd/unix-directory')))
98+
)
9799
);
98100

99101
if (!$this->isCLI) {

tests/lib/Preview/BackgroundCleanupJobTest.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,45 @@ public function testOldPreviews() {
197197
$f2 = $appdata->newFolder('123456782');
198198
$f2->newFile('foo.jpg', 'foo');
199199

200+
/*
201+
* Cleanup of OldPreviewLocations should only remove numeric folders on AppData level,
202+
* therefore these files should stay untouched.
203+
*/
204+
$appdata->getFolder('/')->newFile('not-a-directory', 'foo');
205+
$appdata->getFolder('/')->newFile('133742', 'bar');
206+
200207
$appdata = \OC::$server->getAppDataDir('preview');
208+
209+
// AppData::getDirectoryListing filters all non-folders
201210
$this->assertSame(2, count($appdata->getDirectoryListing()));
211+
try {
212+
$appdata->getFolder('/')->getFile('not-a-directory');
213+
} catch (NotFoundException) {
214+
$this->fail('Could not find file \'not-a-directory\'');
215+
}
216+
try {
217+
$appdata->getFolder('/')->getFile('133742');
218+
} catch (NotFoundException) {
219+
$this->fail('Could not find file \'133742\'');
220+
}
202221

203222
$job = new BackgroundCleanupJob($this->timeFactory, $this->connection, $this->getRoot(), $this->mimeTypeLoader, true);
204223
$job->run([]);
205224

206225
$appdata = \OC::$server->getAppDataDir('preview');
226+
227+
// Check if the files created above are still present
228+
// Remember: AppData::getDirectoryListing filters all non-folders
207229
$this->assertSame(0, count($appdata->getDirectoryListing()));
230+
try {
231+
$appdata->getFolder('/')->getFile('not-a-directory');
232+
} catch (NotFoundException) {
233+
$this->fail('Could not find file \'not-a-directory\'');
234+
}
235+
try {
236+
$appdata->getFolder('/')->getFile('133742');
237+
} catch (NotFoundException) {
238+
$this->fail('Could not find file \'133742\'');
239+
}
208240
}
209241
}

0 commit comments

Comments
 (0)