backend refactor file_system into warren domain
This commit is contained in:
@@ -3,12 +3,12 @@ use std::time::UNIX_EPOCH;
|
||||
use anyhow::{Context, anyhow};
|
||||
use tokio::{fs, io::AsyncWriteExt as _};
|
||||
|
||||
use crate::domain::file_system::{
|
||||
use crate::domain::warren::{
|
||||
models::file::{
|
||||
AbsoluteFilePath, CreateDirectoryError, CreateDirectoryRequest, CreateFileError,
|
||||
CreateFileRequest, DeleteDirectoryError, DeleteDirectoryRequest, DeleteFileError,
|
||||
DeleteFileRequest, File, FileMimeType, FileName, FilePath, FileType, ListFilesError,
|
||||
ListFilesRequest,
|
||||
ListFilesRequest, RenameEntryError, RenameEntryRequest,
|
||||
},
|
||||
ports::FileSystemRepository,
|
||||
};
|
||||
@@ -139,6 +139,27 @@ impl FileSystem {
|
||||
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
async fn rename(
|
||||
&self,
|
||||
path: &AbsoluteFilePath,
|
||||
new_name: &FileName,
|
||||
) -> anyhow::Result<FilePath> {
|
||||
let current_path = self.get_target_path(path);
|
||||
|
||||
let new_path = {
|
||||
let mut c = current_path.to_string();
|
||||
let last_slash_index = c.rfind('/').unwrap();
|
||||
c.drain((last_slash_index + 1)..);
|
||||
c.push_str(new_name.as_str());
|
||||
|
||||
FilePath::new(&c)?
|
||||
};
|
||||
|
||||
fs::rename(current_path, &new_path).await?;
|
||||
|
||||
Ok(new_path)
|
||||
}
|
||||
}
|
||||
|
||||
impl FileSystemRepository for FileSystem {
|
||||
@@ -198,4 +219,20 @@ impl FileSystemRepository for FileSystem {
|
||||
|
||||
Ok(deleted_path)
|
||||
}
|
||||
|
||||
async fn rename_entry(
|
||||
&self,
|
||||
request: RenameEntryRequest,
|
||||
) -> Result<FilePath, RenameEntryError> {
|
||||
let new_path = self
|
||||
.rename(request.path(), request.new_name())
|
||||
.await
|
||||
.context(format!(
|
||||
"Failed to rename {} to {}",
|
||||
request.path(),
|
||||
request.new_name()
|
||||
))?;
|
||||
|
||||
Ok(new_path)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user