Skip to content

Commit 4d6bc1a

Browse files
committed
feat(#541): delete tempfiles of datasets
Temp-files of dataset-uploads are now deleted again after the successful or canceled dataset-upload. Signed-off-by: Tobias Anker <tobias.anker@kitsunemimi.moe>
1 parent fd1ee42 commit 4d6bc1a

1 file changed

Lines changed: 55 additions & 24 deletions

File tree

src/hanami/src/api/http_endpoints/dataset/create_dataset_v1_0.rs

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use apistos::api_operation;
2020
use futures_util::StreamExt;
2121
use tokio::io::AsyncWriteExt;
2222
use tokio::fs;
23-
use log::error;
23+
use log::{error, debug};
2424
use std::path::PathBuf;
2525
use uuid::Uuid;
2626

@@ -112,19 +112,39 @@ pub async fn upload_binary(mut payload: Multipart, path: Path<(String, String)>,
112112
}
113113
};
114114

115-
temp_file_paths.push(temp_file_path);
115+
temp_file_paths.push(temp_file_path.clone());
116116

117117
// fill content into file
118-
while let Some(chunk) = field.next().await {
119-
let data = match chunk {
120-
Ok(value) => value,
121-
Err(e) => {
122-
error!("{}", e);
123-
return Err(ErrorResponse::BadRequest("Failed to read chunk.".to_string()));
124-
}
125-
};
118+
let result = async {
119+
while let Some(chunk) = field.next().await {
120+
let data = match chunk {
121+
Ok(value) => value,
122+
Err(e) => {
123+
error!("{}", e);
124+
return Err(ErrorResponse::BadRequest("Failed to read chunk.".to_string()));
125+
}
126+
};
127+
128+
let _ = f.write_all(&data).await;
129+
}
126130

127-
let _ = f.write_all(&data).await;
131+
Ok(())
132+
}
133+
.await;
134+
135+
match result {
136+
Ok(_) => {},
137+
Err(e) => {
138+
debug!("Dataset-upload broken or canceled.");
139+
match std::fs::remove_file(&temp_file_path) {
140+
Ok(()) => {},
141+
Err(e) => {
142+
let tempfile_path_str: String = temp_file_path.to_string_lossy().into();
143+
error!("Failed to delete temp-file {tempfile_path_str} from disc with error {}.", e);
144+
}
145+
}
146+
return Err(e);
147+
}
128148
}
129149
}
130150

@@ -168,23 +188,34 @@ pub async fn upload_binary(mut payload: Multipart, path: Path<(String, String)>,
168188
};
169189

170190
// get new created dataset from database to get addtional information
171-
match dataset_table::get_dataset(&dataset_uuid, &context) {
172-
Ok(dataset) => {
173-
let resp = DatasetResp {
174-
uuid: dataset_uuid.clone(),
175-
name: dataset.name.clone(),
176-
created_by: dataset.created_by.clone(),
177-
created_at: dataset.created_at.clone(),
178-
updated_by: dataset.updated_by.clone(),
179-
updated_at: dataset.updated_at.clone(),
180-
};
181-
182-
return Ok(CreatedJson(resp));
183-
},
191+
let dataset = match dataset_table::get_dataset(&dataset_uuid, &context) {
192+
Ok(dataset) => dataset,
184193
Err(_) =>
185194
{
186195
error!("Failed to get dataset with ID '{dataset_uuid}' from database, even the user should exist.");
187196
return Err(ErrorResponse::InternalError("".to_string()));
188197
}
189198
};
199+
200+
for file_path in temp_file_paths {
201+
match std::fs::remove_file(&file_path) {
202+
Ok(()) => {},
203+
Err(e) => {
204+
let tempfile_path_str: String = file_path.to_string_lossy().into();
205+
error!("Failed to delete temp-file {tempfile_path_str} from disc with error {}.", e);
206+
}
207+
}
208+
}
209+
210+
// create response
211+
let resp = DatasetResp {
212+
uuid: dataset_uuid.clone(),
213+
name: dataset.name.clone(),
214+
created_by: dataset.created_by.clone(),
215+
created_at: dataset.created_at.clone(),
216+
updated_by: dataset.updated_by.clone(),
217+
updated_at: dataset.updated_at.clone(),
218+
};
219+
220+
return Ok(CreatedJson(resp));
190221
}

0 commit comments

Comments
 (0)