FileUploadRequest.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. namespace App\Http\Requests\API\File;
  3. use App\Models\Enums\FileObjectType;
  4. use App\Models\User;
  5. use Illuminate\Foundation\Http\FormRequest;
  6. use Illuminate\Support\Facades\Auth;
  7. use Illuminate\Validation\Rules\Enum;
  8. use Illuminate\Validation\Rules\File;
  9. class FileUploadRequest extends FormRequest
  10. {
  11. /**
  12. * Determine if the user is authorized to make this request.
  13. */
  14. public function authorize(): bool
  15. {
  16. return true;
  17. }
  18. /**
  19. * Get the validation rules that apply to the request.
  20. *
  21. * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
  22. */
  23. public function rules(): array
  24. {
  25. return [
  26. "files.*" => [
  27. 'required',
  28. File::types(['txt', 'jpeg', 'png', 'gif', 'pdf', 'xls', 'xlsx', 'zip', 'wps', 'docx', 'doc'])
  29. ->max("1gb"),
  30. ],
  31. "object_type" => [
  32. 'required',
  33. new Enum(FileObjectType::class),
  34. ],
  35. "object_id" => [
  36. function ($attribute, $value, $fail) {
  37. $exist = FileObjectType::from($this->get("object_type"))
  38. ->modelBuilderAllowed($value)
  39. ->where("company_id", Auth::user()->company_id)
  40. ->where('id', $value)
  41. ->count();
  42. if (! $exist) {
  43. $fail('Resources without permission to access.');
  44. }
  45. }
  46. ]
  47. ];
  48. }
  49. }