2024-07-04 16:14:22 +02:00
|
|
|
use std::{fs, io};
|
2022-02-18 03:13:08 +01:00
|
|
|
|
2023-02-26 22:56:55 +01:00
|
|
|
use actix_multipart::form::{tempfile::TempFile, text::Text, MultipartForm};
|
2022-08-01 04:19:39 +02:00
|
|
|
use actix_web::{
|
2024-07-07 01:23:03 +02:00
|
|
|
body::SizedStream, delete, error, get, http::Method, middleware::Logger, post, route, web, App,
|
|
|
|
Error, HttpResponse, HttpServer, Responder,
|
2022-08-01 04:19:39 +02:00
|
|
|
};
|
2024-07-07 01:23:03 +02:00
|
|
|
use actix_web_lab::extract::Path;
|
2023-11-20 19:54:13 +01:00
|
|
|
use aws_config::{meta::region::RegionProviderChain, BehaviorVersion};
|
2023-10-29 02:18:40 +02:00
|
|
|
use dotenvy::dotenv;
|
2024-07-04 16:14:22 +02:00
|
|
|
use futures_util::{stream, StreamExt as _};
|
2022-08-01 04:19:39 +02:00
|
|
|
use serde_json::json;
|
2024-07-04 16:14:22 +02:00
|
|
|
use tokio_util::io::ReaderStream;
|
2020-04-01 11:53:59 +02:00
|
|
|
|
2022-08-01 04:19:39 +02:00
|
|
|
mod client;
|
|
|
|
mod upload_file;
|
2022-08-01 02:17:59 +02:00
|
|
|
|
2023-02-26 22:56:55 +01:00
|
|
|
use self::{client::Client, upload_file::UploadedFile};
|
2020-02-06 02:06:41 +01:00
|
|
|
|
2023-02-26 22:56:55 +01:00
|
|
|
#[derive(Debug, MultipartForm)]
|
|
|
|
struct UploadForm {
|
|
|
|
namespace: Text<String>,
|
2020-02-06 02:06:41 +01:00
|
|
|
|
2023-02-26 22:56:55 +01:00
|
|
|
#[multipart(rename = "file")]
|
|
|
|
files: Vec<TempFile>,
|
2022-08-01 04:19:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/")]
|
|
|
|
async fn upload_to_s3(
|
2022-08-01 02:17:59 +02:00
|
|
|
s3_client: web::Data<Client>,
|
2023-02-26 22:56:55 +01:00
|
|
|
MultipartForm(form): MultipartForm<UploadForm>,
|
2022-08-01 04:19:39 +02:00
|
|
|
) -> Result<impl Responder, Error> {
|
2023-02-26 22:56:55 +01:00
|
|
|
let namespace = form.namespace.into_inner();
|
|
|
|
let files = form.files;
|
2022-08-01 02:17:59 +02:00
|
|
|
|
2023-02-26 22:56:55 +01:00
|
|
|
log::info!("namespace = {namespace:?}");
|
2022-08-01 04:19:39 +02:00
|
|
|
log::info!("tmp_files = {files:?}");
|
2022-08-01 02:17:59 +02:00
|
|
|
|
2022-08-01 04:19:39 +02:00
|
|
|
// make key prefix (make sure it ends with a forward slash)
|
2023-02-26 22:56:55 +01:00
|
|
|
let s3_key_prefix = format!("uploads/{namespace}/");
|
2022-08-01 02:17:59 +02:00
|
|
|
|
2023-02-26 22:56:55 +01:00
|
|
|
// upload temp files to s3 and then remove them
|
2022-08-01 04:19:39 +02:00
|
|
|
let uploaded_files = s3_client.upload_files(files, &s3_key_prefix).await?;
|
2022-08-01 02:17:59 +02:00
|
|
|
|
2022-08-01 04:19:39 +02:00
|
|
|
Ok(HttpResponse::Ok().json(json!({
|
|
|
|
"uploadedFiles": uploaded_files,
|
2023-02-26 22:56:55 +01:00
|
|
|
"meta": json!({ "namespace": namespace }),
|
2022-08-01 04:19:39 +02:00
|
|
|
})))
|
2020-02-05 04:53:33 +01:00
|
|
|
}
|
|
|
|
|
2024-07-04 16:14:22 +02:00
|
|
|
#[route("/file/{s3_key}*", method = "GET", method = "HEAD")]
|
2022-08-01 04:53:29 +02:00
|
|
|
async fn fetch_from_s3(
|
|
|
|
s3_client: web::Data<Client>,
|
2024-07-04 16:14:22 +02:00
|
|
|
method: Method,
|
2022-08-01 04:53:29 +02:00
|
|
|
Path((s3_key,)): Path<(String,)>,
|
|
|
|
) -> Result<impl Responder, Error> {
|
|
|
|
let (file_size, file_stream) = s3_client
|
|
|
|
.fetch_file(&s3_key)
|
|
|
|
.await
|
|
|
|
.ok_or_else(|| error::ErrorNotFound("file with specified key not found"))?;
|
|
|
|
|
2024-07-04 16:14:22 +02:00
|
|
|
let stream = match method {
|
|
|
|
// data stream for GET requests
|
|
|
|
Method::GET => ReaderStream::new(file_stream.into_async_read()).boxed_local(),
|
|
|
|
|
|
|
|
// empty stream for HEAD requests
|
|
|
|
Method::HEAD => stream::empty::<Result<_, io::Error>>().boxed_local(),
|
|
|
|
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(HttpResponse::Ok()
|
|
|
|
.no_chunking(file_size)
|
|
|
|
.body(SizedStream::new(file_size, stream)))
|
2022-08-01 04:53:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[delete("/file/{s3_key}*")]
|
|
|
|
async fn delete_from_s3(
|
|
|
|
s3_client: web::Data<Client>,
|
|
|
|
Path((s3_key,)): Path<(String,)>,
|
|
|
|
) -> Result<impl Responder, Error> {
|
|
|
|
if s3_client.delete_file(&s3_key).await {
|
|
|
|
Ok(HttpResponse::NoContent().finish())
|
|
|
|
} else {
|
|
|
|
Err(error::ErrorNotFound("file with specified key not found"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-01 04:19:39 +02:00
|
|
|
#[get("/")]
|
2022-08-01 02:17:59 +02:00
|
|
|
async fn index() -> impl Responder {
|
2024-07-07 01:23:03 +02:00
|
|
|
web::Html::new(include_str!("./index.html").to_owned())
|
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-08-01 02:17:59 +02:00
|
|
|
log::info!("creating temporary upload directory");
|
|
|
|
|
|
|
|
fs::create_dir_all("./tmp").unwrap();
|
2020-02-06 02:06:41 +01:00
|
|
|
|
2022-08-01 02:17:59 +02:00
|
|
|
log::info!("configuring S3 client");
|
|
|
|
let aws_region = RegionProviderChain::default_provider().or_else("us-east-1");
|
2023-11-20 19:54:13 +01:00
|
|
|
let aws_config = aws_config::defaults(BehaviorVersion::latest())
|
|
|
|
.region(aws_region)
|
|
|
|
.load()
|
|
|
|
.await;
|
2022-08-01 04:19:39 +02:00
|
|
|
|
|
|
|
// create singleton S3 client
|
2022-08-01 02:17:59 +02:00
|
|
|
let s3_client = Client::new(&aws_config);
|
2020-02-06 02:06:41 +01:00
|
|
|
|
2022-08-01 02:17:59 +02:00
|
|
|
log::info!("using AWS region: {}", aws_config.region().unwrap());
|
2020-02-05 04:53:33 +01:00
|
|
|
|
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
|
|
|
|
2022-08-01 02:17:59 +02:00
|
|
|
HttpServer::new(move || {
|
2022-02-18 03:13:08 +01:00
|
|
|
App::new()
|
2024-07-04 16:14:22 +02:00
|
|
|
.app_data(web::Data::new(s3_client.clone()))
|
2022-08-01 04:19:39 +02:00
|
|
|
.service(index)
|
|
|
|
.service(upload_to_s3)
|
2022-08-01 04:53:29 +02:00
|
|
|
.service(fetch_from_s3)
|
|
|
|
.service(delete_from_s3)
|
2022-02-18 03:13:08 +01:00
|
|
|
.wrap(Logger::default())
|
2020-02-05 04:53:33 +01:00
|
|
|
})
|
2022-08-01 02:17:59 +02:00
|
|
|
.workers(2)
|
2022-02-18 03:13:08 +01:00
|
|
|
.bind(("127.0.0.1", 8080))?
|
2020-02-05 04:53:33 +01:00
|
|
|
.run()
|
|
|
|
.await
|
|
|
|
}
|