|
1 | 1 | #![allow(clippy::missing_errors_doc)]
|
2 | 2 | #![allow(clippy::unnecessary_struct_initialization)]
|
3 | 3 | #![allow(clippy::unused_async)]
|
4 |
| -use std::{fs, io::Write, path::PathBuf}; |
| 4 | +use std::path::PathBuf; |
5 | 5 |
|
6 | 6 | use axum::{body::Body, debug_handler, extract::Multipart};
|
7 | 7 | use loco_rs::prelude::*;
|
8 | 8 | use sea_orm::QueryOrder;
|
| 9 | +use tokio::{fs, io::AsyncWriteExt}; |
9 | 10 | use tokio_util::io::ReaderStream;
|
10 | 11 |
|
11 | 12 | use crate::models::_entities::files;
|
@@ -46,16 +47,17 @@ pub async fn upload(
|
46 | 47 | let uuid = uuid::Uuid::new_v4().to_string();
|
47 | 48 | let folder = format!("{now}_{uuid}");
|
48 | 49 | let upload_folder = PathBuf::from(UPLOAD_DIR).join(&folder);
|
49 |
| - fs::create_dir_all(&upload_folder)?; |
| 50 | + fs::create_dir_all(&upload_folder).await?; |
50 | 51 |
|
51 | 52 | // Write the file into the newly created folder
|
52 | 53 | let path = upload_folder.join(file_name);
|
53 | 54 | let mut f = fs::OpenOptions::new()
|
54 | 55 | .create_new(true)
|
55 | 56 | .write(true)
|
56 |
| - .open(&path)?; |
57 |
| - f.write_all(&content)?; |
58 |
| - f.flush()?; |
| 57 | + .open(&path) |
| 58 | + .await?; |
| 59 | + f.write_all(&content).await?; |
| 60 | + f.flush().await?; |
59 | 61 |
|
60 | 62 | // Record the file upload in database
|
61 | 63 | let file = files::ActiveModel {
|
@@ -107,7 +109,7 @@ pub async fn view(
|
107 | 109 | .expect("File not found");
|
108 | 110 |
|
109 | 111 | // Stream the file
|
110 |
| - let file = tokio::fs::File::open(format!("{UPLOAD_DIR}/{}", file.file_path)).await?; |
| 112 | + let file = fs::File::open(format!("{UPLOAD_DIR}/{}", file.file_path)).await?; |
111 | 113 | let stream = ReaderStream::new(file);
|
112 | 114 | let body = Body::from_stream(stream);
|
113 | 115 |
|
|
0 commit comments