Skip to main content

StorageEngine

Trait StorageEngine 

Source
pub trait StorageEngine: Send + Sync {
    // Required methods
    fn upload<'life0, 'async_trait>(
        &'life0 mut self,
    ) -> Pin<Box<dyn Future<Output = Result<Option<String>, UploadError>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             Self: 'async_trait;
    fn delete<'life0, 'life1, 'async_trait>(
        &'life0 mut self,
        file_name: &'life1 str,
    ) -> Pin<Box<dyn Future<Output = Result<bool, UploadError>> + Send + 'async_trait>>
       where 'life0: 'async_trait,
             'life1: 'async_trait,
             Self: 'async_trait;
    fn file_name(&self) -> Option<&str>;
    fn file_info(&self) -> Option<&UploadFileInfo>;
    fn error(&self) -> Option<&str>;
    fn set_upload_file(
        &mut self,
        file: &UploadedFile,
    ) -> Result<(), UploadError>;
    fn set_upload_file_by_real(
        &mut self,
        file_path: &Path,
        extension: &str,
    ) -> Result<(), UploadError>;
    fn is_internal(&self) -> bool;

    // Provided method
    fn real_path(&self) -> Option<&Path> { ... }
}
Expand description

存储引擎抽象 — 对齐 PHP storage\engine\Server(abstract)

PHP 抽象方法:upload() / delete($fileName) / getFileName() PHP 具体方法:setUploadFile / setUploadFileByReal / getFileInfo / getRealPath / getError / buildSaveName

Rust 端使用 async_trait 因为 upload/delete 涉及 IO 操作。

Required Methods§

Source

fn upload<'life0, 'async_trait>( &'life0 mut self, ) -> Pin<Box<dyn Future<Output = Result<Option<String>, UploadError>> + Send + 'async_trait>>
where 'life0: 'async_trait, Self: 'async_trait,

执行文件上传 — 对齐 PHP Server::upload()(abstract)

返回值:

  • Ok(Some(save_name)):Local 外部上传返回保存路径
  • Ok(None):Local 内部上传或云存储上传成功(PHP 返回 true
  • Err(_):上传失败
Source

fn delete<'life0, 'life1, 'async_trait>( &'life0 mut self, file_name: &'life1 str, ) -> Pin<Box<dyn Future<Output = Result<bool, UploadError>> + Send + 'async_trait>>
where 'life0: 'async_trait, 'life1: 'async_trait, Self: 'async_trait,

执行文件删除 — 对齐 PHP Server::delete($fileName)(abstract)

返回值:

  • Ok(true):删除成功或文件不存在(对齐 PHP Elvis 短路)
  • Ok(false):删除失败(PHP 设置 error 返回 false
  • Err(_):系统错误
Source

fn file_name(&self) -> Option<&str>

获取文件名 — 对齐 PHP Server::getFileName()

Source

fn file_info(&self) -> Option<&UploadFileInfo>

获取文件信息 — 对齐 PHP Server::getFileInfo()

Source

fn error(&self) -> Option<&str>

获取错误信息 — 对齐 PHP Server::getError()

Source

fn set_upload_file(&mut self, file: &UploadedFile) -> Result<(), UploadError>

设置上传文件 — 对齐 PHP Server::setUploadFile($name)

PHP 行为:从 Request::file($name) 获取文件,调用 buildSaveName() 生成文件名。 Rust 端:从 UploadedFile 设置,调用 build_save_name() 生成文件名。

Source

fn set_upload_file_by_real( &mut self, file_path: &Path, extension: &str, ) -> Result<(), UploadError>

设置内部上传文件 — 对齐 PHP Server::setUploadFileByReal($filePath, $extension)

PHP 行为:设置 isInternal = true,构造 fileInfo,文件名为 storage/{Ymd}/{basename}

Source

fn is_internal(&self) -> bool

是否内部上传

Provided Methods§

Source

fn real_path(&self) -> Option<&Path>

获取真实路径 — 对齐 PHP Server::getRealPath() 第 84-91 行

PHP 行为:

  • isInternal == true → 返回 $this->fileInfo['tmp_name']
  • isInternal == false → 返回 request()->file('iFile')->getRealPath()

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§