Drop the separate ai.mcp.upload.max_size_mb default and reuse Media\Type limits so MCP matches web/API (1GB video ceiling with per-type enforcement). Co-authored-by: Cursor <cursoragent@cursor.com>
67 lines
1.8 KiB
PHP
67 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Requests\Api;
|
|
|
|
use App\Enums\Media\Type as MediaType;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Validation\Validator;
|
|
|
|
class StoreUploadRequest extends FormRequest
|
|
{
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
$allowed = implode(',', array_merge(
|
|
MediaType::Image->allowedMimeTypes(),
|
|
MediaType::Video->allowedMimeTypes(),
|
|
MediaType::Document->allowedMimeTypes(),
|
|
));
|
|
|
|
return [
|
|
// Largest per-type cap as the FormRequest upper bound; per-type
|
|
// enforcement runs in withValidator() below — same pattern as
|
|
// StoreMediaRequest + PostController::storeMedia.
|
|
'media' => [
|
|
'required',
|
|
'file',
|
|
'max:'.MediaType::Video->maxSizeInKb(),
|
|
"mimetypes:{$allowed}",
|
|
],
|
|
];
|
|
}
|
|
|
|
public function withValidator(Validator $validator): void
|
|
{
|
|
$validator->after(function (Validator $validator): void {
|
|
$file = $this->file('media');
|
|
|
|
if ($file === null || $validator->errors()->isNotEmpty()) {
|
|
return;
|
|
}
|
|
|
|
$type = MediaType::fromMime((string) $file->getMimeType());
|
|
|
|
if ($type === null) {
|
|
$validator->errors()->add('media', 'This file type is not supported.');
|
|
|
|
return;
|
|
}
|
|
|
|
if ($file->getSize() > $type->maxSizeInBytes()) {
|
|
$validator->errors()->add(
|
|
'media',
|
|
"File size exceeds the maximum allowed for {$type->value} ({$type->maxSizeInMb()} MB).",
|
|
);
|
|
}
|
|
});
|
|
}
|
|
}
|