Skip to content

Commit e916fff

Browse files
Merge pull request #50474 from nextcloud/backport/48581/stable31
[stable31] fix: Filter for folders in cleanup old preview job
2 parents 62a74d8 + 72a43a1 commit e916fff

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

lib/private/Preview/BackgroundCleanupJob.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,13 @@ private function getOldPreviewLocations(): \Iterator {
6363
$qb->expr()->castColumn('a.name', IQueryBuilder::PARAM_INT), 'b.fileid'
6464
))
6565
->where(
66-
$qb->expr()->isNull('b.fileid')
67-
)->andWhere(
68-
$qb->expr()->eq('a.storage', $qb->createNamedParameter($this->previewFolder->getStorageId()))
69-
)->andWhere(
70-
$qb->expr()->eq('a.parent', $qb->createNamedParameter($this->previewFolder->getId()))
71-
)->andWhere(
72-
$qb->expr()->like('a.name', $qb->createNamedParameter('__%'))
66+
$qb->expr()->andX(
67+
$qb->expr()->isNull('b.fileid'),
68+
$qb->expr()->eq('a.storage', $qb->createNamedParameter($this->previewFolder->getStorageId())),
69+
$qb->expr()->eq('a.parent', $qb->createNamedParameter($this->previewFolder->getId())),
70+
$qb->expr()->like('a.name', $qb->createNamedParameter('__%')),
71+
$qb->expr()->eq('a.mimetype', $qb->createNamedParameter($this->mimeTypeLoader->getId('httpd/unix-directory')))
72+
)
7373
);
7474

7575
if (!$this->isCLI) {

tests/lib/Preview/BackgroundCleanupJobTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,13 +191,44 @@ public function testOldPreviews(): void {
191191
$f2 = $appdata->newFolder((string)PHP_INT_MAX - 1);
192192
$f2->newFile('foo.jpg', 'foo');
193193

194+
/*
195+
* Cleanup of OldPreviewLocations should only remove numeric folders on AppData level,
196+
* therefore these files should stay untouched.
197+
*/
198+
$appdata->getFolder('/')->newFile('not-a-directory', 'foo');
199+
$appdata->getFolder('/')->newFile('133742', 'bar');
200+
194201
$appdata = \OC::$server->getAppDataDir('preview');
202+
// AppData::getDirectoryListing filters all non-folders
195203
$this->assertSame(3, count($appdata->getDirectoryListing()));
204+
try {
205+
$appdata->getFolder('/')->getFile('not-a-directory');
206+
} catch (NotFoundException) {
207+
$this->fail('Could not find file \'not-a-directory\'');
208+
}
209+
try {
210+
$appdata->getFolder('/')->getFile('133742');
211+
} catch (NotFoundException) {
212+
$this->fail('Could not find file \'133742\'');
213+
}
196214

197215
$job = new BackgroundCleanupJob($this->timeFactory, $this->connection, $this->getRoot(), $this->mimeTypeLoader, true);
198216
$job->run([]);
199217

200218
$appdata = \OC::$server->getAppDataDir('preview');
219+
220+
// Check if the files created above are still present
221+
// Remember: AppData::getDirectoryListing filters all non-folders
201222
$this->assertSame(0, count($appdata->getDirectoryListing()));
223+
try {
224+
$appdata->getFolder('/')->getFile('not-a-directory');
225+
} catch (NotFoundException) {
226+
$this->fail('Could not find file \'not-a-directory\'');
227+
}
228+
try {
229+
$appdata->getFolder('/')->getFile('133742');
230+
} catch (NotFoundException) {
231+
$this->fail('Could not find file \'133742\'');
232+
}
202233
}
203234
}

0 commit comments

Comments
 (0)