actix-web:限制上传文件大小

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

我成功上传了参考此示例的文件,但我不知道如何限制文件的大小,例如我无法保存超过

5M

的文件
[dependencies]
actix-web = "4"
actix-multipart = "0.4" 

我尝试过这个,但没有成功。

        web::resource("/upload_file")
            .app_data(web::PayloadConfig::new(1024 * 5))
            .route(web::post().to(views::save_file)),
rust actix-web
3个回答
2
投票

中间件

默认在所有请求中使用,可以使用

req.path()

在指定路由中使用
use actix_web::Error;
use std::future::{ready, Ready};

use actix_web::dev::{forward_ready, Service, ServiceRequest, ServiceResponse, Transform};
use futures_util::future::LocalBoxFuture;


pub struct ContentLengthLimit {
    pub limit: u64, // byte
}
impl<S, B> Transform<S, ServiceRequest> for ContentLengthLimit
where
    S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
    S::Future: 'static,
    B: 'static,
{
    type Response = ServiceResponse<B>;
    type Error = Error;
    type InitError = ();
    type Transform = ContentLengthLimitMiddleware<S>;
    type Future = Ready<Result<Self::Transform, Self::InitError>>;

    fn new_transform(&self, service: S) -> Self::Future {
        ready(Ok(ContentLengthLimitMiddleware {
            service,
            limit: self.limit,
        }))
    }
}

impl Default for ContentLengthLimit {
    fn default() -> Self {
        Self {
            limit: 1024 * 1024 * 5, /* 5M */
        }
    }
}

pub struct ContentLengthLimitMiddleware<S> {
    service: S,
    limit: u64,
}

impl<S, B> ContentLengthLimitMiddleware<S>
where
    S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
    S::Future: 'static,
    B: 'static,
{
    fn is_big(&self, req: &ServiceRequest) -> Result<bool, ()> {
        Ok(req
            .headers()
            .get("content-length")
            .ok_or(())?
            .to_str()
            .map_err(|_| ())?
            .parse::<u64>()
            .map_err(|_| ())?
            > self.limit)
    }
}

impl<S, B> Service<ServiceRequest> for ContentLengthLimitMiddleware<S>
where
    S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error>,
    S::Future: 'static,
    B: 'static,
{
    type Response = ServiceResponse<B>;
    type Error = Error;
    type Future = LocalBoxFuture<'static, Result<Self::Response, Self::Error>>;

    forward_ready!(service);

    fn call(&self, req: ServiceRequest) -> Self::Future {
        if let Ok(r) = self.is_big(&req) {
            if r {
                return Box::pin(async { Err(hje("error").actix()) });
            }
        }

        let fut = self.service.call(req);

        Box::pin(async move {
            let res = fut.await?;
            Ok(res)
        })
    }
}

使用

App::new().wrap(ContentLengthLimit::default())

0
投票

这个发布的答案是指 Actix 中的分段上传示例。目前,由于:

https://github.com/actix/actix-web/issues/2695

有必要在返回错误之前耗尽有效负载,否则连接可能会挂起或线程可能会出现恐慌。这是一个在返回 PayloadTooLarge 错误之前首先耗尽有效负载的示例。一旦上述问题得到解决,这可能就没有必要了:



lazy_static! {
    pub static ref MAX_ALLOWED_CONTENT_LENGTH: u64 =
        settings::get_setting("service_max_content_length")
            .unwrap_or("20971520".to_owned())
            .parse::<u64>()
            .unwrap_or(20971520);
}

#[derive(Debug)]
pub struct ContentLengthChecker;

impl<S, B> Transform<S, ServiceRequest> for ContentLengthChecker
where
    S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
    S::Future: 'static,
    B: 'static,
{
    type Response = ServiceResponse<EitherBody<B>>;
    type Error = Error;
    type Transform = ContentLengthCheckerMiddleware<S>;
    type InitError = ();
    type Future = Ready<Result<Self::Transform, Self::InitError>>;

    fn new_transform(&self, service: S) -> Self::Future {
        ok(ContentLengthCheckerMiddleware {
            service: Rc::new(service),
        })
    }
}

#[derive(Debug)]
pub struct ContentLengthCheckerMiddleware<S> {
    service: Rc<S>,
}

impl<S, B> Service<ServiceRequest> for ContentLengthCheckerMiddleware<S>
where
    S: Service<ServiceRequest, Response = ServiceResponse<B>, Error = Error> + 'static,
    S::Future: 'static,
    B: 'static,
{
    type Response = ServiceResponse<EitherBody<B>>;
    type Error = Error;
    type Future = Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>>>>;

    forward_ready!(service);

    fn call(&self, mut req: ServiceRequest) -> Self::Future {
        let service = Rc::clone(&self.service);

        let content_length = match req.headers().get("content-length") {
            None => 0,
            Some(content_length) => MiddlewareUtility::get_content_length(Some(content_length)),
        };

        if content_length <= *MAX_ALLOWED_CONTENT_LENGTH {
            return Box::pin(async move {
                service
                    .call(req)
                    .await
                    .map(ServiceResponse::map_into_left_body)
            });
        }

        Box::pin(async move {
            /* need to drain the body due to
             https://github.com/actix/actix-web/issues/2695
             */
            let mut payload = req.take_payload();
            while let Ok(Some(_)) = payload.try_next().await {}

            Ok(req.into_response(
                HttpResponse::new(StatusCode::PAYLOAD_TOO_LARGE).map_into_right_body(),
            ))
        })
    }
}



0
投票

2024 年更新:不再需要以前的方法。相反,将其添加到您的 App() 中:

    App::new()
        .wrap(Logger::default())
        .app_data(
            MultipartFormConfig::default()
                .total_limit(10 *1024 * 1024 * 1024) // 10 GB
                .memory_limit(10 * 1024 * 1024) // 10 MB
                .error_handler(handle_multipart_error),
        )

错误处理程序:

fn handle_multipart_error(err: MultipartError, req: &HttpRequest) -> Error {
    logging::log!("Multipart error: {}", err);
    return Error::from(err);
}

进口:

use actix_web::Error;
use actix_web::HttpRequest;
use actix_multipart::MultipartError;

文档:

https://docs.rs/actix-multipart/latest/actix_multipart/form/struct.MultipartFormConfig.html

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