1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-07 21:56:33 +02:00

feat(endpoints): add images endpoints #565

This commit is contained in:
Awilum
2021-08-10 16:50:15 +03:00
parent 808ed2fc86
commit e1eabe79ad
2 changed files with 96 additions and 0 deletions

View File

@@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype\Endpoints;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
class Images extends Endpoints
{
/**
* Fetch image.
*
* @param string $path Image path.
* @param ServerRequestInterface $request PSR7 request.
* @param ResponseInterface $response PSR7 response.
*
* @return ResponseInterface Response.
*/
public function fetch(string $path, ServerRequestInterface $request, ResponseInterface $response): ResponseInterface
{
// Get Query Params
$queryParams = $request->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);
}
}

View File

@@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
/**
* Flextype (https://flextype.org)
* Founded by Sergey Romanenko and maintained by Flextype Community.
*/
namespace Flextype;
use Flextype\Endpoints\Images;
/**
* Fetch image
*
* endpoint: GET /api/images
*
* Parameters:
* path - [REQUIRED] - The file path with valid params for image manipulations.
*
* Query:
* token - [REQUIRED] - Valid public token.
*
* Returns:
* Image file
*/
app()->get('/api/images/{path:.+}', [Images::class, 'fetch']);