From e1eabe79adfca96b04421fda67f2e9748406551e Mon Sep 17 00:00:00 2001 From: Awilum Date: Tue, 10 Aug 2021 16:50:15 +0300 Subject: [PATCH] feat(endpoints): add images endpoints #565 --- src/flextype/core/Endpoints/Images.php | 68 ++++++++++++++++++++++++ src/flextype/routes/endpoints/images.php | 28 ++++++++++ 2 files changed, 96 insertions(+) create mode 100644 src/flextype/core/Endpoints/Images.php create mode 100644 src/flextype/routes/endpoints/images.php diff --git a/src/flextype/core/Endpoints/Images.php b/src/flextype/core/Endpoints/Images.php new file mode 100644 index 00000000..c6cabebb --- /dev/null +++ b/src/flextype/core/Endpoints/Images.php @@ -0,0 +1,68 @@ +getQueryParams(); + + // Check is utils api enabled + if (! registry()->get('flextype.settings.api.images.enabled')) { + return $this->getApiResponse($response, $this->getStatusCodeMessage(400), 400); + } + + // Check is token param exists + if (! isset($queryParams['token'])) { + return $this->getApiResponse($response, $this->getStatusCodeMessage(400), 400); + } + + // Check is token exists + if (! tokens()->has($queryParams['token'])) { + return $this->getApiResponse($response, $this->getStatusCodeMessage(401), 401); + } + + // Fetch token + $tokenData = tokens()->fetch($queryParams['token']); + + // Check token state and limit_calls + if ( + $tokenData['state'] === 'disabled' || + ($tokenData['limit_calls'] !== 0 && $tokenData['calls'] >= $tokenData['limit_calls']) + ) { + return $this->getApiResponse($response, $this->getStatusCodeMessage(400), 400); + } + + // Update token calls + tokens()->update($queryParams['token'], ['calls' => $tokenData['calls'] + 1]); + + // Check is file exists + if (! filesystem()->file(PATH['project'] . '/uploads/' . $path)->exists()) { + return $this->getApiResponse($response, $this->getStatusCodeMessage(404), 404); + } + + // Return image response + return container()->get('images')->getImageResponse($path, $queryParams); + } +} diff --git a/src/flextype/routes/endpoints/images.php b/src/flextype/routes/endpoints/images.php new file mode 100644 index 00000000..fe46c39e --- /dev/null +++ b/src/flextype/routes/endpoints/images.php @@ -0,0 +1,28 @@ +get('/api/images/{path:.+}', [Images::class, 'fetch']); \ No newline at end of file