diff options
Diffstat (limited to 'src/routes.rs')
-rw-r--r-- | src/routes.rs | 74 |
1 files changed, 42 insertions, 32 deletions
diff --git a/src/routes.rs b/src/routes.rs index c48ec24..15c255e 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -27,22 +27,23 @@ pub async fn routes( let files_dir = Path::new(&files_dir); match (request.method(), path) { - (&Method::GET, [""]) => { - Ok(with_headers( - response(StatusCode::OK, templates::INDEX.to_string()), - vec![(CONTENT_TYPE, "text/html")], - )) - }, + (&Method::GET, [""]) => Ok(with_headers( + response(StatusCode::OK, templates::INDEX), + vec![(CONTENT_TYPE, "text/html")], + )), (&Method::GET, ["static", "main.js"]) => Ok(static_file( - include_str!("static/main.js").to_string(), + include_str!("static/main.js"), "application/javascript", )), - (&Method::GET, ["static", "main.css"]) => Ok(static_file( - include_str!("static/main.css").to_string(), - "text/css", - )), - (&Method::POST, ["upload"]) => upload_file(request, db_conn, authorized_key, files_dir).await, - (&Method::GET, ["share", file_id]) => get(db_conn, file_id, GetFile::ShowPage, files_dir).await, + (&Method::GET, ["static", "main.css"]) => { + Ok(static_file(include_str!("static/main.css"), "text/css")) + } + (&Method::POST, ["upload"]) => { + upload_file(request, db_conn, authorized_key, files_dir).await + } + (&Method::GET, ["share", file_id]) => { + get(db_conn, file_id, GetFile::ShowPage, files_dir).await + } (&Method::GET, ["share", file_id, "download"]) => { get(db_conn, file_id, GetFile::Download, files_dir).await } @@ -59,10 +60,7 @@ async fn upload_file( let key = get_header(&request, "X-Key"); if key != Some(authorized_key) { log::info!("Unauthorized file upload"); - Ok(response( - StatusCode::UNAUTHORIZED, - "Unauthorized".to_string(), - )) + Ok(response(StatusCode::UNAUTHORIZED, "Unauthorized")) } else { let file_id = model::generate_file_id(); let filename = get_header(&request, "X-Filename").map(|s| util::sanitize_filename(&s)); @@ -92,7 +90,7 @@ async fn upload_file( content_length, }; - match db::insert_file(&db_conn, file.clone()).await { + match db::files::insert(&db_conn, file.clone()).await { Ok(_) => Ok(response(StatusCode::OK, file_id)), Err(msg) => { log::error!("Insert file: {msg}"); @@ -128,14 +126,12 @@ async fn get( get_file: GetFile, files_dir: &Path, ) -> Result<Response<BoxBody<Bytes, std::io::Error>>> { - let file = db::get_file(&db_conn, file_id.to_string()).await; + let file = db::files::get(&db_conn, file_id).await; match (get_file, file) { - (GetFile::ShowPage, Ok(Some(file))) => { - Ok(with_headers( - response(StatusCode::OK, templates::file_page(file)), - vec![(CONTENT_TYPE, "text/html")], - )) - } + (GetFile::ShowPage, Ok(Some(file))) => Ok(with_headers( + response(StatusCode::OK, templates::file_page(file)), + vec![(CONTENT_TYPE, "text/html")], + )), (GetFile::Download, Ok(Some(file))) => { let path = files_dir.join(file_id); Ok(stream_file(path, file).await) @@ -148,17 +144,31 @@ async fn get( } } -fn static_file(text: String, content_type: &str) -> Response<BoxBody<Bytes, std::io::Error>> { +fn static_file( + text: impl Into<String>, + content_type: &str, +) -> Response<BoxBody<Bytes, std::io::Error>> { let response = Response::builder() - .body(Full::new(text.into()).map_err(|e| match e {}).boxed()) + .body( + Full::new(text.into().into()) + .map_err(|e| match e {}) + .boxed(), + ) .unwrap(); with_headers(response, vec![(CONTENT_TYPE, content_type)]) } -fn response(status_code: StatusCode, text: String) -> Response<BoxBody<Bytes, std::io::Error>> { +fn response( + status_code: StatusCode, + text: impl Into<String>, +) -> Response<BoxBody<Bytes, std::io::Error>> { Response::builder() .status(status_code) - .body(Full::new(text.into()).map_err(|e| match e {}).boxed()) + .body( + Full::new(text.into().into()) + .map_err(|e| match e {}) + .boxed(), + ) .unwrap() } @@ -190,17 +200,17 @@ async fn stream_file(path: PathBuf, file: model::File) -> Response<BoxBody<Bytes } fn not_found() -> Response<BoxBody<Bytes, std::io::Error>> { - response(StatusCode::NOT_FOUND, templates::NOT_FOUND.to_string()) + response(StatusCode::NOT_FOUND, templates::NOT_FOUND) } fn bad_request() -> Response<BoxBody<Bytes, std::io::Error>> { - response(StatusCode::BAD_REQUEST, templates::BAD_REQUEST.to_string()) + response(StatusCode::BAD_REQUEST, templates::BAD_REQUEST) } fn internal_server_error() -> Response<BoxBody<Bytes, std::io::Error>> { response( StatusCode::INTERNAL_SERVER_ERROR, - templates::INTERNAL_SERVER_ERROR.to_string(), + templates::INTERNAL_SERVER_ERROR, ) } |