Advertisement
kyoo0000

file_uploader

Sep 3rd, 2021
2,088
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.67 KB | None | 0 0
  1. use std::io::Write;
  2.  
  3. use actix_web::{App, HttpResponse, HttpServer, middleware, web};
  4. use actix_multipart::Multipart;
  5. use actix_web::Error;
  6. use clap::Arg;
  7. use futures_lite::StreamExt;
  8.  
  9.  
  10. async fn save_file(mut payload: Multipart) -> Result<HttpResponse, Error> {
  11.     while let Ok(Some(mut field)) = payload.try_next().await {
  12.         let content_type = field.content_disposition().unwrap();
  13.         let filename = content_type.get_filename().unwrap();
  14.         let filepath = format!("uploaded-images/{}", sanitize_filename::sanitize(&filename));
  15.  
  16.         let mut f = web::block(|| std::fs::File::create(filepath))
  17.             .await
  18.             .unwrap();
  19.  
  20.         while let Some(chunk) = field.next().await {
  21.             let data = chunk.unwrap();
  22.             f = web::block(move || f.write_all(&data).map(|_| f)).await?;
  23.         }
  24.     }
  25.     Ok(HttpResponse::Ok().into())
  26. }
  27.  
  28. fn index() -> HttpResponse {
  29.     let html = r#"<html>
  30.        <head><title>Upload Test</title></head>
  31.        <body>
  32.            <form target="/" method="post" enctype="multipart/form-data">
  33.                <input type="file" multiple name="file"/>
  34.                <button type="submit">Submit</button>
  35.            </form>
  36.        </body>
  37.    </html>"#;
  38.  
  39.     HttpResponse::Ok()
  40.         .content_type("text/html; charset=utf-8")
  41.         .body(html)
  42. }
  43.  
  44. #[actix_web::main]
  45. async fn main() -> Result<(), Error> {
  46.     std::env::set_var("RUST_LOG", "actix_server=info,actix_web=info");
  47.     std::fs::create_dir_all("./uploaded-images").unwrap();
  48.    
  49.     // Configurable host and port by command line args
  50.     let command_line = clap::App::new("File uploader")
  51.         .arg(Arg::with_name("apphost")
  52.             .short("h")
  53.             .long("host")
  54.             .value_name("APPHOST")
  55.             .help("if empty, default is localhost")
  56.             .takes_value(true))
  57.         .arg(Arg::with_name("appport")
  58.             .short("p")
  59.             .long("port")
  60.             .value_name("APPPORT")
  61.             .help("app port, if empty default is 9000")
  62.             .takes_value(true))
  63.         .get_matches();
  64.     let host = command_line.value_of("host").unwrap_or("127.0.0.1");
  65.     let port = command_line.value_of("port").unwrap_or("9000");
  66.    
  67.     println!("File uploader server running on {}:{}", host, port);
  68.  
  69.     HttpServer::new(|| {
  70.         App::new().wrap(middleware::Logger::default())
  71.             .service(
  72.                 actix_web::web::resource("/")
  73.                 .route(actix_web::web::get().to(index))
  74.                 .route(actix_web::web::post().to(save_file)),
  75.             )
  76.     })
  77.     .bind(format!("{}:{}", host, port))?
  78.     .run()
  79.     .await;
  80.  
  81.     Ok(())
  82. }
  83.  
  84.  
  85.  
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement