2024-08-19 14:44:16 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace app\controllers;
|
|
|
|
use app\models\Parameter;
|
2024-08-20 06:47:29 +02:00
|
|
|
use app\models\Image;
|
2024-08-20 11:23:11 +02:00
|
|
|
use yii\filters\ContentNegotiator;
|
2024-08-20 06:47:29 +02:00
|
|
|
use yii\web\NotFoundHttpException;
|
|
|
|
use yii\web\Request;
|
|
|
|
use yii\web\Response;
|
2024-08-19 14:44:16 +02:00
|
|
|
|
|
|
|
class ApiController extends \yii\rest\Controller
|
|
|
|
{
|
|
|
|
public function actionIndex()
|
|
|
|
{
|
|
|
|
return Parameter::find()->with(['icon', 'iconGray'])->all();
|
|
|
|
}
|
|
|
|
|
2024-08-20 11:23:11 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2024-08-20 11:23:11 +02:00
|
|
|
$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
|
|
|
}
|
|
|
|
|
2024-08-19 14:44:16 +02:00
|
|
|
public function verbs()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
'index' => [ 'GET' ],
|
|
|
|
];
|
|
|
|
}
|
2024-08-20 11:23:11 +02:00
|
|
|
|
|
|
|
public function behaviors()
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
[
|
|
|
|
'class' => ContentNegotiator::class,
|
|
|
|
'formats' => [
|
|
|
|
'image/*' => Response::FORMAT_RAW,
|
|
|
|
'application/json' => Response::FORMAT_JSON
|
|
|
|
]
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
2024-08-19 14:44:16 +02:00
|
|
|
}
|