Compare commits

...

2 Commits

Author SHA1 Message Date
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 49 additions and 8 deletions

View File

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

View File

@@ -12,4 +12,29 @@
</form> </form>
</body> </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> </html>