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.

70 lines
2.4 KiB

  1. <?php
  2. namespace App\Http\Requests;
  3. use App\Models\File;
  4. use App\Utilities\Polices\BasePolicy;
  5. use Illuminate\Foundation\Http\FormRequest;
  6. use Illuminate\Support\Facades\Storage;
  7. class FileStoreRequest extends FormRequest
  8. {
  9. public function isImage($file)
  10. {
  11. if (@is_array(getimagesize($file))) {
  12. return true;
  13. } else {
  14. return false;
  15. }
  16. }
  17. /**
  18. * Determine if the user is authorized to make this request.
  19. *
  20. * @return bool
  21. */
  22. public function authorize()
  23. {
  24. if (!app()->collection->tmp_support && !$this->model_id) {
  25. return false;
  26. }
  27. 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) {
  28. return false;
  29. }
  30. if (!app()->bound('file') && is_null($this->file('file'))) {
  31. return false;
  32. }
  33. if (!$this->hasFile('file')) {
  34. $this->replace([
  35. 'file' => new \Illuminate\Http\File(app()->file->getPath(), app()->file->uuid . '.' . app()->collection->ext)
  36. ]);
  37. }
  38. return true;
  39. }
  40. /**
  41. * Get the validation rules that apply to the request.
  42. *
  43. * @return array<string, mixed>
  44. */
  45. public function rules()
  46. {
  47. return [
  48. "file" => [
  49. "mimes:" . app()->collection->getExts(),
  50. "mimetypes:" . app()->collection->getMimeTypes(),
  51. !$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,
  52. "max:" . app()->collection->max_file_size,
  53. "min:" . app()->collection->min_file_size,
  54. ],
  55. "alts" => [app()->collection->alt_required ? "required" : "nullable", 'array'],
  56. "alts.*" => [app()->collection->alt_required ? "required" : "nullable", 'max:1000'],
  57. "description" => [app()->collection->description_required ? "required" : "nullable", 'max:300'],
  58. 'original_name' => ["string", "nullable", 'max:300'],
  59. 'published_at' => ['date_format:Y-m-d H:i:s', 'nullable'],
  60. ];
  61. }
  62. }