123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- <?php
- namespace App\Http\Requests\API\File;
- use App\BO\BimFileBO;
- use App\Models\Enums\FileObjectType;
- use Illuminate\Foundation\Http\FormRequest;
- use Illuminate\Support\Arr;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Validation\Rules\Enum;
- use Illuminate\Validation\Rules\File;
- class FileUploadRequest extends FormRequest
- {
- /**
- * Determine if the user is authorized to make this request.
- */
- public function authorize(): bool
- {
- return true;
- }
- /**
- * Get the validation rules that apply to the request.
- *
- * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
- */
- public function rules(): array
- {
- $routeName = $this->route()->getName();
- switch ($routeName) {
- case 'file.cos.token':
- return $this->cosTokenRules();
- case 'file.modelUpload':
- return $this->modelUploadRules();
- }
- $baseRules = $this->baseRules();
- $rules = [
- "files.*" => 'required',
- "name" => '',
- "source" => "in:1,2",
- ];
- $rules = array_merge($baseRules, $rules);
- $rules['file.*'][] = $this->get("source", 1) == 1
- ? File::types(['jpeg', 'png', 'gif'])->max("10mb")
- : File::types(['txt', 'jpeg', 'png', 'gif', 'pdf', 'xls', 'xlsx', 'zip', 'wps', 'docx', 'doc', 'pptx', 'mp4', 'avi', 'mpeg', 'mov'])->max("2gb");
- return $rules;
- }
- private function baseRules()
- {
- return [
- "object_type" => ['required', new Enum(FileObjectType::class)],
- "object_id" => [
- function ($attribute, $value, $fail) {
- $exist = FileObjectType::from($this->get("object_type"))
- ->modelBuilderAllowed($value, ($this->get("is_action") === '0') ? true : ($this->get("is_action") === '1' ? false : false))
- ->where("company_id", Auth::user()->company_id)
- ->where('id', $value)
- ->count();
- if (!$exist) {
- $fail('Resources without permission to access.');
- }
- }
- ],
- ];
- }
- protected function cosTokenRules(): array
- {
- return Arr::only($this->baseRules(), ['object_type']);
- }
- protected function modelUploadRules(): array
- {
- $baseRules = $this->baseRules();
- $rules = [
- 'folder_id' => 'integer',
- 'files' => 'required',
- 'files.*.size' => 'required|integer',
- 'files.*.title' => 'required|string',
- 'files.*.pathname' => 'required|string',
- 'configJson' => 'array',
- ];
- $configJsonRules = BimFileBO::configJsonRules();
- foreach ($configJsonRules as $key => $value) {
- $newKey = 'configJson.' . $key;
- $configJsonRules[$newKey] = $configJsonRules[$key];
- unset($configJsonRules[$key]);
- }
- return array_merge($baseRules, $rules, $configJsonRules);
- }
- }
|