diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Get.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Get.php index 5c3515122b..4e75de27c8 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Get.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/Get.php @@ -51,6 +51,7 @@ class Get extends Action ->inject('dbForProject') ->inject('project') ->inject('getLogsDB') + ->inject('authorization') ->callback($this->action(...)); } @@ -59,7 +60,8 @@ class Get extends Action Response $response, Database $dbForProject, Document $project, - callable $getLogsDB + callable $getLogsDB, + Authorization $authorization, ): void { $bucket = $dbForProject->getDocument('buckets', $bucketId); @@ -75,19 +77,20 @@ class Get extends Action $statsDocId = md5('_inf_' . $metric); - $dbForLogs = call_user_func($getLogsDB, $project); - $storageStats = Authorization::skip( - fn () => $dbForLogs->getDocument( + $totalSize = 0; + + try { + $dbForLogs = $getLogsDB($project); + $storageStats = $authorization->skip(fn () => $dbForLogs->getDocument( 'stats', $statsDocId, [Query::select(['value'])] - ) - ); + )); - /** - * The value can be 0 if stats were not aggregated when this request was made! - */ - $totalSize = $storageStats->isEmpty() ? 0 : $storageStats->getAttribute('value', 0); + $totalSize = $storageStats->getAttribute('value', 0); + } catch (\Throwable) { + // Stats may not be available, default to 0 + } $bucket->setAttribute('totalSize', $totalSize); diff --git a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/XList.php b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/XList.php index a2c880ce08..601d9b5321 100644 --- a/src/Appwrite/Platform/Modules/Storage/Http/Buckets/XList.php +++ b/src/Appwrite/Platform/Modules/Storage/Http/Buckets/XList.php @@ -58,6 +58,7 @@ class XList extends Action ->inject('dbForProject') ->inject('project') ->inject('getLogsDB') + ->inject('authorization') ->callback($this->action(...)); } @@ -68,7 +69,8 @@ class XList extends Action Response $response, Database $dbForProject, Document $project, - callable $getLogsDB + callable $getLogsDB, + Authorization $authorization ) { try { $queries = Query::parseQueries($queries); @@ -117,7 +119,6 @@ class XList extends Action if (!empty($buckets)) { $bucketByStatsId = []; - $dbForLogs = call_user_func($getLogsDB, $project); foreach ($buckets as $bucket) { $metric = str_replace( @@ -134,22 +135,28 @@ class XList extends Action $bucket->setAttribute('totalSize', 0); } - /* @type Document[] $stats */ - $stats = Authorization::skip(function () use ($dbForLogs, $bucketByStatsId) { - $statsIds = array_keys($bucketByStatsId); + try { + $dbForLogs = $getLogsDB($project); - return $dbForLogs->find('stats', [ - Query::equal('$id', $statsIds), - Query::select(['value']), - ]); - }); + /* @var array $stats */ + $stats = $authorization->skip(function () use ($dbForLogs, $bucketByStatsId) { + $statsIds = array_keys($bucketByStatsId); - foreach ($stats as $stat) { - $bucket = $bucketByStatsId[$stat->getId()]; + return $dbForLogs->find('stats', [ + Query::equal('$id', $statsIds), + Query::select(['value']), + ]); + }); - if ($bucket) { - $bucket->setAttribute('totalSize', $stat->getAttribute('value', 0)); + foreach ($stats as $stat) { + $bucket = $bucketByStatsId[$stat->getId()]; + + if ($bucket) { + $bucket->setAttribute('totalSize', $stat->getAttribute('value', 0)); + } } + } catch (\Throwable) { + // Stats may not be available, default to 0 } }