Skip to content

Commit 8c28cc0

Browse files
committed
feat: customizable Quota handler
Signed-off-by: Roberto Guido <info@madbob.org>
1 parent d3aba32 commit 8c28cc0

3 files changed

Lines changed: 32 additions & 19 deletions

File tree

lib/private/Files/SetupManager.php

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -209,20 +209,6 @@ function ($mountPoint, IStorage $storage, IMountPoint $mount) use ($reSharingEna
209209
return $storage;
210210
}, 50, $existingMounts);
211211

212-
$quotaIncludeExternal = $this->config->getSystemValue('quota_include_external_storage', false);
213-
$this->storageFactory->addStorageWrapper(Quota::class, function ($mountPoint, $storage, IMountPoint $mount) use ($quotaIncludeExternal) {
214-
// set up quota for home storages, even for other users
215-
// which can happen when using sharing
216-
if ($mount instanceof HomeMountPoint) {
217-
$user = $mount->getUser();
218-
return new Quota(['storage' => $storage, 'quotaCallback' => function () use ($user) {
219-
return $user->getQuotaBytes();
220-
}, 'root' => 'files', 'include_external_storage' => $quotaIncludeExternal]);
221-
}
222-
223-
return $storage;
224-
}, 50, $existingMounts);
225-
226212
$this->storageFactory->addStorageWrapper('readonly', function (string $mountPoint, IStorage $storage, IMountPoint $mount) {
227213
/*
228214
* Do not allow any operations that modify the storage
@@ -241,6 +227,28 @@ function ($mountPoint, IStorage $storage, IMountPoint $mount) use ($reSharingEna
241227
}, 50, $existingMounts);
242228
}
243229

230+
/**
231+
* Some wrapper are inited after BeforeFileSystemSetupEvent, to permit
232+
* custom apps to preventively register their own handler
233+
*
234+
* @param IMountPoint[] $existingMounts
235+
*/
236+
private function setupCustomizableWrappers(array $existingMounts): void {
237+
$quotaIncludeExternal = $this->config->getSystemValue('quota_include_external_storage', false);
238+
$this->storageFactory->addStorageWrapper(Quota::class, function ($mountPoint, $storage, IMountPoint $mount) use ($quotaIncludeExternal) {
239+
// set up quota for home storages, even for other users
240+
// which can happen when using sharing
241+
if ($mount instanceof HomeMountPoint) {
242+
$user = $mount->getUser();
243+
return new Quota(['storage' => $storage, 'quotaCallback' => function () use ($user) {
244+
return $user->getQuotaBytes();
245+
}, 'root' => 'files', 'include_external_storage' => $quotaIncludeExternal]);
246+
}
247+
248+
return $storage;
249+
}, 50, $existingMounts);
250+
}
251+
244252
/**
245253
* Update the cached mounts for all non-authoritative mount providers for a user.
246254
*/
@@ -328,6 +336,8 @@ private function oneTimeUserSetup(IUser $user): void {
328336
}
329337
}
330338

339+
$this->setupCustomizableWrappers($mounts);
340+
331341
$userDir = '/' . $user->getUID() . '/files';
332342

333343
Filesystem::initInternal($userDir);

lib/private/Files/Storage/Wrapper/Quota.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ private function hasQuota(): bool {
5858

5959
protected function getSize(string $path, ?IStorage $storage = null): int|float {
6060
if ($this->quotaIncludeExternalStorage) {
61-
$rootInfo = Filesystem::getFileInfo('', 'ext');
61+
$rootInfo = Filesystem::getFileInfo($path, 'ext');
6262
if ($rootInfo) {
6363
return $rootInfo->getSize(true);
6464
}

lib/private/legacy/OC_Helper.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,10 +187,7 @@ public static function getStorageInfo($path, $rootInfo = null, $includeMountPoin
187187
if (!$rootInfo instanceof FileInfo) {
188188
throw new NotFoundException('The root directory of the user\'s files is missing');
189189
}
190-
$used = $rootInfo->getSize($includeMountPoints);
191-
if ($used < 0) {
192-
$used = 0.0;
193-
}
190+
194191
/** @var int|float $quota */
195192
$quota = FileInfo::SPACE_UNLIMITED;
196193
$mount = $rootInfo->getMountPoint();
@@ -226,7 +223,13 @@ public static function getStorageInfo($path, $rootInfo = null, $includeMountPoin
226223
if ($sourceStorage->instanceOfStorage('\OC\Files\Storage\Wrapper\Quota')) {
227224
/** @var Quota $sourceStorage */
228225
$quota = $sourceStorage->getQuota();
226+
$used = $sourceStorage->getSize($path, $storage);
227+
} else {
228+
$used = $rootInfo->getSize($includeMountPoints);
229229
}
230+
231+
$used = max($used, 0.0);
232+
230233
try {
231234
$free = $sourceStorage->free_space($rootInfo->getInternalPath());
232235
if (is_bool($free)) {

0 commit comments

Comments
 (0)