Skip to content

Commit dd0d90c

Browse files
committed
fix(StorageService#countFiles): Only count files in eligible mounts and with the correct size
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
1 parent 334c219 commit dd0d90c

File tree

1 file changed

+60
-13
lines changed

1 file changed

+60
-13
lines changed

lib/Service/StorageService.php

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,65 @@ public function __construct(
5454
* @throws Exception
5555
*/
5656
public function countFiles(): int {
57+
$totalCount = 0;
58+
foreach ($this->getMounts() as $mount) {
59+
$totalCount += $this->countFilesInMount($mount['storage_id'], $mount['root_id']);
60+
}
61+
return $totalCount;
62+
}
63+
64+
/**
65+
* @param int $storageId
66+
* @param int $rootId
67+
* @return int
68+
*/
69+
public function countFilesInMount(int $storageId, int $rootId): int {
70+
$qb = $this->getCacheQueryBuilder();
71+
try {
72+
$qb->selectFileCache();
73+
$qb->andWhere($qb->expr()->eq('filecache.fileid', $qb->createNamedParameter($rootId, IQueryBuilder::PARAM_INT)));
74+
$result = $qb->executeQuery();
75+
/** @var array{path:string}|false $root */
76+
$root = $result->fetch();
77+
$result->closeCursor();
78+
} catch (Exception $e) {
79+
$this->logger->error('Could not fetch storage root', ['exception' => $e]);
80+
return 0;
81+
}
82+
83+
if ($root === false) {
84+
$this->logger->error('Could not fetch storage root');
85+
return 0;
86+
}
87+
5788
$mimeTypes = array_map(fn ($mimeType) => $this->mimeTypes->getId($mimeType), Application::MIMETYPES);
89+
5890
$qb = $this->getCacheQueryBuilder();
59-
$qb->select($qb->func()->count('*'))
60-
->from('filecache', 'filecache')
61-
->where($qb->expr()->in('mimetype', $qb->createNamedParameter($mimeTypes, IQueryBuilder::PARAM_INT_ARRAY)));
62-
$result = $qb->executeQuery();
63-
$count = $result->fetchOne();
91+
92+
try {
93+
$path = $root['path'] === '' ? '' : $root['path'] . '/';
94+
95+
$qb->select($qb->func()->count('*'))
96+
->from('filecache', 'filecache');
97+
$qb->whereStorageId($storageId);
98+
$qb
99+
->andWhere($qb->expr()->like('path', $qb->createNamedParameter($path . '%')))
100+
->andWhere($qb->expr()->eq('storage', $qb->createNamedParameter($storageId)))
101+
->andWhere($qb->expr()->in('mimetype', $qb->createNamedParameter($mimeTypes, IQueryBuilder::PARAM_INT_ARRAY)))
102+
->andWhere($qb->expr()->lte('size', $qb->createNamedParameter(Application::CC_MAX_SIZE, IQueryBuilder::PARAM_INT)))
103+
->andWhere($qb->expr()->gt('size', $qb->createNamedParameter(0, IQueryBuilder::PARAM_INT)));
104+
$result = $qb->executeQuery();
105+
} catch (Exception $e) {
106+
$this->logger->error('Could not count files in mount: storage=' . $storageId . ' root=' . $rootId, ['exception' => $e]);
107+
return 0;
108+
}
109+
110+
$countInMount = $result->fetchOne();
64111
$result->closeCursor();
65-
if ($count === false) {
112+
if ($countInMount === false) {
66113
return 0;
67114
}
68-
return $count;
115+
return $countInMount;
69116
}
70117

71118
/**
@@ -116,17 +163,16 @@ public function getMounts(): \Generator {
116163
/**
117164
* @param int $storageId
118165
* @param int $rootId
119-
* @param array $models
120166
* @param int $lastFileId
121167
* @param int $maxResults
122168
* @return \Generator<int,int,mixed,void>
123169
*/
124170
public function getFilesInMount(int $storageId, int $rootId, int $lastFileId = 0, int $maxResults = 100): \Generator {
125171
$qb = $this->getCacheQueryBuilder();
126172
try {
127-
$result = $qb->selectFileCache()
128-
->andWhere($qb->expr()->eq('filecache.fileid', $qb->createNamedParameter($rootId, IQueryBuilder::PARAM_INT)))
129-
->executeQuery();
173+
$qb->selectFileCache();
174+
$qb->andWhere($qb->expr()->eq('filecache.fileid', $qb->createNamedParameter($rootId, IQueryBuilder::PARAM_INT)));
175+
$result = $qb->executeQuery();
130176
/** @var array{path:string}|false $root */
131177
$root = $result->fetch();
132178
$result->closeCursor();
@@ -147,8 +193,9 @@ public function getFilesInMount(int $storageId, int $rootId, int $lastFileId = 0
147193
try {
148194
$path = $root['path'] === '' ? '' : $root['path'] . '/';
149195

150-
$qb->selectFileCache()
151-
->whereStorageId($storageId)
196+
$qb->selectFileCache();
197+
$qb->whereStorageId($storageId);
198+
$qb
152199
->andWhere($qb->expr()->like('path', $qb->createNamedParameter($path . '%')))
153200
->andWhere($qb->expr()->eq('storage', $qb->createNamedParameter($storageId)))
154201
->andWhere($qb->expr()->gt('filecache.fileid', $qb->createNamedParameter($lastFileId)))

0 commit comments

Comments
 (0)