1
0
mirror of https://github.com/actix/examples synced 2024-12-03 18:22:14 +01:00
examples/forms/multipart-s3/src/main.rs

105 lines
3.5 KiB
Rust
Raw Normal View History

2022-02-18 03:13:08 +01:00
use std::{borrow::BorrowMut, env};
2020-02-05 04:53:33 +01:00
use actix_multipart::Multipart;
2022-02-18 03:13:08 +01:00
use actix_web::{middleware::Logger, web, App, Error, HttpResponse, HttpServer};
2020-02-06 02:06:41 +01:00
use dotenv::dotenv;
2020-04-01 11:53:59 +02:00
use serde::{Deserialize, Serialize};
mod utils;
2022-02-18 03:13:08 +01:00
use self::utils::upload::{save_file as upload_save_file, split_payload, UploadFile};
2020-02-06 02:06:41 +01:00
2020-04-01 11:53:59 +02:00
#[derive(Deserialize, Serialize, Debug)]
pub struct InpAdd {
pub text: String,
pub number: i32,
2020-02-05 04:53:33 +01:00
}
2020-02-06 02:06:41 +01:00
2020-02-05 04:53:33 +01:00
async fn save_file(mut payload: Multipart) -> Result<HttpResponse, Error> {
let pl = split_payload(payload.borrow_mut()).await;
println!("bytes={:#?}", pl.0);
2020-04-01 11:53:59 +02:00
let inp_info: InpAdd = serde_json::from_slice(&pl.0).unwrap();
2020-02-05 04:53:33 +01:00
println!("converter_struct={:#?}", inp_info);
println!("tmpfiles={:#?}", pl.1);
//make key
let s3_upload_key = format!("projects/{}/", "posts_id");
//create tmp file and upload s3 and remove tmp file
2022-02-18 03:44:02 +01:00
let upload_files: Vec<UploadFile> = upload_save_file(pl.1, s3_upload_key).await.unwrap();
2020-02-05 04:53:33 +01:00
println!("upload_files={:#?}", upload_files);
Ok(HttpResponse::Ok().into())
}
2022-02-02 01:44:58 +01:00
async fn index() -> HttpResponse {
2020-02-05 04:53:33 +01:00
let html = r#"<html>
<head><title>Upload Test</title></head>
<body>
<form target="/" method="post" enctype="multipart/form-data" id="myForm" >
2022-06-08 04:53:28 +02:00
<input type="text" id="text" name="text" value="test_text"/>
<input type="number" id="number" name="number" value="123123"/>
2020-02-05 04:53:33 +01:00
<input type="button" value="Submit" onclick="myFunction()"></button>
</form>
<input type="file" multiple name="file" id="myFile"/>
</body>
<script>
function myFunction(){
var myForm = document.getElementById('myForm');
var myFile = document.getElementById('myFile');
2022-06-08 04:53:28 +02:00
2020-02-05 04:53:33 +01:00
let formData = new FormData();
const obj = {
text: document.getElementById('text').value,
number: Number(document.getElementById('number').value)
};
const json = JSON.stringify(obj);
console.log(obj);
console.log(json);
2022-06-08 04:53:28 +02:00
2020-02-05 04:53:33 +01:00
formData.append("data", json);
formData.append("myFile", myFile.files[0]);
2022-06-08 04:53:28 +02:00
2020-02-05 04:53:33 +01:00
var request = new XMLHttpRequest();
request.open("POST", "");
request.send(formData);
}
2022-06-08 04:53:28 +02:00
2020-02-05 04:53:33 +01:00
</script>
</html>"#;
HttpResponse::Ok()
.content_type("text/html; charset=utf-8")
.body(html)
2020-02-05 04:53:33 +01:00
}
2020-09-12 17:49:45 +02:00
#[actix_web::main]
2020-02-05 04:53:33 +01:00
async fn main() -> std::io::Result<()> {
2020-02-06 02:06:41 +01:00
dotenv().ok();
2022-02-18 03:13:08 +01:00
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
2022-02-18 03:44:02 +01:00
let aws_access_key_id = env::var("AWS_ACCESS_KEY_ID").expect("AWS_ACCESS_KEY_ID must be set");
2020-04-01 11:53:59 +02:00
let aws_secret_access_key =
2020-02-06 02:06:41 +01:00
env::var("AWS_SECRET_ACCESS_KEY").expect("AWS_SECRET_ACCESS_KEY must be set");
2020-04-01 11:53:59 +02:00
let aws_s3_bucket_name =
2020-02-06 02:06:41 +01:00
env::var("AWS_S3_BUCKET_NAME").expect("AWS_S3_BUCKET_NAME must be set");
log::info!("aws_access_key_id: {aws_access_key_id}");
log::info!("aws_secret_access_key: {aws_secret_access_key}");
log::info!("aws_s3_bucket_name: {aws_s3_bucket_name}");
2020-02-06 02:06:41 +01:00
2020-02-05 04:53:33 +01:00
std::fs::create_dir_all("./tmp").unwrap();
2022-02-18 03:13:08 +01:00
log::info!("starting HTTP server at http://localhost:8080");
2020-02-05 04:53:33 +01:00
HttpServer::new(|| {
2022-02-18 03:13:08 +01:00
App::new()
.service(
web::resource("/")
.route(web::get().to(index))
.route(web::post().to(save_file)),
)
.wrap(Logger::default())
2020-02-05 04:53:33 +01:00
})
2022-02-18 03:13:08 +01:00
.bind(("127.0.0.1", 8080))?
2020-02-05 04:53:33 +01:00
.run()
.await
}