diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..3dc9b0c --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "rpc.enabled": true +} \ No newline at end of file diff --git a/server/src/fs/upload.rs b/server/src/fs/upload.rs index 2f3747e..b5f2add 100644 --- a/server/src/fs/upload.rs +++ b/server/src/fs/upload.rs @@ -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)