88namespace OC \Files \Storage \Wrapper ;
99
1010use OC \Files \Filesystem ;
11+ use OC \Files \View ;
1112use OC \SystemConfig ;
1213use OCP \Files \Cache \ICacheEntry ;
1314use OCP \Files \FileInfo ;
1415use OCP \Files \Storage \IStorage ;
16+ use OCP \IUser ;
1517
1618class Quota extends Wrapper {
1719 /** @var callable|null */
@@ -22,6 +24,8 @@ class Quota extends Wrapper {
2224 private SystemConfig $ config ;
2325 private bool $ quotaIncludeExternalStorage ;
2426 private bool $ enabled = true ;
27+ /** The user the wrapped home storage belongs to, if known */
28+ private ?IUser $ user ;
2529
2630 /**
2731 * @param array $parameters
@@ -32,6 +36,7 @@ public function __construct(array $parameters) {
3236 $ this ->quotaCallback = $ parameters ['quotaCallback ' ] ?? null ;
3337 $ this ->sizeRoot = $ parameters ['root ' ] ?? '' ;
3438 $ this ->quotaIncludeExternalStorage = $ parameters ['include_external_storage ' ] ?? false ;
39+ $ this ->user = $ parameters ['user ' ] ?? null ;
3540 }
3641
3742 public function getQuota (): int |float {
@@ -53,13 +58,28 @@ private function hasQuota(): bool {
5358 return $ this ->getQuota () !== FileInfo::SPACE_UNLIMITED ;
5459 }
5560
61+ /**
62+ * Usage of the quota user's files including the external storages mounted in them.
63+ * It is read through that user's view, since the session user differs when writing into their shares.
64+ * Storages using the quota user's session credentials count as empty in other sessions.
65+ */
66+ private function getSizeIncludingExternalStorage (): int |float {
67+ if ($ this ->user === null ) {
68+ $ rootInfo = Filesystem ::getFileInfo ('' , 'ext ' );
69+ } else {
70+ $ view = new View ('/ ' . $ this ->user ->getUID () . '/files ' );
71+ $ rootInfo = $ view ->getFileInfo ('' , 'ext ' );
72+ }
73+
74+ if ($ rootInfo ) {
75+ return $ rootInfo ->getSize (true );
76+ }
77+ return FileInfo::SPACE_NOT_COMPUTED ;
78+ }
79+
5680 protected function getSize (string $ path , ?IStorage $ storage = null ): int |float {
5781 if ($ this ->quotaIncludeExternalStorage ) {
58- $ rootInfo = Filesystem ::getFileInfo ('' , 'ext ' );
59- if ($ rootInfo ) {
60- return $ rootInfo ->getSize (true );
61- }
62- return FileInfo::SPACE_NOT_COMPUTED ;
82+ return $ this ->getSizeIncludingExternalStorage ();
6383 } else {
6484 $ cache = is_null ($ storage ) ? $ this ->getCache () : $ storage ->getCache ();
6585 $ data = $ cache ->get ($ path );
0 commit comments