mirror of
https://github.com/MedzikUser/HomeDisk.git
synced 2024-08-14 21:46:53 +00:00
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:
parent
860fd76969
commit
cdc185c4d8
2 changed files with 20 additions and 11 deletions
3
.vscode/settings.json
vendored
Normal file
3
.vscode/settings.json
vendored
Normal file
|
@ -0,0 +1,3 @@
|
|||
{
|
||||
"rpc.enabled": true
|
||||
}
|
|
@ -26,20 +26,26 @@ pub async fn handle(
|
|||
let content = base64::decode(request.content)
|
||||
.map_err(|err| ServerError::FsError(FsError::Base64(err.to_string())))?;
|
||||
|
||||
// check if file already exists
|
||||
if Path::new(&request.path).exists() {
|
||||
let path = format!(
|
||||
"{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));
|
||||
}
|
||||
|
||||
let folder_path = format!(
|
||||
"{path}/{username}",
|
||||
path = config.storage.path,
|
||||
username = res.username,
|
||||
);
|
||||
let path = format!("{folder_path}/{filepath}", filepath = request.path);
|
||||
|
||||
// create user directory
|
||||
fs::create_dir_all(&folder_path).unwrap();
|
||||
// create a directorys where the file will be placed
|
||||
// e.g. path ==> `/secret/files/images/screenshot.png`
|
||||
// directories up to `/home/homedisk/{username}/secret/files/images/` will be created
|
||||
match path.parent() {
|
||||
Some(prefix) => fs::create_dir_all(&prefix).unwrap(),
|
||||
None => (),
|
||||
}
|
||||
|
||||
// write file
|
||||
fs::write(&path, content)
|
||||
|
|
Loading…
Reference in a new issue