FileUploadRequest.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. $rules = [
  26. "files.*" => [
  27. 'required',
  28. ],
  29. "object_type" => [
  30. 'required',
  31. new Enum(FileObjectType::class),
  32. ],
  33. "object_id" => [
  34. function ($attribute, $value, $fail) {
  35. $exist = FileObjectType::from($this->get("object_type"))
  36. ->modelBuilderAllowed($value)
  37. ->where("company_id", Auth::user()->company_id)
  38. ->where('id', $value)
  39. ->count();
  40. if (! $exist) {
  41. $fail('Resources without permission to access.');
  42. }
  43. }
  44. ],
  45. "source" => "in:1,2",
  46. ];
  47. $rules['file.*'][] = $this->get("source", 1) == 1
  48. ? File::types(['jpeg', 'png', 'gif'])->max("10mb")
  49. : File::types(['txt', 'jpeg', 'png', 'gif', 'pdf', 'xls', 'xlsx', 'zip', 'wps', 'docx', 'doc'])->max("2gb");
  50. return $rules;
  51. }
  52. }