mirror of
https://github.com/appwrite/appwrite
synced 2026-05-22 00:18:25 +00:00
add: return bucket actual size.
This commit is contained in:
parent
68b4a49e9d
commit
67ef2ab552
2 changed files with 45 additions and 0 deletions
|
|
@ -8,6 +8,9 @@ use Appwrite\SDK\Method;
|
|||
use Appwrite\SDK\Response as SDKResponse;
|
||||
use Appwrite\Utopia\Response;
|
||||
use Utopia\Database\Database;
|
||||
use Utopia\Database\Document;
|
||||
use Utopia\Database\Query;
|
||||
use Utopia\Database\Validator\Authorization;
|
||||
use Utopia\Database\Validator\UID;
|
||||
use Utopia\Platform\Action;
|
||||
use Utopia\Platform\Scope\HTTP;
|
||||
|
|
@ -60,6 +63,42 @@ class Get extends Action
|
|||
throw new Exception(Exception::STORAGE_BUCKET_NOT_FOUND);
|
||||
}
|
||||
|
||||
$this->addBucketStorageSize($dbForProject, $bucket);
|
||||
|
||||
$response->dynamic($bucket, Response::MODEL_BUCKET);
|
||||
}
|
||||
|
||||
private function addBucketStorageSize(Database $dbForProject, Document $bucket): void
|
||||
{
|
||||
$metric = str_replace(
|
||||
'{bucketInternalId}',
|
||||
$bucket->getSequence(),
|
||||
METRIC_BUCKET_ID_FILES_STORAGE
|
||||
);
|
||||
|
||||
/**
|
||||
* StatsUsage does this create an ID -
|
||||
*
|
||||
* `$time = null;`\
|
||||
* `$id = md5("{$time}_{$period}_{$key}");`
|
||||
*
|
||||
* but when $time is null it just makes the $id as md5('_inf_' . $key);
|
||||
*
|
||||
* Why do this though?\
|
||||
* Using `getDocument()` below to leverage cache!
|
||||
*/
|
||||
$statsDocId = md5('_inf_' . $metric);
|
||||
|
||||
$storageStats = Authorization::skip(
|
||||
fn () => $dbForProject->getDocument(
|
||||
'stats',
|
||||
$statsDocId,
|
||||
[Query::select(['value'])]
|
||||
)
|
||||
);
|
||||
|
||||
$totalSize = $storageStats->isEmpty() ? 0 : $storageStats->getAttribute('value', 0);
|
||||
|
||||
$bucket->setAttribute('totalSize', $totalSize);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -92,6 +92,12 @@ class Bucket extends Model
|
|||
'default' => true,
|
||||
'example' => false,
|
||||
])
|
||||
->addRule('totalSize', [
|
||||
'type' => self::TYPE_INTEGER,
|
||||
'description' => 'Total size of this bucket in bytes.',
|
||||
'default' => 0,
|
||||
'example' => 128,
|
||||
])
|
||||
;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue