feat: add /api/image and make the action accept image/* header

This commit is contained in:
b1ek 2024-08-20 19:23:11 +10:00
parent 4201519e1c
commit 1839181887
Signed by: blek
GPG Key ID: 14546221E3595D0C
2 changed files with 54 additions and 13 deletions

View File

@ -3,8 +3,7 @@
namespace app\controllers; namespace app\controllers;
use app\models\Parameter; use app\models\Parameter;
use app\models\Image; use app\models\Image;
use yii\data\ActiveDataProvider; use yii\filters\ContentNegotiator;
use yii\rest\IndexAction;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
use yii\web\Request; use yii\web\Request;
use yii\web\Response; use yii\web\Response;
@ -16,16 +15,15 @@ class ApiController extends \yii\rest\Controller
return Parameter::find()->with(['icon', 'iconGray'])->all(); return Parameter::find()->with(['icon', 'iconGray'])->all();
} }
public function actionImage(Request $request) public function actionImage(Request $request, Response $response)
{ {
$img = Image::find()->where([ 'sha256' => $request->getQueryParam('sha256') ])->one(); $img = Image::find()->where([ 'sha256' => $request->getQueryParam('sha256') ])->one();
if (!$img) { if (!$img) {
throw new NotFoundHttpException(); throw new NotFoundHttpException();
} }
\Yii::$app->response->format = Response::FORMAT_RAW; $response->headers->set('X-Image-Data', json_encode($img->getAttributes()));
\Yii::$app->response->headers->set('Content-Type', $img->mime); return $response->sendFile(env('UPLOADS_PATH') . '/' . $img->sha256, $img->original_name);
return file_get_contents(env('UPLOADS_PATH') . '/' . $img->sha256);
} }
public function verbs() public function verbs()
@ -34,4 +32,17 @@ class ApiController extends \yii\rest\Controller
'index' => [ 'GET' ], 'index' => [ 'GET' ],
]; ];
} }
public function behaviors()
{
return [
[
'class' => ContentNegotiator::class,
'formats' => [
'image/*' => Response::FORMAT_RAW,
'application/json' => Response::FORMAT_JSON
]
]
];
}
} }

View File

@ -5,12 +5,42 @@ openapi: '3.1.0'
tags: tags:
- name: Routes - name: Routes
paths: paths:
/api/image:
get:
tags:
- Routes
summary: Get image
description: |-
Get image by SHA256.
This method is designed to be referenced via `<img>`'s `src` attribute, so it returns the image with it's content type in the header.
The image data, such as original name is available in JSON in header `X-Image-Data`
parameters:
- name: sha256
schema:
type: string
in: query
responses:
200:
description: |-
# Important: application/json body is the `X-Image-Data` header value, not the actual body!
The actual body is the raw image. Its content type is in the `Content-Type` header.
content:
image/*:
type:
application/json:
schema:
$ref: '#/components/schemas/Image'
/api: /api:
get: get:
tags: tags:
- Routes - Routes
summary: Get all parameters summary: Get all parameters
description: Get all parameters description: |-
This route will get all parameters.
To get the image itself, use /api/
responses: responses:
200: 200:
description: OK description: OK
@ -22,7 +52,7 @@ paths:
$ref: '#/components/schemas/Parameter' $ref: '#/components/schemas/Parameter'
example: [ example: [
{ {
"id": 0, "id": 1,
"title": "A thing happening somewhere", "title": "A thing happening somewhere",
"type": 1, "type": 1,
"icon": null, "icon": null,
@ -33,13 +63,13 @@ paths:
"title": "The fox jumping over a cat", "title": "The fox jumping over a cat",
"type": 1, "type": 1,
"icon": { "icon": {
"id": 1,
"original_name": "fox_jump.jpeg", "original_name": "fox_jump.jpeg",
"url": "/images/SHA256_HASH.jpeg",
"sha256": "SHA256" "sha256": "SHA256"
}, },
"icon_gray": { "icon_gray": {
"id": 2,
"original_name": "fox_jump.gray.jpeg", "original_name": "fox_jump.gray.jpeg",
"url": "/images/SHA256_HASH.jpeg",
"sha256": "SHA256" "sha256": "SHA256"
} }
} }
@ -49,12 +79,12 @@ components:
Image: Image:
type: object type: object
properties: properties:
id:
type: number
example: 1
source_name: source_name:
type: string type: string
example: "source_file.jpeg" example: "source_file.jpeg"
url:
type: string
example: "/images/SHA256_HASH.jpeg"
sha256: sha256:
type: string type: string
required: required: