pub struct FileValidator { /* private fields */ }Expand description
文件校验器 — 对齐 PHP app\common\library\storage\Driver::validate
PHP 行为(第 25-46 行):
public function validate($name, $fileInfo, $sence = 'image'){
if($sence == 'image'){
try{
validate([$name=>[...]], [$name.'.fileSize' => '...', ...])
->check([$name => $fileInfo]);
return true;
}catch(\Exception $e){
$this->engine->error = $e->getMessage();
return false;
}
}
return false;
}§Rust 端语义
- PHP
sence == 'image'分支 →FileValidator::validate_image - PHP
sence != 'image'直接return false→ Rust 端不提供此分支 (业务层应直接拒绝非 image 场景,或使用其他校验器)
Implementations§
Source§impl FileValidator
impl FileValidator
Sourcepub fn new() -> FileValidator
pub fn new() -> FileValidator
创建校验器(使用默认图片规则和消息,对齐 PHP Driver::validate 默认行为)
Sourcepub fn with(
rule: FileValidateRule,
messages: FileValidateMessages,
) -> FileValidator
pub fn with( rule: FileValidateRule, messages: FileValidateMessages, ) -> FileValidator
创建校验器(自定义规则和消息)
Sourcepub fn rule(&self) -> &FileValidateRule
pub fn rule(&self) -> &FileValidateRule
获取校验规则引用
Sourcepub fn messages(&self) -> &FileValidateMessages
pub fn messages(&self) -> &FileValidateMessages
获取校验消息引用
Sourcepub fn check_ext(file: &UploadedFile, allowed: &[String]) -> bool
pub fn check_ext(file: &UploadedFile, allowed: &[String]) -> bool
校验扩展名 — 对齐 PHP Validate::checkExt 第 957-964 行
PHP 行为:
protected function checkExt(File $file, $ext): bool {
if (is_string($ext)) { $ext = explode(',', $ext); }
return in_array(strtolower($file->extension()), $ext);
}注意:$file->extension() 对 UploadedFile 会调用覆写版本(返回 original_extension)。
Sourcepub fn check_mime(
file: &File,
allowed: &[String],
) -> Result<bool, FileValidateError>
pub fn check_mime( file: &File, allowed: &[String], ) -> Result<bool, FileValidateError>
校验 MIME — 对齐 PHP Validate::checkMime 第 985-992 行
PHP 行为:
protected function checkMime(File $file, $mime): bool {
if (is_string($mime)) { $mime = explode(',', $mime); }
return in_array(strtolower($file->getMime()), $mime);
}注意:$file->getMime() 使用服务器端 finfo_file 检测(Rust 端使用 infer crate)。
Sourcepub fn check_size(file: &File, max: u64) -> Result<bool, FileValidateError>
pub fn check_size(file: &File, max: u64) -> Result<bool, FileValidateError>
校验大小 — 对齐 PHP Validate::checkSize 第 973-976 行
PHP 行为:
protected function checkSize(File $file, $size): bool {
return $file->getSize() <= (int) $size;
}Sourcepub fn validate_image(
&self,
file: &UploadedFile,
) -> Result<(), FileValidateError>
pub fn validate_image( &self, file: &UploadedFile, ) -> Result<(), FileValidateError>
校验文件 — 对齐 PHP Driver::validate($name, $fileInfo, 'image')
执行顺序:扩展名 → MIME → 大小(对齐 PHP validate()->check() 的规则遍历顺序)
§返回值
Ok(()):校验通过Err(FileValidateError::*):校验失败,包含错误消息(对齐 PHP$e->getMessage())
Trait Implementations§
Source§impl Clone for FileValidator
impl Clone for FileValidator
Source§fn clone(&self) -> FileValidator
fn clone(&self) -> FileValidator
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreSource§impl Debug for FileValidator
impl Debug for FileValidator
Source§impl Default for FileValidator
impl Default for FileValidator
Source§fn default() -> FileValidator
fn default() -> FileValidator
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for FileValidator
impl RefUnwindSafe for FileValidator
impl Send for FileValidator
impl Sync for FileValidator
impl Unpin for FileValidator
impl UnsafeUnpin for FileValidator
impl UnwindSafe for FileValidator
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
The inverse inclusion map: attempts to construct
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
Checks if
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
Use with care! Same as
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
The inclusion map: converts
self to the equivalent element of its superset.