From d09bf306ce6c7a386c1dc5fe8200c8a33dfd8ede Mon Sep 17 00:00:00 2001 From: shimon Date: Tue, 26 Jul 2022 15:50:33 +0300 Subject: [PATCH] response changes --- app/controllers/api/avatars.php | 16 ++++-------- app/controllers/api/storage.php | 2 -- app/controllers/shared/api.php | 45 ++++++++++++++++++-------------- app/workers/deletes.php | 2 +- src/Appwrite/Utopia/Response.php | 9 +++---- 5 files changed, 36 insertions(+), 38 deletions(-) diff --git a/app/controllers/api/avatars.php b/app/controllers/api/avatars.php index c8ae25bd7..b6d9d7500 100644 --- a/app/controllers/api/avatars.php +++ b/app/controllers/api/avatars.php @@ -47,8 +47,6 @@ $avatarCallback = function (string $type, string $code, int $width, int $height, $output = (empty($output)) ? $type : $output; $data = $image->output($output, $quality); $response - ->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 30)) . ' GMT') - ->addHeader('X-Appwrite-Cache', 'miss') ->setContentType('image/png') ->file($data, 'image/png'); @@ -155,8 +153,6 @@ App::get('/v1/avatars/image') $data = $image->output($output, $quality); $response - ->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 30)) . ' GMT') - ->addHeader('X-Appwrite-Cache', 'miss') ->setContentType('image/png') ->file($data, 'image/png'); @@ -268,9 +264,10 @@ App::get('/v1/avatars/favicon') if (empty($data) || (\mb_substr($data, 0, 5) === 'addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 30)) . ' GMT') - ->addHeader('X-Appwrite-Cache', 'miss') - ->file($data, 'image/png'); + $response + ->setContentType('image/x-icon') + ->file($data, 'image/x-icon') + ; } $fetch = @\file_get_contents($outputHref, false); @@ -285,8 +282,6 @@ App::get('/v1/avatars/favicon') $data = $image->output($output, $quality); $response - ->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 30)) . ' GMT') - ->addHeader('X-Appwrite-Cache', 'miss') ->setContentType('image/png') ->file($data, 'image/png'); @@ -329,8 +324,7 @@ App::get('/v1/avatars/qr') $image->crop((int) $size, (int) $size); $response - ->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 30)) . ' GMT') // 30 days cache - ->addHeader('X-Appwrite-Cache', 'miss') + ->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 45)) . ' GMT') // 45 days cache ->setContentType('image/png') ->send($image->output('png', 9)); }); diff --git a/app/controllers/api/storage.php b/app/controllers/api/storage.php index a58859d56..37ec9f17b 100644 --- a/app/controllers/api/storage.php +++ b/app/controllers/api/storage.php @@ -959,8 +959,6 @@ App::get('/v1/storage/buckets/:bucketId/files/:fileId/preview') $contentType = (\array_key_exists($output, $outputs)) ? $outputs[$output] : $outputs['jpg']; $response - ->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 30)) . ' GMT') - ->addHeader('X-Appwrite-Cache', 'miss') ->setContentType($contentType) ->file($data, $contentType); diff --git a/app/controllers/shared/api.php b/app/controllers/shared/api.php index 24b154718..b10005962 100644 --- a/app/controllers/shared/api.php +++ b/app/controllers/shared/api.php @@ -117,28 +117,29 @@ App::init(function (App $utopia, Request $request, Response $response, Document $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())); - $data = $cache->load($key, 60 * 60 * 24 * 30 * 1); + $key = md5($request->getURI() . implode('*', $request->getParams())); + $cache = new Cache(new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $project->getId())); + $timestamp = 60 * 60 * 24 * 30; + $data = $cache->load($key, $timestamp); if (!empty($data)) { $cacheLog = Authorization::skip(fn () => $dbForProject->getDocument('cache', $key)); if ($cacheLog->isEmpty()) { Authorization::skip(fn () => $dbForProject->createDocument('cache', new Document([ - '$id' => $key, - 'accessedAt' => time(), - ]))); + '$id' => $key, + 'accessedAt' => time(), + ]))); } else { $cacheLog->setAttribute('accessedAt', time()); Authorization::skip(fn () => $dbForProject->updateDocument('cache', $cacheLog->getId(), $cacheLog)); } $data = json_decode($data, true); - + $expire = \date('D, d M Y H:i:s', \time() + $timestamp) . ' GMT'; $response - ->addHeader('Expires', \date('D, d M Y H:i:s', \time() + (60 * 60 * 24 * 30)) . ' GMT') - ->addHeader('X-Appwrite-Cache', 'hit') - ->setContentType($data['content-type']) - ->file(base64_decode($data['payload']), $data['content-type'], $data['date']); + ->addHeader('Expires', $expire) + ->addHeader('X-Appwrite-Cache', 'hit') + ->setContentType($data['content-type']) + ->file(base64_decode($data['payload']), $data['content-type']); } } }, ['utopia', 'request', 'response', 'project', 'user', 'events', 'audits', 'mails', 'usage', 'deletes', 'database', 'dbForProject', 'mode'], 'api'); @@ -268,22 +269,28 @@ App::shutdown(function (App $utopia, Request $request, Response $response, Docum $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())); - $data = $response->getPayload(); + $key = md5($request->getURI() . implode('*', $request->getParams())); + $cache = new Cache(new Filesystem(APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $project->getId())); + $data = $response->getPayload(); + $timestamp = 60 * 60 * 24 * 30; if (!empty($data)) { $cacheLog = $dbForProject->getDocument('cache', $key); if ($cacheLog->isEmpty()) { Authorization::skip(fn () => $dbForProject->createDocument('cache', new Document([ - '$id' => $key, - 'accessedAt' => time(), - ]))); + '$id' => $key, + 'accessedAt' => time(), + ]))); } else { $cacheLog->setAttribute('accessedAt', time()); Authorization::skip(fn () => $dbForProject->updateDocument('cache', $cacheLog->getId(), $cacheLog)); } - $data['payload'] = base64_encode($data['payload']); - $cache->save($key, json_encode($data)); + if (!empty($data['payload'])) { + $data['payload'] = base64_encode($data['payload']); + $cache->save($key, json_encode($data)); + } + $response + ->addHeader('Expires', \date('D, d M Y H:i:s', \time() + $timestamp) . ' GMT') + ->addHeader('X-Appwrite-Cache', 'miss'); } } diff --git a/app/workers/deletes.php b/app/workers/deletes.php index 1be68965c..c0920ad1d 100644 --- a/app/workers/deletes.php +++ b/app/workers/deletes.php @@ -147,7 +147,7 @@ class DeletesV1 extends Worker [new Query('accessedAt', Query::TYPE_LESSER, [$timestamp])], $dbForProject, function (Document $document) use ($cache, $projectId) { - $path = APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $projectId . DIRECTORY_SEPARATOR . $document->getId(); + $path = APP_STORAGE_CACHE . DIRECTORY_SEPARATOR . 'app-' . $projectId . DIRECTORY_SEPARATOR . $document->getId(); if ($cache->purge($document->getId())) { Console::success('Deleting cache file: ' . $path); diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index 98c02e524..68608a79c 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -457,10 +457,9 @@ class Response extends SwooleResponse public function file(string $data, string $contentType): void { $this->payload = [ - 'content-type' => $contentType, - 'payload' => $data - ]; - + 'content-type' => $contentType, + 'payload' => $data + ]; $this->send($data); } @@ -491,7 +490,7 @@ class Response extends SwooleResponse /** * @return array */ - public function getPayload(): string | array + public function getPayload(): array { return $this->payload; }