server (/fs/upload): create all directories required to save the file so you don't get the message that the folder doesn't exist

This commit is contained in:
MedzikUser 2022-04-30 22:19:53 +02:00
parent 860fd76969
commit cdc185c4d8
No known key found for this signature in database
GPG Key ID: A5FAC1E185C112DB
2 changed files with 20 additions and 11 deletions

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"rpc.enabled": true
}

View File

@ -26,20 +26,26 @@ pub async fn handle(
let content = base64::decode(request.content) let content = base64::decode(request.content)
.map_err(|err| ServerError::FsError(FsError::Base64(err.to_string())))?; .map_err(|err| ServerError::FsError(FsError::Base64(err.to_string())))?;
// check if file already exists let path = format!(
if Path::new(&request.path).exists() { "{path}/{username}/{filepath}",
path = config.storage.path,
username = res.username,
filepath = request.path
);
let path = Path::new(&path);
// check if the file currently exists to avoid overwriting it
if path.exists() {
return Err(ServerError::FsError(FsError::FileAlreadyExists)); return Err(ServerError::FsError(FsError::FileAlreadyExists));
} }
let folder_path = format!( // create a directorys where the file will be placed
"{path}/{username}", // e.g. path ==> `/secret/files/images/screenshot.png`
path = config.storage.path, // directories up to `/home/homedisk/{username}/secret/files/images/` will be created
username = res.username, match path.parent() {
); Some(prefix) => fs::create_dir_all(&prefix).unwrap(),
let path = format!("{folder_path}/{filepath}", filepath = request.path); None => (),
}
// create user directory
fs::create_dir_all(&folder_path).unwrap();
// write file // write file
fs::write(&path, content) fs::write(&path, content)