UploadRequest.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace App\Http\Requests\API\File;
  3. use App\Http\Requests\RuleHelper;
  4. use Illuminate\Foundation\Http\FormRequest;
  5. use Illuminate\Support\Facades\Storage;
  6. class UploadRequest extends FormRequest
  7. {
  8. use RuleHelper;
  9. /**
  10. * Determine if the user is authorized to make this request.
  11. */
  12. public function authorize(): bool
  13. {
  14. return true;
  15. }
  16. /**
  17. * Get the validation rules that apply to the request.
  18. *
  19. * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
  20. */
  21. public function rules(): array
  22. {
  23. return [
  24. 'file' => [
  25. 'required',
  26. 'max:52428800',
  27. ],
  28. 'fileName' => [
  29. 'nullable',
  30. 'max:150',
  31. function ($attribute, $value, $fail) {
  32. // 检查文件是否存在默认存储对象系统中
  33. if (Storage::exists('uploads/' .date('Ymd/'). $value)) {
  34. $fail('File already exists');
  35. }
  36. },
  37. ]
  38. ];
  39. }
  40. }