banki.test/controllers/ApiController.php

53 lines
1.4 KiB
PHP
Raw Permalink Normal View History

<?php
namespace app\controllers;
use app\models\Parameter;
2024-08-20 06:47:29 +02:00
use app\models\Image;
use yii\filters\ContentNegotiator;
2024-08-20 06:47:29 +02:00
use yii\web\NotFoundHttpException;
use yii\web\Request;
use yii\web\Response;
class ApiController extends \yii\rest\Controller
{
public function actionIndex()
{
2024-08-21 03:48:54 +02:00
return [
'images' => Image::find()->join('INNER JOIN', 'parameters', 'parameters.icon_id = image.id OR parameters.icon_gray_id = image.id')->all(),
'params' => Parameter::find()->all(),
];
}
public function actionImage(Request $request, Response $response)
2024-08-20 06:47:29 +02:00
{
$img = Image::find()->where([ 'sha256' => $request->getQueryParam('sha256') ])->one();
if (!$img) {
throw new NotFoundHttpException();
}
$response->headers->set('Content-Type', $img->mime);
$response->headers->set('X-Image-Data', json_encode($img->getAttributes()));
return $response->sendFile(env('UPLOADS_PATH') . '/' . $img->sha256, $img->original_name);
2024-08-20 06:47:29 +02:00
}
public function verbs()
{
return [
'index' => [ 'GET' ],
];
}
public function behaviors()
{
return [
[
'class' => ContentNegotiator::class,
'formats' => [
'image/*' => Response::FORMAT_RAW,
'application/json' => Response::FORMAT_JSON
]
]
];
}
}