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:
68
src/flextype/core/Endpoints/Images.php
Normal file
68
src/flextype/core/Endpoints/Images.php
Normal 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);
|
||||
}
|
||||
}
|
28
src/flextype/routes/endpoints/images.php
Normal file
28
src/flextype/routes/endpoints/images.php
Normal 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']);
|
Reference in New Issue
Block a user