rustactix-web

is it possible to limit the file size without fullly received the entire file


I am using rust actix to upload some zip file, now I want to check the upload zip file size less than 100MB. This is the code look like:

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

and this is the FullProjUpload defined like:

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>
}

I have tried to limit the size in the project entry like this:

#[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
}

and it works fine, but how to limit the file size in each actix api? because the image upload size limit(<2MB) is different with the project zip file size limit(<100MB). I also need to check the size without full received the file.


Solution

  • You can provide .app_data on a per Scope/Resource level and not just on the whole App.

    Just provide a different config via .app_data on the Scope/Resource you want to have a different configuration. The most specific configuration will be used.