FileUploadRequest.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace App\Http\Requests\API\File;
  3. use App\BO\BimFileBO;
  4. use App\Models\Enums\FileObjectType;
  5. use Illuminate\Foundation\Http\FormRequest;
  6. use Illuminate\Support\Arr;
  7. use Illuminate\Support\Facades\Auth;
  8. use Illuminate\Validation\Rules\Enum;
  9. use Illuminate\Validation\Rules\File;
  10. class FileUploadRequest extends FormRequest
  11. {
  12. /**
  13. * Determine if the user is authorized to make this request.
  14. */
  15. public function authorize(): bool
  16. {
  17. return true;
  18. }
  19. /**
  20. * Get the validation rules that apply to the request.
  21. *
  22. * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
  23. */
  24. public function rules(): array
  25. {
  26. $routeName = $this->route()->getName();
  27. switch ($routeName) {
  28. case 'file.cos.token':
  29. return $this->cosTokenRules();
  30. case 'file.modelUpload':
  31. return $this->modelUploadRules();
  32. }
  33. $baseRules = $this->baseRules();
  34. $rules = [
  35. "files.*" => 'required',
  36. "name" => '',
  37. "source" => "in:1,2",
  38. ];
  39. $rules = array_merge($baseRules, $rules);
  40. $rules['file.*'][] = $this->get("source", 1) == 1
  41. ? File::types(['jpeg', 'png', 'gif'])->max("10mb")
  42. : File::types(['txt', 'jpeg', 'png', 'gif', 'pdf', 'xls', 'xlsx', 'zip', 'wps', 'docx', 'doc', 'pptx', 'mp4', 'avi', 'mpeg', 'mov'])->max("2gb");
  43. return $rules;
  44. }
  45. private function baseRules()
  46. {
  47. return [
  48. "object_type" => ['required', new Enum(FileObjectType::class)],
  49. "object_id" => [
  50. function ($attribute, $value, $fail) {
  51. $exist = FileObjectType::from($this->get("object_type"))
  52. ->modelBuilderAllowed($value, ($this->get("is_action") === '0') ? true : ($this->get("is_action") === '1' ? false : false))
  53. ->where("company_id", Auth::user()->company_id)
  54. ->where('id', $value)
  55. ->count();
  56. if (!$exist) {
  57. $fail('Resources without permission to access.');
  58. }
  59. }
  60. ],
  61. ];
  62. }
  63. protected function cosTokenRules(): array
  64. {
  65. return Arr::only($this->baseRules(), ['object_type']);
  66. }
  67. protected function modelUploadRules(): array
  68. {
  69. $baseRules = $this->baseRules();
  70. $rules = [
  71. 'folder_id' => 'integer',
  72. 'files' => 'required',
  73. 'files.*.size' => 'required|integer',
  74. 'files.*.title' => 'required|string',
  75. 'files.*.pathname' => 'required|string',
  76. 'configJson' => 'array',
  77. ];
  78. $configJsonRules = BimFileBO::configJsonRules();
  79. foreach ($configJsonRules as $key => $value) {
  80. $newKey = 'configJson.' . $key;
  81. $configJsonRules[$newKey] = $configJsonRules[$key];
  82. unset($configJsonRules[$key]);
  83. }
  84. return array_merge($baseRules, $rules, $configJsonRules);
  85. }
  86. }