koel/app/Http/Requests/API/MediaImageUpdateRequest.php

39 lines
861 B
PHP
Raw Normal View History

<?php
namespace App\Http\Requests\API;
use App\Rules\ImageData;
2022-07-29 06:47:10 +00:00
abstract class MediaImageUpdateRequest extends Request
{
public function authorize(): bool
{
return auth()->user()->is_admin;
}
2020-12-22 20:11:22 +00:00
/** @return array<mixed> */
public function rules(): array
{
return [
2020-09-06 21:20:42 +00:00
$this->getImageFieldName() => ['string', 'required', new ImageData()],
];
}
public function getFileContentAsBinaryString(): string
{
2020-12-22 20:11:22 +00:00
[, $data] = explode(',', $this->{$this->getImageFieldName()});
2020-12-22 20:11:22 +00:00
return base64_decode($data, true);
}
public function getFileExtension(): string
{
2020-12-22 20:11:22 +00:00
[$type,] = explode(';', $this->{$this->getImageFieldName()});
[, $extension] = explode('/', $type);
return $extension;
}
abstract protected function getImageFieldName(): string;
}