From 64050337cf500f6c734b4a5bdd685d19686cd459 Mon Sep 17 00:00:00 2001 From: shimon Date: Sun, 24 Jul 2022 10:05:24 +0300 Subject: [PATCH] tidy up --- .gitignore | 4 +--- app/config/collections.php | 4 +--- app/controllers/api/avatars.php | 13 +++++++++---- app/controllers/api/storage.php | 4 ++-- app/controllers/general.php | 1 - app/controllers/shared/api.php | 11 +++++++---- app/init.php | 2 -- src/Appwrite/Utopia/Response.php | 7 ++----- 8 files changed, 22 insertions(+), 24 deletions(-) diff --git a/.gitignore b/.gitignore index a998122514..555f4b1d5d 100644 --- a/.gitignore +++ b/.gitignore @@ -10,6 +10,4 @@ debug/ app/sdks dev/yasd_init.php -test/tmp/ -tests/resources/disk-a/video-srt.srt -tests/resources/disk-a/video-srt.mp4 + diff --git a/app/config/collections.php b/app/config/collections.php index 26792cd7ee..7312a7cfdd 100644 --- a/app/config/collections.php +++ b/app/config/collections.php @@ -2783,11 +2783,9 @@ $collections = [ ], 'indexes' => [ [ - '$id' => '_key_accessed', + '$id' => '_key_accessedAt', 'type' => Database::INDEX_KEY, 'attributes' => ['accessedAt'], - 'lengths' => [], - 'orders' => [], ], ], ], diff --git a/app/controllers/api/avatars.php b/app/controllers/api/avatars.php index a2df53ce0f..3fbc99b22b 100644 --- a/app/controllers/api/avatars.php +++ b/app/controllers/api/avatars.php @@ -55,7 +55,8 @@ $avatarCallback = function (string $type, string $code, int $width, int $height, App::get('/v1/avatars/credit-cards/:code') ->desc('Get Credit Card Icon') - ->groups(['api', 'avatars', 'cache']) + ->groups(['api', 'avatars']) + ->label('cache', true) ->label('scope', 'avatars.read') ->label('sdk.auth', [APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_JWT]) ->label('sdk.namespace', 'avatars') @@ -73,7 +74,8 @@ App::get('/v1/avatars/credit-cards/:code') App::get('/v1/avatars/browsers/:code') ->desc('Get Browser Icon') - ->groups(['api', 'avatars', 'cache']) + ->groups(['api', 'avatars']) + ->label('cache', true) ->label('scope', 'avatars.read') ->label('sdk.auth', [APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_JWT]) ->label('sdk.namespace', 'avatars') @@ -91,7 +93,8 @@ App::get('/v1/avatars/browsers/:code') App::get('/v1/avatars/flags/:code') ->desc('Get Country Flag') - ->groups(['api', 'avatars', 'cache']) + ->groups(['api', 'avatars']) + ->label('cache', true) ->label('scope', 'avatars.read') ->label('sdk.auth', [APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_JWT]) ->label('sdk.namespace', 'avatars') @@ -110,6 +113,7 @@ App::get('/v1/avatars/flags/:code') App::get('/v1/avatars/image') ->desc('Get Image from URL') ->groups(['api', 'avatars']) + ->label('cache', true) ->label('scope', 'avatars.read') ->label('sdk.auth', [APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_JWT]) ->label('sdk.namespace', 'avatars') @@ -156,7 +160,8 @@ App::get('/v1/avatars/image') App::get('/v1/avatars/favicon') ->desc('Get Favicon') - ->groups(['api', 'avatars', 'cache']) + ->groups(['api', 'avatars']) + ->label('cache', true) ->label('scope', 'avatars.read') ->label('sdk.auth', [APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_JWT]) ->label('sdk.namespace', 'avatars') diff --git a/app/controllers/api/storage.php b/app/controllers/api/storage.php index 73d67c2138..87088ed826 100644 --- a/app/controllers/api/storage.php +++ b/app/controllers/api/storage.php @@ -800,8 +800,8 @@ App::get('/v1/storage/buckets/:bucketId/files/:fileId') App::get('/v1/storage/buckets/:bucketId/files/:fileId/preview') ->alias('/v1/storage/files/:fileId/preview', ['bucketId' => 'default']) ->desc('Get File Preview') - ->groups(['api', 'storage', 'cache']) - ->label('useCache', true) + ->groups(['api', 'storage']) + ->label('cache', true) ->label('scope', 'files.read') ->label('sdk.auth', [APP_AUTH_TYPE_SESSION, APP_AUTH_TYPE_KEY, APP_AUTH_TYPE_JWT]) ->label('sdk.namespace', 'storage') diff --git a/app/controllers/general.php b/app/controllers/general.php index 18f4825724..620f16f1c1 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -612,7 +612,6 @@ App::get('/.well-known/acme-challenge') }); include_once __DIR__ . '/shared/api.php'; -include_once __DIR__ . '/shared/cache.php'; include_once __DIR__ . '/shared/web.php'; foreach (Config::getParam('services', []) as $service) { diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 1407ffcf0e..c33946b4d7 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -47,7 +47,6 @@ App::init(function (App $utopia, Request $request, Response $response, Document } $closestLimit = null; - $roles = Authorization::getRoles(); $isPrivilegedUser = Auth::isPrivilegedUser($roles); $isAppUser = Auth::isAppUser($roles); @@ -116,7 +115,7 @@ App::init(function (App $utopia, Request $request, Response $response, Document $deletes->setProject($project); $database->setProject($project); - $useCache = $route->getLabel('useCache', false); + $useCache = $route->getLabel('cache', false); if ($useCache) { $key = md5($request->getURI() . $request->getServer('query_string')); $cache = new Cache(new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $project->getId())); @@ -133,7 +132,11 @@ App::init(function (App $utopia, Request $request, Response $response, Document Authorization::skip(fn () => $dbForProject->updateDocument('cache', $cacheLog->getId(), $cacheLog)); } $data = json_decode($data, true); - $response->file(base64_decode($data['payload']), $data['content-type'], $data['date'], 'hit'); + $response->setContentType($data['content-type']) + ->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)) . ' GMT') // 45 days cache + ->addHeader('Expires', $data['date']) + ->addHeader('X-Appwrite-Cache', 'hit') + ->file(base64_decode($data['payload'])); } } }, ['utopia', 'request', 'response', 'project', 'user', 'events', 'audits', 'mails', 'usage', 'deletes', 'database', 'dbForProject', 'mode'], 'api'); @@ -261,7 +264,7 @@ App::shutdown(function (App $utopia, Request $request, Response $response, Docum $route = $utopia->match($request); - $useCache = $route->getLabel('useCache', false); + $useCache = $route->getLabel('cache', false); if ($useCache) { $key = md5($request->getURI() . $request->getServer('query_string')); $cache = new Cache(new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $project->getId())); diff --git a/app/init.php b/app/init.php index 489170ca6a..d72123a513 100644 --- a/app/init.php +++ b/app/init.php @@ -695,8 +695,6 @@ App::setResource('layout', function ($locale) { return $layout; }, ['locale']); -App::setResource('cacheKey', fn () => ''); -App::setResource('cachePath', fn () => ''); App::setResource('locale', fn() => new Locale(App::getEnv('_APP_LOCALE', 'en'))); // Queues diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index 0c0ce1d444..82470492bd 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -381,7 +381,7 @@ class Response extends SwooleResponse * * return array */ - public function output(Document $document, string $model): string | array + public function output(Document $document, string $model): array { $data = $document; $model = $this->getModel($model); @@ -453,10 +453,7 @@ class Response extends SwooleResponse 'payload' => $data ]; - $this->setContentType($contentType) - ->addHeader('Expires', $date) - ->addHeader('X-Appwrite-Cache', $state) - ->send($data); + $this->send($data); } /**