You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

73 lines
2.5 KiB

<?php
namespace App\Http\Requests;
use App\Models\File;
use App\Utilities\Polices\BasePolicy;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Storage;
class FileStoreRequest extends FormRequest
{
public function isImage($file)
{
if (@is_array(getimagesize($file))) {
return true;
} else {
return false;
}
}
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
if (!app()->collection->tmp_support && !$this->model_id) {
return false;
}
if (app()->collection->count !== 1 && (app()->collection->count <= File::where('model_id', auth()->id())->count()) && !app()->collection->tmp_support) {
return false;
}
if (!app()->bound('file') && is_null($this->file('file'))) {
return false;
}
if (!$this->hasFile('file')) {
$this->replace([
'file' => new \Illuminate\Http\File(Storage::disk(app()->collection->disk)->path(app()->file->server_path . app()->file->uuid . '.' . app()->collection->ext), app()->file->uuid . '.' . app()->collection->ext)
]);
}
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules()
{
return [
"file" => [
"mimes:" . app()->collection->getExts(),
"mimetypes:" . app()->collection->getMimeTypes(),
!$this->isImage(request()->file->path()) ?: "dimensions:min_width=" . app()->collection->min_width . ",min_height=" . app()->collection->min_height . ',max_width=' . app()->collection->max_width . ',max_height=' . app()->collection->max_height,
"max:" . app()->collection->max_file_size,
"min:" . app()->collection->min_file_size,
],
"alts" => [app()->collection->alt_required ? "required" : "nullable", 'array'],
"alts.*" => [app()->collection->alt_required ? "required" : "nullable", 'max:1000'],
"description" => [app()->collection->description_required ? "required" : "nullable", 'max:300'],
'original_name' => ["string", "nullable", 'max:300'],
'public' => ['boolean', 'nullable'],
'published_at' => ['date_format:Y-m-d H:i:s', 'nullable'],
];
}
}