是否可以在未完全接收整个文件的情况下限制文件大小

问题描述 投票:0回答:1

我正在使用 rust actix 上传一些 zip 文件,现在我想检查上传的 zip 文件大小是否小于 100MB。这是代码:

async fn upload_full_proj(
    MultipartForm(form): MultipartForm<FullProjUpload>,
    login_user_info: LoginUserInfo,
) -> HttpResponse {
    return save_full_proj(form, &login_user_info).await;
}

这是

FullProjUpload
定义如下:

use actix_multipart::form::{MultipartForm, tempfile::TempFile, text::Text};

#[derive(Debug, MultipartForm)]
pub struct FullProjUpload {
    #[multipart(rename = "file")]
    pub files: Vec<TempFile>,
    pub project_id: Text<String>,
    pub parent: Text<String>
}

我尝试像这样限制项目条目的大小:

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    rust_i18n::set_locale("zh-CN");
    log4rs::init_file("log4rs.yaml", Default::default()).unwrap();
    let port: u16 = get_app_config("texhub.port").parse().unwrap();
    let address = ("0.0.0.0", port);
    consume_sys_events();
    HttpServer::new(|| {
        App::new().app_data(
            MultipartFormConfig::default()
                .total_limit(1048576) // 1 MB = 1024 * 1024
                .memory_limit(2097152) // 2 MB = 2 * 1024 * 1024
                .error_handler(handle_multipart_error),
        )
    })
    .workers(3)
    .bind(address)?
    .run()
    .await
}

并且工作正常,但是如何限制每个 actix api 中的文件大小?因为图片上传大小限制(<2MB) is different with the project zip file size limit(<100MB). I also need to check the size without full received the file.

rust actix-web
1个回答
0
投票

您可以在每个 .app_data

/
Scope 级别上提供
Resource
,而不仅仅是整体 
App

只需通过

.app_data
上的
Scope
/
Resource
提供不同的配置即可获得不同的配置。将使用最具体的配置。

© www.soinside.com 2019 - 2024. All rights reserved.