From 45368dcc9a20df606559e37e1cab8ae0cec5c048 Mon Sep 17 00:00:00 2001 From: 409 <409dev@protonmail.com> Date: Tue, 29 Jul 2025 15:22:32 +0200 Subject: [PATCH] actually return file paths in `FileSystem::save` --- .../src/lib/domain/warren/models/file/mod.rs | 20 ++++++++++++++++--- backend/src/lib/outbound/file_system.rs | 8 ++++---- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/backend/src/lib/domain/warren/models/file/mod.rs b/backend/src/lib/domain/warren/models/file/mod.rs index e1c0a60..148b97f 100644 --- a/backend/src/lib/domain/warren/models/file/mod.rs +++ b/backend/src/lib/domain/warren/models/file/mod.rs @@ -151,9 +151,21 @@ impl AsRef for FilePath { } impl RelativeFilePath { - /* pub fn new(path: FilePath) -> Self { - Self(path.0.trim_start_matches('/').to_owned()) - } */ + pub fn new(path: FilePath) -> Result { + Self::from_str(&path.0) + } + + pub fn from_str(raw: &str) -> Result { + if raw.starts_with("/") { + return Err(RelativeFilePathError::Absolute); + } + + if raw.is_empty() { + return Err(RelativeFilePathError::Empty); + } + + Ok(Self(raw.to_owned())) + } pub fn join(mut self, other: &RelativeFilePath) -> Self { self.0.push('/'); @@ -197,6 +209,8 @@ pub enum FilePathError { #[derive(Clone, Debug, Error)] pub enum RelativeFilePathError { + #[error("The path is empty")] + Empty, #[error("A relative file path must not with a slash")] Absolute, } diff --git a/backend/src/lib/outbound/file_system.rs b/backend/src/lib/outbound/file_system.rs index d8376ef..dfd2ba3 100644 --- a/backend/src/lib/outbound/file_system.rs +++ b/backend/src/lib/outbound/file_system.rs @@ -167,12 +167,10 @@ impl FileSystem { ) -> anyhow::Result> { let base_path = self.get_target_path(path); - let paths = Vec::new(); + let mut paths = Vec::new(); while let Ok(Some(mut upload_file)) = stream.try_next().await { - // TODO: Refactor this result question mark chain thing - let file_name_as_path: RelativeFilePath = - FilePath::new(upload_file.file_name().as_str())?.try_into()?; + let file_name_as_path = RelativeFilePath::from_str(upload_file.file_name().as_str())?; let file_path = base_path.join(&file_name_as_path); let mut file = fs::OpenOptions::new() @@ -184,6 +182,8 @@ impl FileSystem { while let Ok(Some(chunk)) = upload_file.try_next().await { file.write(&chunk).await?; } + + paths.push(path.clone().join(&file_name_as_path)); } Ok(paths)