pub struct File { /* private fields */ }Expand description
文件类 — 对齐 PHP think\File(基于 SplFileInfo)
PHP 第 22-46 行:
class File extends SplFileInfo {
protected $hash = [];
protected $hashName;
protected $extension;
public function __construct(string $path, bool $checkPath = true) {
if ($checkPath && !is_file($path)) {
throw new FileException(sprintf('The file "%s" does not exist', $path));
}
parent::__construct($path);
}
}Implementations§
Source§impl File
impl File
Sourcepub fn new<P>(path: P, check_path: bool) -> Result<File, UploadError>
pub fn new<P>(path: P, check_path: bool) -> Result<File, UploadError>
创建 File 实例 — 对齐 PHP File::__construct 第 39-46 行
check_path = true 时检查文件是否存在,不存在返回 UploadError::FileNotFound。
Sourcepub fn new_unchecked<P>(path: P) -> File
pub fn new_unchecked<P>(path: P) -> File
不检查路径创建实例 — 对齐 PHP 第 138 行 new self($target, false)
Sourcepub fn hash(&mut self, algo: HashAlgo) -> Result<String, UploadError>
pub fn hash(&mut self, algo: HashAlgo) -> Result<String, UploadError>
获取文件哈希 — 对齐 PHP File::hash($type) 第 54-61 行
PHP 行为:
public function hash(string $type = 'sha1'): string {
if (!isset($this->hash[$type])) {
$this->hash[$type] = hash_file($type, $this->getPathname());
}
return $this->hash[$type];
}Sourcepub fn md5(&mut self) -> Result<String, UploadError>
pub fn md5(&mut self) -> Result<String, UploadError>
获取文件 MD5 — 对齐 PHP File::md5() 第 68-71 行
Sourcepub fn sha1(&mut self) -> Result<String, UploadError>
pub fn sha1(&mut self) -> Result<String, UploadError>
获取文件 SHA1 — 对齐 PHP File::sha1() 第 78-81 行
Sourcepub fn get_mime(&self) -> Result<String, UploadError>
pub fn get_mime(&self) -> Result<String, UploadError>
获取文件 MIME — 对齐 PHP File::getMime() 第 88-93 行
PHP 行为:使用 finfo_open(FILEINFO_MIME_TYPE) + finfo_file() 检测 MIME。
Rust 端:优先用 infer crate 从内容检测,回退到 mime_guess 从扩展名猜测。
Sourcepub fn move_to<P>(
&mut self,
directory: P,
name: Option<&str>,
) -> Result<File, UploadError>
pub fn move_to<P>( &mut self, directory: P, name: Option<&str>, ) -> Result<File, UploadError>
移动文件 — 对齐 PHP File::move() 第 102-118 行
PHP 行为:
- 获取 target 文件
rename($this->getPathname(), $target)移动文件- 失败抛
FileException chmod($target, 0666 & ~umask())设置权限- 返回新
File实例
Sourcepub fn extension(&self) -> String
pub fn extension(&self) -> String
文件扩展名 — 对齐 PHP File::extension() 第 159-162 行
PHP 行为:return $this->getExtension();(SplFileInfo::getExtension() 返回最后点后的部分)
Sourcepub fn set_extension(&mut self, extension: &str)
pub fn set_extension(&mut self, extension: &str)
指定保存文件的扩展名 — 对齐 PHP File::setExtension() 第 169-172 行
Sourcepub fn hash_name(&mut self, rule: HashNameRule) -> Result<String, UploadError>
pub fn hash_name(&mut self, rule: HashNameRule) -> Result<String, UploadError>
自动生成文件名 — 对齐 PHP File::hashName($rule) 第 180-203 行
PHP 行为:
public function hashName($rule = ''): string {
if (!$this->hashName) {
if ($rule instanceof \Closure) {
$this->hashName = call_user_func_array($rule, [$this]);
} else {
switch (true) {
case in_array($rule, hash_algos()):
$hash = $this->hash($rule);
$this->hashName = substr($hash, 0, 2) . DIRECTORY_SEPARATOR . substr($hash, 2);
break;
case is_callable($rule):
$this->hashName = call_user_func($rule);
break;
default:
$this->hashName = date('Ymd') . DIRECTORY_SEPARATOR . md5(microtime(true) . $this->getPathname());
break;
}
}
}
$extension = $this->extension ?? $this->extension();
return $this->hashName . ($extension ? '.' . $extension : '');
}Rust 端简化:只支持 Default 和 Hash(algo) 两种规则。
Trait Implementations§
Auto Trait Implementations§
impl Freeze for File
impl RefUnwindSafe for File
impl Send for File
impl Sync for File
impl Unpin for File
impl UnsafeUnpin for File
impl UnwindSafe for File
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
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> ⓘ
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> ⓘ
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>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
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
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.