Skip to main content

Module validate

Module validate 

Source
Expand description

文件上传校验模块 — 对齐 PHP think\Validate 文件校验规则 + app\common\library\storage\Driver::validate

本模块实现文件类型/大小校验机制,对齐 PHP:

  • think\Validate::fileSize / fileExt / fileMime 三个校验规则 (vendor framework/src/think/Validate.php 第 957-1062 行)
  • app\common\library\storage\Driver::validate 业务层校验驱动 (第 25-46 行,默认规则 + 自定义消息)
  • app\api\controller\file\Upload.php 文件类型分类逻辑 (第 62-69 行,image/video/file 三类)

§PHP 对齐

§核心类映射

PHP 类/方法Rust 结构/方法说明
Driver::validate($name, $fileInfo, $sence)FileValidator::validate_image图片校验驱动
validate([$name=>['fileSize'=>...]])FileValidateRule校验规则
$name.'.fileSize' => '最大可上传2M图片'FileValidateMessages校验消息
Validate::fileExt() / checkExt()FileValidator::check_ext扩展名校验
Validate::fileMime() / checkMime()FileValidator::check_mimeMIME 校验
Validate::fileSize() / checkSize()FileValidator::check_size大小校验
in_array($extension, [...]) 文件类型分类detect_file_type文件类型检测

§PHP 行为对齐(R5 硬约束)

  • R5-9checkExt 使用 strtolower($file->extension()) + in_array($ext, explode(',', $rule)) (对齐 PHP Validate.php 第 957-964 行)
  • R5-10checkMime 使用 strtolower($file->getMime()) + in_array($mime, explode(',', $rule)) (对齐 PHP Validate.php 第 985-992 行)
  • R5-11checkSize 使用 $file->getSize() <= (int) $size (对齐 PHP Validate.php 第 973-976 行)
  • R5-12Driver::validatesence == 'image' 执行校验 (对齐 PHP Driver.php 第 26 行)
  • R5-13:默认图片规则 fileSize=20971520, fileExt='jpg,jpeg,png,gif,bmp', fileMime='image/jpeg,image/png,image/gif,image/bmp' (对齐 PHP Driver.php 第 30-32 行)
  • R5-14:默认图片消息 '最大可上传2M图片' / '只能上传jpg,jpeg,png,gif,bmp格式图片' (对齐 PHP Driver.php 第 34-38 行)
  • R5-15:文件类型分类 image 12 种 / video 13 种 / file 其他 (对齐 PHP Upload.php 第 62-69 行)

§PHP 源码参考

  • e:\vue\test\鲜视达\server\vendor\topthink\framework\src\think\Validate.php
    • 第 957-964 行:checkExt(File $file, $ext)
    • 第 973-976 行:checkSize(File $file, $size)
    • 第 985-992 行:checkMime(File $file, $mime)
    • 第 1002-1016 行:fileExt($file, $rule)
    • 第 1025-1039 行:fileMime($file, $rule)
    • 第 1048-1062 行:fileSize($file, $rule)
  • e:\vue\test\鲜视达\server\app\common\library\storage\Driver.php
    • 第 25-46 行:validate($name, $fileInfo, $sence = 'image')
  • e:\vue\test\鲜视达\server\app\api\controller\file\Upload.php
    • 第 62-69 行:文件类型分类逻辑

Structs§

FileValidateMessages
文件校验消息 — 对齐 PHP Driver::validate 第 34-38 行自定义消息
FileValidateRule
文件校验规则 — 对齐 PHP Driver::validatefileSize/fileExt/fileMime 规则
FileValidator
文件校验器 — 对齐 PHP app\common\library\storage\Driver::validate

Enums§

FileType
文件类型分类 — 对齐 PHP app\api\controller\file\Upload.php 第 62-69 行
FileValidateError
文件校验错误 — 对齐 PHP Driver::validate 校验失败

Functions§

detect_file_type
检测文件类型 — 对齐 PHP in_array($extension, [...]) 分类逻辑
parse_ext_list
解析扩展名列表 — 对齐 PHP explode(',', $ext) + strtolower
parse_mime_list
解析 MIME 列表 — 对齐 PHP explode(',', $mime) + strtolower