1
0
Fork 0
mirror of synced 2024-06-03 11:24:48 +12:00
appwrite/app/controllers/shared/api.php

523 lines
20 KiB
PHP
Raw Normal View History

2019-05-09 18:54:39 +12:00
<?php
2019-10-01 17:57:41 +13:00
2021-01-12 10:52:05 +13:00
use Appwrite\Auth\Auth;
2022-05-26 23:51:08 +12:00
use Appwrite\Event\Audit;
Database layer (#3338) * database response model * database collection config * new database scopes * database service update * database execption codes * remove read write permission from database model * updating tests and fixing some bugs * server side tests are now passing * databases api * tests for database endpoint * composer update * fix error * formatting * formatting fixes * get database test * more updates to events and usage * more usage updates * fix delete type * fix test * delete database * more fixes * databaseId in attributes and indexes * more fixes * fix issues * fix index subquery * fix console scope and index query * updating tests as required * fix phpcs errors and warnings * updates to review suggestions * UI progress * ui updates and cleaning up * fix type * rework database events * update tests * update types * event generation fixed * events config updated * updating context to support multiple * realtime updates * fix ids * update context * validator updates * fix naming conflict * fix tests * fix lint errors * fix wprler and realtime tests * fix webhooks test * fix event validator and other tests * formatting fixes * removing leftover var_dumps * remove leftover comment * update usage params * usage metrics updates * update database usage * fix usage * specs update * updates to usage * fix UI and usage * fix lints * internal id fixes * fixes for internal Id * renaming services and related files * rename tests * rename doc link * rename readme * fix test name * tests: fixes for 0.15.x sync Co-authored-by: Torsten Dittmann <torsten.dittmann@googlemail.com>
2022-06-22 22:51:49 +12:00
use Appwrite\Event\Database as EventDatabase;
2022-05-26 23:51:08 +12:00
use Appwrite\Event\Delete;
2022-04-04 18:30:07 +12:00
use Appwrite\Event\Event;
2022-05-26 23:51:08 +12:00
use Appwrite\Event\Mail;
2021-06-30 23:36:58 +12:00
use Appwrite\Messaging\Adapter\Realtime;
2022-08-09 18:28:38 +12:00
use Appwrite\Usage\Stats;
2022-05-26 23:51:08 +12:00
use Appwrite\Utopia\Response;
use Appwrite\Utopia\Request;
2020-06-29 05:31:21 +12:00
use Utopia\App;
2022-02-07 01:49:01 +13:00
use Appwrite\Extend\Exception;
2019-11-30 07:23:29 +13:00
use Utopia\Abuse\Abuse;
use Utopia\Abuse\Adapters\TimeLimit;
use Utopia\Cache\Adapter\Filesystem;
use Utopia\Cache\Cache;
2022-10-16 21:50:53 +13:00
use Utopia\CLI\Console;
2022-05-26 23:51:08 +12:00
use Utopia\Database\Database;
2022-08-31 20:52:55 +12:00
use Utopia\Database\DateTime;
2021-10-08 04:35:17 +13:00
use Utopia\Database\Document;
2022-01-19 00:05:04 +13:00
use Utopia\Database\Validator\Authorization;
2019-11-30 07:23:29 +13:00
2022-08-17 00:28:30 +12:00
$parseLabel = function (string $label, array $responsePayload, array $requestParams, Document $user) {
preg_match_all('/{(.*?)}/', $label, $matches);
foreach ($matches[1] ?? [] as $pos => $match) {
$find = $matches[0][$pos];
$parts = explode('.', $match);
if (count($parts) !== 2) {
throw new Exception('Too less or too many parts', 400, Exception::GENERAL_ARGUMENT_INVALID);
}
$namespace = $parts[0] ?? '';
$replace = $parts[1] ?? '';
$params = match ($namespace) {
'user' => (array)$user,
'request' => $requestParams,
default => $responsePayload,
};
if (array_key_exists($replace, $params)) {
$label = \str_replace($find, $params[$replace], $label);
}
}
return $label;
};
2019-11-30 07:23:29 +13:00
2022-10-30 18:14:46 +13:00
$databaseListener = function (string $event, Document $document, Stats $usage) {
$multiplier = 1;
2022-10-30 18:16:51 +13:00
if ($event === Database::EVENT_DOCUMENT_DELETE) {
2022-10-30 18:14:46 +13:00
$multiplier = -1;
}
$collection = $document->getCollection();
switch ($collection) {
case 'users':
$usage->setParam('users.{scope}.count.total', 1 * $multiplier);
break;
case 'databases':
$usage->setParam('databases.{scope}.count.total', 1 * $multiplier);
break;
case 'buckets':
$usage->setParam('buckets.{scope}.count.total', 1 * $multiplier);
break;
case 'deployments':
$usage->setParam('deployments.{scope}.storage.size', $document->getAttribute('size') * $multiplier);
break;
default:
if (strpos($collection, 'bucket_') === 0) {
$usage
->setParam('bucketId', $document->getAttribute('bucketId'))
->setParam('files.{scope}.storage.size', $document->getAttribute('sizeOriginal') * $multiplier)
->setParam('files.{scope}.count.total', 1 * $multiplier);
} elseif (strpos($collection, 'database_') === 0) {
$usage
->setParam('databaseId', $document->getAttribute('databaseId'));
if (strpos($collection, '_collection_') !== false) {
2022-10-30 18:14:46 +13:00
$usage
->setParam('collectionId', $document->getAttribute('$collectionId'))
->setParam('documents.{scope}.count.total', 1 * $multiplier);
} else {
$usage->setParam('collections.{scope}.count.total', 1 * $multiplier);
}
}
break;
}
};
2022-07-22 18:00:42 +12:00
App::init()
2022-08-02 13:10:48 +12:00
->groups(['api'])
2022-07-22 18:00:42 +12:00
->inject('utopia')
->inject('request')
->inject('response')
->inject('project')
->inject('user')
->inject('events')
->inject('audits')
->inject('mails')
->inject('usage')
->inject('deletes')
->inject('database')
->inject('dbForProject')
->inject('mode')
2022-10-30 18:14:46 +13:00
->action(function (App $utopia, Request $request, Response $response, Document $project, Document $user, Event $events, Audit $audits, Mail $mails, Stats $usage, Delete $deletes, EventDatabase $database, Database $dbForProject, string $mode) use ($databaseListener) {
2022-07-22 18:00:42 +12:00
$route = $utopia->match($request);
if ($project->isEmpty() && $route->getLabel('abuse-limit', 0) > 0) { // Abuse limit requires an active project scope
2022-08-09 02:44:07 +12:00
throw new Exception(Exception::PROJECT_UNKNOWN);
2021-06-24 03:11:23 +12:00
}
/*
2022-07-22 18:00:42 +12:00
* Abuse Check
*/
2022-08-12 11:53:52 +12:00
$abuseKeyLabel = $route->getLabel('abuse-key', 'url:{url},ip:{ip}');
$timeLimitArray = [];
2021-06-07 17:17:29 +12:00
2022-07-22 18:00:42 +12:00
$abuseKeyLabel = (!is_array($abuseKeyLabel)) ? [$abuseKeyLabel] : $abuseKeyLabel;
2021-06-07 17:17:29 +12:00
2022-08-12 11:53:52 +12:00
foreach ($abuseKeyLabel as $abuseKey) {
$timeLimit = new TimeLimit($abuseKey, $route->getLabel('abuse-limit', 0), $route->getLabel('abuse-time', 3600), $dbForProject);
$timeLimit
->setParam('{userId}', $user->getId())
->setParam('{userAgent}', $request->getUserAgent(''))
->setParam('{ip}', $request->getIP())
2022-08-31 15:49:44 +12:00
->setParam('{url}', $request->getHostname() . $route->getPath())
->setParam('{method}', $request->getMethod());
2022-08-12 11:53:52 +12:00
$timeLimitArray[] = $timeLimit;
}
2021-06-07 17:17:29 +12:00
2022-07-22 18:00:42 +12:00
$closestLimit = null;
2021-06-07 17:17:29 +12:00
2022-07-22 18:00:42 +12:00
$roles = Authorization::getRoles();
$isPrivilegedUser = Auth::isPrivilegedUser($roles);
$isAppUser = Auth::isAppUser($roles);
2021-06-07 17:17:29 +12:00
2022-07-22 18:00:42 +12:00
foreach ($timeLimitArray as $timeLimit) {
foreach ($request->getParams() as $key => $value) { // Set request params as potential abuse keys
if (!empty($value)) {
$timeLimit->setParam('{param-' . $key . '}', (\is_array($value)) ? \json_encode($value) : $value);
}
}
2021-06-07 17:17:29 +12:00
2022-07-22 18:00:42 +12:00
$abuse = new Abuse($timeLimit);
$remaining = $timeLimit->remaining();
$limit = $timeLimit->limit();
2022-08-31 20:52:55 +12:00
$time = (new \DateTime($timeLimit->time()))->getTimestamp() + $route->getLabel('abuse-time', 3600);
2022-07-22 18:00:42 +12:00
if ($limit && ($remaining < $closestLimit || is_null($closestLimit))) {
$closestLimit = $remaining;
2022-07-22 18:00:42 +12:00
$response
->addHeader('X-RateLimit-Limit', $limit)
->addHeader('X-RateLimit-Remaining', $remaining)
->addHeader('X-RateLimit-Reset', $time)
2022-07-22 18:00:42 +12:00
;
2021-03-01 07:36:13 +13:00
}
2021-06-07 17:17:29 +12:00
$enabled = App::getEnv('_APP_OPTIONS_ABUSE', 'enabled') !== 'disabled';
2022-07-22 18:00:42 +12:00
if (
$enabled // Abuse is enabled
&& !$isAppUser // User is not API key
&& !$isPrivilegedUser // User is not an admin
&& $abuse->check() // Route is rate-limited
) {
2022-08-09 02:44:07 +12:00
throw new Exception(Exception::GENERAL_RATE_LIMIT_EXCEEDED);
2021-03-01 07:36:13 +13:00
}
}
2021-01-06 01:22:20 +13:00
/*
* Background Jobs
*/
2022-08-12 11:53:52 +12:00
$events
2022-07-22 18:00:42 +12:00
->setEvent($route->getLabel('event', ''))
->setProject($project)
2022-08-12 01:47:53 +12:00
->setUser($user);
2022-04-04 18:30:07 +12:00
2022-07-22 18:00:42 +12:00
$mails
->setProject($project)
2022-08-12 01:47:53 +12:00
->setUser($user);
2021-03-01 07:36:13 +13:00
2022-07-22 18:00:42 +12:00
$audits
->setMode($mode)
->setUserAgent($request->getUserAgent(''))
->setIP($request->getIP())
2022-09-04 20:23:24 +12:00
->setEvent($route->getLabel('audits.event', ''))
2022-07-22 18:00:42 +12:00
->setProject($project)
2022-08-12 01:47:53 +12:00
->setUser($user);
2021-03-01 07:36:13 +13:00
2022-07-22 18:00:42 +12:00
$usage
->setParam('projectInternalId', $project->getInternalId())
2022-07-22 18:00:42 +12:00
->setParam('projectId', $project->getId())
2022-08-17 22:55:01 +12:00
->setParam('project.{scope}.network.requests', 1)
2022-07-22 18:00:42 +12:00
->setParam('httpMethod', $request->getMethod())
2022-08-17 22:55:01 +12:00
->setParam('project.{scope}.network.inbound', 0)
->setParam('project.{scope}.network.outbound', 0);
2022-07-22 18:00:42 +12:00
$deletes->setProject($project);
$database->setProject($project);
2022-08-09 23:57:33 +12:00
2022-10-30 18:14:46 +13:00
$dbForProject->on(Database::EVENT_DOCUMENT_CREATE, fn ($event, Document $document) => $databaseListener($event, $document, $usage));
$dbForProject->on(Database::EVENT_DOCUMENT_DELETE, fn ($event, Document $document) => $databaseListener($event, $document, $usage));
2022-10-17 20:10:18 +13:00
2022-08-09 23:57:33 +12:00
$useCache = $route->getLabel('cache', false);
2022-08-10 01:43:37 +12:00
2022-08-09 23:57:33 +12:00
if ($useCache) {
2022-11-11 23:41:41 +13:00
$key = md5($request->getURI() . implode('*', $request->getParams())) . '*' . APP_CACHE_BUSTER;
2022-08-16 01:55:11 +12:00
$cache = new Cache(
new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $project->getId())
);
2022-08-09 23:57:33 +12:00
$timestamp = 60 * 60 * 24 * 30;
$data = $cache->load($key, $timestamp);
2022-08-09 23:57:33 +12:00
if (!empty($data)) {
2022-08-10 01:43:37 +12:00
$data = json_decode($data, true);
$parts = explode('/', $data['resourceType']);
$type = $parts[0] ?? null;
if ($type === 'bucket') {
$bucketId = $parts[1] ?? null;
$bucket = Authorization::skip(fn () => $dbForProject->getDocument('buckets', $bucketId));
if ($bucket->isEmpty() || (!$bucket->getAttribute('enabled') && $mode !== APP_MODE_ADMIN)) {
throw new Exception(Exception::STORAGE_BUCKET_NOT_FOUND);
}
$fileSecurity = $bucket->getAttribute('fileSecurity', false);
$validator = new Authorization(Database::PERMISSION_READ);
$valid = $validator->isValid($bucket->getRead());
if (!$fileSecurity && !$valid) {
throw new Exception(Exception::USER_UNAUTHORIZED);
}
$parts = explode('/', $data['resource']);
$fileId = $parts[1] ?? null;
if ($fileSecurity && !$valid) {
$file = $dbForProject->getDocument('bucket_' . $bucket->getInternalId(), $fileId);
} else {
$file = Authorization::skip(fn() => $dbForProject->getDocument('bucket_' . $bucket->getInternalId(), $fileId));
}
if ($file->isEmpty()) {
throw new Exception(Exception::STORAGE_FILE_NOT_FOUND);
}
}
2022-08-10 01:43:37 +12:00
$response
->addHeader('Expires', \date('D, d M Y H:i:s', \time() + $timestamp) . ' GMT')
->addHeader('X-Appwrite-Cache', 'hit')
->setContentType($data['contentType'])
2022-08-10 01:43:37 +12:00
->send(base64_decode($data['payload']))
;
2022-08-10 22:13:05 +12:00
$route->setIsActive(false);
2022-08-10 01:43:37 +12:00
} else {
$response->addHeader('X-Appwrite-Cache', 'miss');
}
}
2022-07-22 18:00:42 +12:00
});
App::init()
2022-08-02 13:10:48 +12:00
->groups(['auth'])
2022-07-22 18:00:42 +12:00
->inject('utopia')
->inject('request')
->inject('project')
->action(function (App $utopia, Request $request, Document $project) {
$route = $utopia->match($request);
$isPrivilegedUser = Auth::isPrivilegedUser(Authorization::getRoles());
$isAppUser = Auth::isAppUser(Authorization::getRoles());
if ($isAppUser || $isPrivilegedUser) { // Skip limits for app and console devs
return;
}
2021-03-01 07:36:13 +13:00
2022-07-22 18:00:42 +12:00
$auths = $project->getAttribute('auths', []);
switch ($route->getLabel('auth.type', '')) {
case 'emailPassword':
if (($auths['emailPassword'] ?? true) === false) {
2022-08-09 02:44:07 +12:00
throw new Exception(Exception::USER_AUTH_METHOD_UNSUPPORTED, 'Email / Password authentication is disabled for this project');
2022-07-22 18:00:42 +12:00
}
break;
case 'magic-url':
if ($project->getAttribute('usersAuthMagicURL', true) === false) {
2022-08-09 02:44:07 +12:00
throw new Exception(Exception::USER_AUTH_METHOD_UNSUPPORTED, 'Magic URL authentication is disabled for this project');
2022-07-22 18:00:42 +12:00
}
break;
case 'anonymous':
if (($auths['anonymous'] ?? true) === false) {
2022-08-09 02:44:07 +12:00
throw new Exception(Exception::USER_AUTH_METHOD_UNSUPPORTED, 'Anonymous authentication is disabled for this project');
2022-07-22 18:00:42 +12:00
}
break;
case 'invites':
if (($auths['invites'] ?? true) === false) {
2022-08-09 02:44:07 +12:00
throw new Exception(Exception::USER_AUTH_METHOD_UNSUPPORTED, 'Invites authentication is disabled for this project');
2022-07-22 18:00:42 +12:00
}
break;
case 'jwt':
if (($auths['JWT'] ?? true) === false) {
2022-08-09 02:44:07 +12:00
throw new Exception(Exception::USER_AUTH_METHOD_UNSUPPORTED, 'JWT authentication is disabled for this project');
2022-07-22 18:00:42 +12:00
}
break;
default:
2022-08-09 02:44:07 +12:00
throw new Exception(Exception::USER_AUTH_METHOD_UNSUPPORTED, 'Unsupported authentication route');
2022-07-22 18:00:42 +12:00
break;
2022-08-12 11:53:52 +12:00
}
2022-07-22 18:00:42 +12:00
});
App::shutdown()
2022-08-02 13:10:48 +12:00
->groups(['api'])
2022-07-22 18:00:42 +12:00
->inject('utopia')
->inject('request')
->inject('response')
->inject('project')
->inject('events')
->inject('audits')
->inject('usage')
->inject('deletes')
->inject('database')
->inject('mode')
->inject('dbForProject')
2022-08-17 00:28:30 +12:00
->action(function (App $utopia, Request $request, Response $response, Document $project, Event $events, Audit $audits, Stats $usage, Delete $deletes, EventDatabase $database, string $mode, Database $dbForProject) use ($parseLabel) {
2022-08-09 23:57:33 +12:00
2022-08-08 03:09:37 +12:00
$responsePayload = $response->getPayload();
2022-07-22 18:00:42 +12:00
if (!empty($events->getEvent())) {
if (empty($events->getPayload())) {
2022-08-08 03:09:37 +12:00
$events->setPayload($responsePayload);
2021-03-01 07:36:13 +13:00
}
2022-07-22 18:00:42 +12:00
/**
* Trigger functions.
*/
$events
->setClass(Event::FUNCTIONS_CLASS_NAME)
->setQueue(Event::FUNCTIONS_QUEUE_NAME)
->trigger();
/**
* Trigger webhooks.
*/
$events
->setClass(Event::WEBHOOK_CLASS_NAME)
->setQueue(Event::WEBHOOK_QUEUE_NAME)
->trigger();
/**
* Trigger realtime.
*/
if ($project->getId() !== 'console') {
$allEvents = Event::generateEvents($events->getEvent(), $events->getParams());
$payload = new Document($events->getPayload());
$db = $events->getContext('database');
$collection = $events->getContext('collection');
$bucket = $events->getContext('bucket');
$target = Realtime::fromPayload(
2022-08-11 19:02:40 +12:00
// Pass first, most verbose event pattern
2022-07-22 18:00:42 +12:00
event: $allEvents[0],
payload: $payload,
project: $project,
database: $db,
collection: $collection,
bucket: $bucket,
);
Realtime::send(
projectId: $target['projectId'] ?? $project->getId(),
payload: $events->getPayload(),
events: $allEvents,
channels: $target['channels'],
roles: $target['roles'],
options: [
'permissionsChanged' => $target['permissionsChanged'],
'userId' => $events->getParam('userId')
]
);
2021-03-01 07:36:13 +13:00
}
2022-04-19 04:21:45 +12:00
}
2021-03-01 07:36:13 +13:00
2022-08-09 00:19:41 +12:00
$route = $utopia->match($request);
2022-08-15 07:27:43 +12:00
$requestParams = $route->getParamsValues();
2022-08-13 01:21:32 +12:00
$user = $audits->getUser();
2022-08-09 00:19:41 +12:00
2022-08-18 02:29:22 +12:00
/**
* Audit labels
*/
2022-08-17 00:28:30 +12:00
$pattern = $route->getLabel('audits.resource', null);
if (!empty($pattern)) {
$resource = $parseLabel($pattern, $responsePayload, $requestParams, $user);
if (!empty($resource) && $resource !== $pattern) {
$audits->setResource($resource);
2022-08-08 02:30:47 +12:00
}
2022-08-08 03:49:30 +12:00
}
2022-08-13 20:02:00 +12:00
2022-08-17 00:28:30 +12:00
$pattern = $route->getLabel('audits.userId', null);
2022-08-12 01:47:53 +12:00
if (!empty($pattern)) {
2022-08-17 00:28:30 +12:00
$userId = $parseLabel($pattern, $responsePayload, $requestParams, $user);
$user = $dbForProject->getDocument('users', $userId);
$audits->setUser($user);
2021-03-12 05:28:03 +13:00
}
2021-10-08 04:35:17 +13:00
2022-08-17 00:28:30 +12:00
if (!empty($audits->getResource()) && !empty($audits->getUser()->getId())) {
/**
* audits.payload is switched to default true
* in order to auto audit payload for all endpoints
*/
$pattern = $route->getLabel('audits.payload', true);
if (!empty($pattern)) {
$audits->setPayload($responsePayload);
}
2022-07-22 18:00:42 +12:00
foreach ($events->getParams() as $key => $value) {
$audits->setParam($key, $value);
2021-03-01 07:36:13 +13:00
}
2022-07-22 18:00:42 +12:00
$audits->trigger();
2022-04-19 04:21:45 +12:00
}
2021-10-08 04:35:17 +13:00
2022-07-22 18:00:42 +12:00
if (!empty($deletes->getType())) {
$deletes->trigger();
}
2021-10-08 04:35:17 +13:00
2022-07-22 18:00:42 +12:00
if (!empty($database->getType())) {
$database->trigger();
}
2021-10-08 04:35:17 +13:00
2022-08-18 02:29:22 +12:00
/**
* Cache label
*/
2022-08-17 03:02:17 +12:00
$useCache = $route->getLabel('cache', false);
if ($useCache) {
$resource = $resourceType = null;
2022-08-10 01:43:37 +12:00
$data = $response->getPayload();
2022-08-31 00:44:37 +12:00
2022-08-17 03:02:17 +12:00
if (!empty($data['payload'])) {
$pattern = $route->getLabel('cache.resource', null);
if (!empty($pattern)) {
$resource = $parseLabel($pattern, $responsePayload, $requestParams, $user);
}
2022-08-15 21:05:41 +12:00
$pattern = $route->getLabel('cache.resourceType', null);
if (!empty($pattern)) {
$resourceType = $parseLabel($pattern, $responsePayload, $requestParams, $user);
}
2022-11-11 23:41:41 +13:00
$key = md5($request->getURI() . implode('*', $request->getParams())) . '*' . APP_CACHE_BUSTER;
2022-08-17 03:02:17 +12:00
$data = json_encode([
'resourceType' => $resourceType,
'resource' => $resource,
'contentType' => $response->getContentType(),
2022-08-16 00:16:32 +12:00
'payload' => base64_encode($data['payload']),
2022-08-17 03:02:17 +12:00
]) ;
2022-08-15 21:05:41 +12:00
2022-08-17 03:02:17 +12:00
$signature = md5($data);
$cacheLog = $dbForProject->getDocument('cache', $key);
2022-08-31 20:52:55 +12:00
$accessedAt = $cacheLog->getAttribute('accessedAt', '');
$now = DateTime::now();
2022-08-17 03:02:17 +12:00
if ($cacheLog->isEmpty()) {
Authorization::skip(fn () => $dbForProject->createDocument('cache', new Document([
2022-08-10 01:43:37 +12:00
'$id' => $key,
2022-08-15 21:05:41 +12:00
'resource' => $resource,
'accessedAt' => $now,
2022-08-15 21:05:41 +12:00
'signature' => $signature,
2022-08-10 01:43:37 +12:00
])));
2022-09-01 01:11:23 +12:00
} elseif (DateTime::formatTz(DateTime::addSeconds(new \DateTime(), -APP_CACHE_UPDATE)) > $accessedAt) {
$cacheLog->setAttribute('accessedAt', $now);
2022-08-10 01:43:37 +12:00
Authorization::skip(fn () => $dbForProject->updateDocument('cache', $cacheLog->getId(), $cacheLog));
2022-08-17 03:02:17 +12:00
}
2022-08-15 03:01:34 +12:00
2022-08-17 03:02:17 +12:00
if ($signature !== $cacheLog->getAttribute('signature')) {
$cache = new Cache(
new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $project->getId())
);
$cache->save($key, $data);
}
2022-08-16 00:16:32 +12:00
}
}
2022-07-22 18:00:42 +12:00
if (
App::getEnv('_APP_USAGE_STATS', 'enabled') == 'enabled'
&& $project->getId()
&& !empty($route->getLabel('sdk.namespace', null))
) { // Don't calculate console usage on admin mode
$metric = $route->getLabel('usage.metric', '');
2022-08-25 23:51:59 +12:00
$usageParams = $route->getLabel('usage.params', []);
2022-08-11 19:02:40 +12:00
if (!empty($metric)) {
$usage->setParam($metric, 1);
foreach ($usageParams as $param) {
$param = $parseLabel($param, $responsePayload, $requestParams, $user);
$parts = explode(':', $param);
2022-08-21 22:16:55 +12:00
if (count($parts) != 2) {
throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Usage params not properly set');
}
$usage->setParam($parts[0], $parts[1]);
}
}
2022-08-17 22:55:01 +12:00
$fileSize = 0;
$file = $request->getFiles('file');
2022-08-17 23:10:17 +12:00
if (!empty($file)) {
2022-08-17 22:55:01 +12:00
$fileSize = (\is_array($file['size']) && isset($file['size'][0])) ? $file['size'][0] : $file['size'];
}
2022-07-22 18:00:42 +12:00
$usage
2022-08-17 22:55:01 +12:00
->setParam('project.{scope}.network.inbound', $request->getSize() + $fileSize)
->setParam('project.{scope}.network.outbound', $response->getSize())
->submit();
2022-07-22 18:00:42 +12:00
}
});