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.
 
 
 
 

71 lines
2.4 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('user_id', auth()->id())->where('collection_id',app()->collection->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(app()->file->getPath(), 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'],
'published_at' => ['date_format:Y-m-d H:i:s', 'nullable'],
];
}
}