Skip to content

Commit fd1ee42

Browse files
committed
feat(#541): store tempfiles in separate directory again
Temporary files of dataset-uploads are now stored in a separate temp-file-directory again. Signed-off-by: Tobias Anker <tobias.anker@kitsunemimi.moe>
1 parent 2a5d0cb commit fd1ee42

1 file changed

Lines changed: 25 additions & 15 deletions

File tree

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

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use crate::api::errors::ErrorResponse;
2929
use crate::database::dataset_table;
3030
use crate::config;
3131

32-
use hanami_dataset::dataset_io::{DataSetType, init_new_data_set_file, Column};
3332
use hanami_dataset::converter::load_mnist_images;
3433
use hanami_common::error::HanamiError;
3534

@@ -44,10 +43,14 @@ use super::dataset_structs::DatasetResp;
4443
error_code = 500
4544
)]
4645
pub async fn upload_binary(mut payload: Multipart, path: Path<(String, String)>, context: UserContext) -> Result<CreatedJson<DatasetResp>, ErrorResponse> {
47-
let upload_dir_path = config::CONFIG.storage.dataset_location.clone();
48-
let upload_dir = PathBuf::from(&upload_dir_path);
46+
let tempfile_location = config::CONFIG.storage.tempfile_location.clone();
47+
let dataset_location = config::CONFIG.storage.dataset_location.clone();
48+
49+
let tempfile_dir = PathBuf::from(&tempfile_location);
50+
let dataset_dir = PathBuf::from(&dataset_location);
51+
4952
let dataset_uuid = Uuid::new_v4();
50-
let target_filepath: PathBuf = upload_dir.join(&dataset_uuid.to_string());
53+
let target_filepath: PathBuf = dataset_dir.join(&dataset_uuid.to_string());
5154

5255
let (dataset_type_str, name) = path.into_inner();
5356
let dataset_type = dataset_type_str.to_string();
@@ -59,15 +62,22 @@ pub async fn upload_binary(mut payload: Multipart, path: Path<(String, String)>,
5962
}
6063

6164
// Ensure directory exists
62-
match fs::create_dir_all(&upload_dir).await {
65+
match fs::create_dir_all(&tempfile_dir).await {
66+
Ok(_) => (),
67+
Err(e) => {
68+
error!("Failed to create dataset-upload-directory '{tempfile_location}' with error: {e}");
69+
return Err(ErrorResponse::InternalError("".to_string()));
70+
}
71+
}
72+
match fs::create_dir_all(&dataset_dir).await {
6373
Ok(_) => (),
6474
Err(e) => {
65-
error!("Failed to create dataset-upload-directory '{upload_dir_path}' with error: {e}");
75+
error!("Failed to create dataset-upload-directory '{dataset_location}' with error: {e}");
6676
return Err(ErrorResponse::InternalError("".to_string()));
6777
}
6878
}
6979

70-
let mut filepaths = Vec::new();
80+
let mut temp_file_paths = Vec::new();
7181
// process items from payload
7282
while let Some(item) = payload.next().await {
7383
let mut field = match item {
@@ -91,18 +101,18 @@ pub async fn upload_binary(mut payload: Multipart, path: Path<(String, String)>,
91101
};
92102

93103
// create file
94-
let filepath: PathBuf = upload_dir.join(filename + &dataset_uuid.to_string());
95-
let mut f = match fs::File::create(&filepath).await {
104+
let temp_file_path: PathBuf = tempfile_dir.join(filename + &dataset_uuid.to_string());
105+
let mut f = match fs::File::create(&temp_file_path).await {
96106
Ok(value) => value,
97107
Err(e) => {
98-
let path = filepath.as_os_str().to_str().unwrap();
108+
let path = temp_file_path.as_os_str().to_str().unwrap();
99109
let msg = format!("Failed to create upload-file '{path}' with error: {e}.");
100110
error!("{}", msg);
101111
return Err(ErrorResponse::InternalError("".to_string()));
102112
}
103113
};
104114

105-
filepaths.push(filepath);
115+
temp_file_paths.push(temp_file_path);
106116

107117
// fill content into file
108118
while let Some(chunk) = field.next().await {
@@ -120,14 +130,14 @@ pub async fn upload_binary(mut payload: Multipart, path: Path<(String, String)>,
120130

121131
// process mnist-dataset
122132
if dataset_type == "mnist" {
123-
let path_len = filepaths.len();
124-
if filepaths.len() != 2 {
133+
let path_len = temp_file_paths.len();
134+
if temp_file_paths.len() != 2 {
125135
let msg = format!("MNIST-dataset expect 2 uploaded files, but there were {path_len} files found.");
126136
return Err(ErrorResponse::BadRequest(msg));
127137
}
128138
match load_mnist_images(
129-
&filepaths[0],
130-
&filepaths[1],
139+
&temp_file_paths[0],
140+
&temp_file_paths[1],
131141
&target_filepath,
132142
dataset_uuid.clone(),
133143
name.clone(),

0 commit comments

Comments
 (0)