monorepo/file-service/src/main.rs

196 lines
5.4 KiB
Rust
Raw Normal View History

#[macro_use]
extern crate log;
2023-10-03 15:10:37 +00:00
use handlers::{file, handle_auth, handle_upload, thumbnail};
use http::status::StatusCode;
// use mustache::{compile_path, Template};
// use orizentic::{Permissions, ResourceName, Secret};
use bytes::Buf;
2023-09-25 03:52:29 +00:00
use futures_util::StreamExt;
use std::{
2023-09-27 02:43:33 +00:00
collections::HashMap,
2023-10-03 15:10:37 +00:00
convert::Infallible,
2023-09-21 03:31:52 +00:00
io::Read,
net::{IpAddr, Ipv4Addr, SocketAddr},
path::PathBuf,
sync::{Arc, RwLock},
};
2023-10-03 15:10:37 +00:00
use store::Username;
use warp::{filters::multipart::Part, Filter, Rejection};
2023-10-03 15:10:37 +00:00
mod handlers;
mod html;
mod pages;
mod store;
2023-10-03 15:10:37 +00:00
pub use handlers::handle_index;
pub use store::{
App, AuthDB, AuthToken, FileHandle, FileId, FileInfo, ReadFileError, SessionToken, Store,
};
/*
2023-10-03 15:10:37 +00:00
async fn authenticate_user(app: App, auth_token: String) -> Result<Username, warp::Rejection> {
match app.auth_session(SessionToken::from(auth_token)).await {
Ok(username) => Ok(username),
Err(_) => Err(warp::reject::not_found()),
}
}
*/
2023-10-03 15:10:37 +00:00
/*
*/
2023-10-03 15:10:37 +00:00
/*
2023-09-27 02:43:33 +00:00
async fn collect_content(
mut part: Part,
) -> Result<(Option<String>, Option<String>, Vec<u8>), String> {
let mut content: Vec<u8> = Vec::new();
while let Some(Ok(data)) = part.data().await {
let mut reader = data.reader();
reader.read_to_end(&mut content).unwrap();
}
2023-09-27 02:43:33 +00:00
Ok((
part.content_type().map(|s| s.to_owned()),
part.filename().map(|s| s.to_owned()),
content,
))
}
2023-10-03 15:10:37 +00:00
*/
2023-10-03 15:10:37 +00:00
/*
async fn collect_multipart(
mut stream: warp::filters::multipart::FormData,
2023-09-27 02:43:33 +00:00
) -> Result<Vec<(Option<String>, Option<String>, Vec<u8>)>, warp::Error> {
let mut content: Vec<(Option<String>, Option<String>, Vec<u8>)> = Vec::new();
while let Some(part) = stream.next().await {
match part {
Ok(part) => content.push(collect_content(part).await.unwrap()),
Err(err) => return Err(err),
}
}
Ok(content)
}
2023-10-03 15:10:37 +00:00
*/
2023-10-03 15:10:37 +00:00
/*
2023-09-27 02:43:33 +00:00
async fn handle_upload(
form: warp::filters::multipart::FormData,
2023-10-03 15:10:37 +00:00
app: App,
2023-09-27 02:43:33 +00:00
) -> warp::http::Result<warp::http::Response<String>> {
let files = collect_multipart(form).await;
match files {
Ok(files) => {
for (_, filename, content) in files {
match filename {
Some(filename) => {
2023-10-03 15:10:37 +00:00
app.add_file(filename, content).unwrap();
2023-09-27 02:43:33 +00:00
}
None => {
return warp::http::Response::builder()
.status(StatusCode::BAD_REQUEST)
.body("".to_owned())
}
}
}
}
Err(_err) => {
return warp::http::Response::builder()
.status(StatusCode::BAD_REQUEST)
.body("".to_owned())
}
}
// println!("file length: {:?}", files.map(|f| f.len()));
warp::http::Response::builder()
.header("location", "/")
.status(StatusCode::SEE_OTHER)
.body("".to_owned())
}
2023-10-03 15:10:37 +00:00
*/
2023-09-27 02:43:33 +00:00
2023-10-03 15:10:37 +00:00
fn with_app(app: App) -> impl Filter<Extract = (App,), Error = Infallible> + Clone {
warp::any().map(move || app.clone())
}
2023-10-03 15:10:37 +00:00
fn maybe_with_session() -> impl Filter<Extract = (Option<SessionToken>,), Error = Rejection> + Copy
{
warp::any()
.and(warp::header::optional::<String>("cookie"))
.map(|cookies| {
println!("cookies retrieved: {:?}", cookies);
None
})
}
2023-10-03 15:10:37 +00:00
fn with_session() -> impl Filter<Extract = (SessionToken,), Error = Rejection> + Copy {
warp::any()
.and(warp::header::<String>("cookie"))
.map(|token: String| SessionToken::from(token))
}
2023-10-03 15:10:37 +00:00
#[tokio::main]
pub async fn main() {
pretty_env_logger::init();
2023-10-03 15:10:37 +00:00
let authdb = AuthDB::new(PathBuf::from(":memory:")).await.unwrap();
let store = Store::new(PathBuf::from(&std::env::var("FILE_SHARE_DIR").unwrap()));
2023-10-03 15:10:37 +00:00
let app = App::new(authdb, store);
/*
2023-10-03 15:10:37 +00:00
let with_app = {
2023-09-27 02:43:33 +00:00
let app = app.clone();
warp::any().map(move || app.clone())
};
2023-10-03 15:10:37 +00:00
*/
2023-09-27 02:43:33 +00:00
let log = warp::log("file_service");
2023-10-03 15:10:37 +00:00
let root = warp::path!()
.and(warp::get())
.and(with_app(app.clone()))
.and(maybe_with_session())
.then(handle_index);
2023-10-03 15:10:37 +00:00
let auth = warp::path!("auth")
2023-09-27 02:43:33 +00:00
.and(warp::post())
.and(with_app(app.clone()))
2023-10-03 15:10:37 +00:00
.and(warp::filters::body::form())
.then(handle_auth);
2023-09-27 02:43:33 +00:00
2023-10-03 15:10:37 +00:00
let upload_handler = warp::path!("upload")
.and(warp::post())
2023-10-03 15:10:37 +00:00
.and(with_app(app.clone()))
.and(with_session())
.then(handle_upload);
2023-09-21 04:00:09 +00:00
let thumbnail = warp::path!(String / "tn")
.and(warp::get())
2023-09-21 04:00:09 +00:00
.and(warp::header::optional::<String>("if-none-match"))
2023-10-03 15:10:37 +00:00
.and(with_app(app.clone()))
.then(move |id, old_etags, app: App| thumbnail(app, id, old_etags));
let file = warp::path!(String)
.and(warp::get())
.and(warp::header::optional::<String>("if-none-match"))
2023-10-03 15:10:37 +00:00
.and(with_app(app.clone()))
.then(move |id, old_etags, app: App| file(app, id, old_etags));
2023-09-21 03:31:52 +00:00
let server = warp::serve(
2023-10-03 15:10:37 +00:00
root.or(auth).with(log), /*
root.or(auth)
.or(thumbnail)
.or(file)
.or(upload_handler)
.with(log),
*/
);
2023-10-03 15:10:37 +00:00
server
.run(SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 8002))
.await;
}