Compare commits

..

3 Commits

Author SHA1 Message Date
Shawn Bloecker
e62b6ae0c3 change file options 2025-10-01 14:24:56 +02:00
409
f1cd09a218 center inputs on page 2025-10-01 13:47:05 +02:00
409
5dc06b24e6 upload in chunks 2025-10-01 13:44:23 +02:00
2 changed files with 48 additions and 8 deletions

View File

@@ -7,7 +7,7 @@ use axum::{
response::Html,
routing::{get, post},
};
use tokio::{net::TcpListener, time::Instant};
use tokio::{io::AsyncWriteExt, net::TcpListener, time::Instant};
#[tokio::main]
async fn main() -> Result<()> {
@@ -25,25 +25,40 @@ async fn main() -> Result<()> {
async fn upload(mut multipart: Multipart) -> Result<Html<String>, StatusCode> {
let now = Instant::now();
let field = multipart
println!("Starting upload");
let mut field = multipart
.next_field()
.await
.map_err(|_| StatusCode::BAD_REQUEST)?
.ok_or(StatusCode::BAD_REQUEST)?;
println!("Got field");
let file_name = field.file_name().ok_or(StatusCode::BAD_REQUEST)?.to_owned();
let bytes = field.bytes().await.map_err(|e| {
dbg!(e);
StatusCode::BAD_REQUEST
})?;
println!("Got file name: {file_name}");
tokio::fs::write(format!("./uploads/{file_name}"), bytes)
let mut file = tokio::fs::OpenOptions::new()
.write(true)
.truncate(true)
.create(true)
.open(format!("./uploads/{file_name}"))
.await
.map_err(|_| StatusCode::BAD_REQUEST)?;
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
println!("Writing to file");
while let Ok(Some(chunk)) = field.chunk().await {
file.write(&chunk)
.await
.map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?;
}
let elapsed = now.elapsed();
println!("Finished writing to file: {elapsed:?}");
Ok(Html(
UploadedTemplate {
time: &format!("{elapsed:?}"),

View File

@@ -12,4 +12,29 @@
</form>
</body>
<style>
html,
body {
width: 100%;
height: 100%;
}
body, form {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
gap: 4px;
}
input {
width: 300px;
}
button {
font-size: 20px;
width: 100%;
}
</style>
</html>