feat: API for getting images

This commit is contained in:
b1ek 2024-08-20 14:47:29 +10:00
parent c0edeca406
commit 1fc412ba57
Signed by: blek
GPG Key ID: 14546221E3595D0C
1 changed files with 16 additions and 0 deletions

View File

@ -2,8 +2,12 @@
namespace app\controllers; namespace app\controllers;
use app\models\Parameter; use app\models\Parameter;
use app\models\Image;
use yii\data\ActiveDataProvider; use yii\data\ActiveDataProvider;
use yii\rest\IndexAction; use yii\rest\IndexAction;
use yii\web\NotFoundHttpException;
use yii\web\Request;
use yii\web\Response;
class ApiController extends \yii\rest\Controller class ApiController extends \yii\rest\Controller
{ {
@ -12,6 +16,18 @@ 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)
{
$img = Image::find()->where([ 'sha256' => $request->getQueryParam('sha256') ])->one();
if (!$img) {
throw new NotFoundHttpException();
}
\Yii::$app->response->format = Response::FORMAT_RAW;
\Yii::$app->response->headers->set('Content-Type', $img->mime);
return file_get_contents(env('UPLOADS_PATH') . '/' . $img->sha256);
}
public function verbs() public function verbs()
{ {
return [ return [