KeepDirectoryUploadRequest.php 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace App\Http\Requests\API\File;
  3. use App\Models\Enums\FileObjectType;
  4. use Illuminate\Foundation\Http\FormRequest;
  5. use Illuminate\Http\UploadedFile;
  6. use Illuminate\Validation\Rules\Enum;
  7. use Illuminate\Validation\Rules\File;
  8. class KeepDirectoryUploadRequest extends FormRequest
  9. {
  10. /**
  11. * Determine if the user is authorized to make this request.
  12. */
  13. public function authorize(): bool
  14. {
  15. return true;
  16. }
  17. /**
  18. * Get the validation rules that apply to the request.
  19. *
  20. * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
  21. */
  22. public function rules(): array
  23. {
  24. $rules = [
  25. "files" => "required|array",
  26. "folders" => "required|array",
  27. "object_type" => [
  28. "required",
  29. new Enum(FileObjectType::class),
  30. ],
  31. "object_id" => "min:0",
  32. "names" => "array",
  33. "folder_id" => "min:0",
  34. "uuid" => "max:40"
  35. ];
  36. if (! $this->request->has("object_id")) {
  37. $rules['uuid'] = "required|max:40";
  38. }
  39. $extensions = ['jpg','txt', 'jpeg', 'png', 'gif', 'pdf', 'xls', 'xlsx', 'zip', 'wps', 'docx', 'doc'];
  40. if ($this->request->get("object_type") == FileObjectType::CONTAINER->value) {
  41. $extensions = [...$extensions, ...config("bim.extensions")];
  42. }
  43. $rules['files.*'] = [
  44. File::default()->max("2gb"),
  45. function ($attribute, UploadedFile $value, $fail) use ($extensions) {
  46. if (! in_array($value->getClientOriginalExtension(), $extensions) && !in_array($value->getExtension(), $extensions)) {
  47. $fail(sprintf("Please select the correct file, the following files are supported: %s", implode(",", $extensions)));
  48. }
  49. }
  50. ];
  51. return $rules;
  52. }
  53. }