This commit is contained in:
2025-07-16 08:24:58 +02:00
parent a2cb58867c
commit aeda56863b
22 changed files with 370 additions and 309 deletions

View File

@@ -1,7 +1,10 @@
use std::time::UNIX_EPOCH;
use anyhow::{Context, anyhow, bail};
use tokio::{fs, io::AsyncWriteExt as _};
use tokio::{
fs,
io::{self, AsyncWriteExt as _},
};
use crate::domain::warren::{
models::file::{
@@ -91,9 +94,13 @@ impl FileSystem {
/// Actually created a directory in the underlying file system
///
/// * `path`: The directory's absolute path (absolute not in relation to the root file system but `self.base_directory`)
async fn create_dir(&self, path: &AbsoluteFilePath) -> anyhow::Result<FilePath> {
async fn create_dir(&self, path: &AbsoluteFilePath) -> io::Result<FilePath> {
let file_path = self.get_target_path(path);
if fs::try_exists(&file_path).await? {
return Err(io::ErrorKind::AlreadyExists.into());
}
fs::create_dir(&file_path).await?;
Ok(file_path)
@@ -103,7 +110,7 @@ impl FileSystem {
///
/// * `path`: The directory's absolute path (absolute not in relation to the root file system but `self.base_directory`)
/// * `force`: Whether to delete directories that are not empty
async fn remove_dir(&self, path: &AbsoluteFilePath, force: bool) -> anyhow::Result<FilePath> {
async fn remove_dir(&self, path: &AbsoluteFilePath, force: bool) -> io::Result<FilePath> {
let file_path = self.get_target_path(path);
if force {
@@ -157,7 +164,7 @@ impl FileSystem {
};
if fs::try_exists(&new_path).await? {
bail!("File already exists");
bail!("File exists");
}
fs::rename(current_path, &new_path).await?;
@@ -182,35 +189,40 @@ impl FileSystemRepository for FileSystem {
&self,
request: CreateDirectoryRequest,
) -> Result<FilePath, CreateDirectoryError> {
let created_path = self.create_dir(request.path()).await.context(format!(
"Failed to create directory at path {}",
request.path()
))?;
Ok(created_path)
match self.create_dir(request.path()).await {
Ok(path) => Ok(path),
Err(e) => match e.kind() {
std::io::ErrorKind::AlreadyExists => Err(CreateDirectoryError::Exists),
_ => Err(anyhow!("Failed to create directory at path: {}", request.path()).into()),
},
}
}
async fn delete_directory(
&self,
request: DeleteDirectoryRequest,
) -> Result<FilePath, DeleteDirectoryError> {
let deleted_path = self
.remove_dir(request.path(), false)
.await
.context(format!("Failed to delete directory at {}", request.path()))?;
Ok(deleted_path)
match self.remove_dir(request.path(), request.force()).await {
Ok(deleted_path) => return Ok(deleted_path),
Err(e) => match e.kind() {
std::io::ErrorKind::NotFound => Err(DeleteDirectoryError::NotFound),
std::io::ErrorKind::DirectoryNotEmpty => Err(DeleteDirectoryError::NotEmpty),
_ => Err(anyhow!("Failed to delete directory at {}: {e:?}", request.path()).into()),
},
}
}
async fn create_file(&self, request: CreateFileRequest) -> Result<FilePath, CreateFileError> {
let file_path = self
.write_file(request.path(), request.data())
.await
.context(format!(
"Failed to write {} byte(s) to path {}",
request.data().len(),
request.path()
))?;
.map_err(|e| {
anyhow!(
"Failed to write {} byte(s) to path {}: {e:?}",
request.data().len(),
request.path()
)
})?;
Ok(file_path)
}
@@ -231,11 +243,13 @@ impl FileSystemRepository for FileSystem {
let new_path = self
.rename(request.path(), request.new_name())
.await
.context(format!(
"Failed to rename {} to {}",
request.path(),
request.new_name()
))?;
.map_err(|e| {
anyhow!(
"Failed to rename {} to {}: {e:?}",
request.path(),
request.new_name()
)
})?;
Ok(new_path)
}

View File

@@ -11,114 +11,114 @@ impl MetricsDebugLogger {
impl WarrenMetrics for MetricsDebugLogger {
async fn record_warren_list_success(&self) {
log::debug!("[Metrics] Warren list succeeded");
tracing::debug!("[Metrics] Warren list succeeded");
}
async fn record_warren_list_failure(&self) {
log::debug!("[Metrics] Warren list failed");
tracing::debug!("[Metrics] Warren list failed");
}
async fn record_warren_fetch_success(&self) {
log::debug!("[Metrics] Warren fetch succeeded");
tracing::debug!("[Metrics] Warren fetch succeeded");
}
async fn record_warren_fetch_failure(&self) {
log::debug!("[Metrics] Warren fetch failed");
tracing::debug!("[Metrics] Warren fetch failed");
}
async fn record_list_warren_files_success(&self) {
log::debug!("[Metrics] Warren list files succeeded");
tracing::debug!("[Metrics] Warren list files succeeded");
}
async fn record_list_warren_files_failure(&self) {
log::debug!("[Metrics] Warren list files failed");
tracing::debug!("[Metrics] Warren list files failed");
}
async fn record_warren_directory_creation_success(&self) {
log::debug!("[Metrics] Warren directory creation succeeded");
tracing::debug!("[Metrics] Warren directory creation succeeded");
}
async fn record_warren_directory_creation_failure(&self) {
log::debug!("[Metrics] Warren directory creation failed");
tracing::debug!("[Metrics] Warren directory creation failed");
}
async fn record_warren_directory_deletion_success(&self) {
log::debug!("[Metrics] Warren directory deletion succeeded");
tracing::debug!("[Metrics] Warren directory deletion succeeded");
}
async fn record_warren_directory_deletion_failure(&self) {
log::debug!("[Metrics] Warren directory deletion failed");
tracing::debug!("[Metrics] Warren directory deletion failed");
}
async fn record_warren_file_upload_success(&self) {
log::debug!("[Metrics] Warren file upload succeeded");
tracing::debug!("[Metrics] Warren file upload succeeded");
}
async fn record_warren_file_upload_failure(&self) {
log::debug!("[Metrics] Warren file upload failed");
tracing::debug!("[Metrics] Warren file upload failed");
}
async fn record_warren_files_upload_success(&self) {
log::debug!("[Metrics] Warren files upload succeded");
tracing::debug!("[Metrics] Warren files upload succeded");
}
async fn record_warren_files_upload_failure(&self) {
log::debug!("[Metrics] Warren files upload failed at least partially");
tracing::debug!("[Metrics] Warren files upload failed at least partially");
}
async fn record_warren_file_deletion_success(&self) {
log::debug!("[Metrics] Warren file deletion succeeded");
tracing::debug!("[Metrics] Warren file deletion succeeded");
}
async fn record_warren_file_deletion_failure(&self) {
log::debug!("[Metrics] Warren file deletion failed");
tracing::debug!("[Metrics] Warren file deletion failed");
}
async fn record_warren_entry_rename_success(&self) {
log::debug!("[Metrics] Warren entry rename succeeded");
tracing::debug!("[Metrics] Warren entry rename succeeded");
}
async fn record_warren_entry_rename_failure(&self) {
log::debug!("[Metrics] Warren entry rename failed");
tracing::debug!("[Metrics] Warren entry rename failed");
}
}
impl FileSystemMetrics for MetricsDebugLogger {
async fn record_list_files_success(&self) {
log::debug!("[Metrics] File list succeeded");
tracing::debug!("[Metrics] File list succeeded");
}
async fn record_list_files_failure(&self) {
log::debug!("[Metrics] File list failed");
tracing::debug!("[Metrics] File list failed");
}
async fn record_directory_creation_success(&self) {
log::debug!("[Metrics] Directory creation succeeded");
tracing::debug!("[Metrics] Directory creation succeeded");
}
async fn record_directory_creation_failure(&self) {
log::debug!("[Metrics] Directory creation failed");
tracing::debug!("[Metrics] Directory creation failed");
}
async fn record_directory_deletion_success(&self) {
log::debug!("[Metrics] Directory deletion succeeded");
tracing::debug!("[Metrics] Directory deletion succeeded");
}
async fn record_directory_deletion_failure(&self) {
log::debug!("[Metrics] Directory deletion failed");
tracing::debug!("[Metrics] Directory deletion failed");
}
async fn record_file_creation_success(&self) {
log::debug!("[Metrics] File creation succeeded");
tracing::debug!("[Metrics] File creation succeeded");
}
async fn record_file_creation_failure(&self) {
log::debug!("[Metrics] File creation failed");
tracing::debug!("[Metrics] File creation failed");
}
async fn record_file_deletion_success(&self) {
log::debug!("[Metrics] File deletion succeeded");
tracing::debug!("[Metrics] File deletion succeeded");
}
async fn record_file_deletion_failure(&self) {
log::debug!("[Metrics] File deletion failed");
tracing::debug!("[Metrics] File deletion failed");
}
async fn record_entry_rename_success(&self) -> () {
log::debug!("[Metrics] Entry rename succeeded");
tracing::debug!("[Metrics] Entry rename succeeded");
}
async fn record_entry_rename_failure(&self) -> () {
log::debug!("[Metrics] Entry rename failed");
tracing::debug!("[Metrics] Entry rename failed");
}
}

View File

@@ -17,15 +17,15 @@ impl NotifierDebugLogger {
impl WarrenNotifier for NotifierDebugLogger {
async fn warrens_listed(&self, warrens: &Vec<Warren>) {
log::debug!("[Notifier] Listed {} warren(s)", warrens.len());
tracing::debug!("[Notifier] Listed {} warren(s)", warrens.len());
}
async fn warren_fetched(&self, warren: &Warren) {
log::debug!("[Notifier] Fetched warren {}", warren.name());
tracing::debug!("[Notifier] Fetched warren {}", warren.name());
}
async fn warren_files_listed(&self, warren: &Warren, files: &Vec<File>) {
log::debug!(
tracing::debug!(
"[Notifier] Listed {} file(s) in warren {}",
files.len(),
warren.name()
@@ -33,7 +33,7 @@ impl WarrenNotifier for NotifierDebugLogger {
}
async fn warren_directory_created(&self, warren: &Warren, path: &FilePath) {
log::debug!(
tracing::debug!(
"[Notifier] Created directory {} in warren {}",
path,
warren.name()
@@ -41,7 +41,7 @@ impl WarrenNotifier for NotifierDebugLogger {
}
async fn warren_directory_deleted(&self, warren: &Warren, path: &FilePath) {
log::debug!(
tracing::debug!(
"[Notifier] Deleted directory {} in warren {}",
path,
warren.name()
@@ -49,7 +49,7 @@ impl WarrenNotifier for NotifierDebugLogger {
}
async fn warren_file_uploaded(&self, warren: &Warren, path: &FilePath) {
log::debug!(
tracing::debug!(
"[Notifier] Uploaded file {} to warren {}",
path,
warren.name()
@@ -57,7 +57,7 @@ impl WarrenNotifier for NotifierDebugLogger {
}
async fn warren_files_uploaded(&self, warren: &Warren, paths: &[FilePath]) {
log::debug!(
tracing::debug!(
"[Notifier] Uploaded {} file(s) to warren {}",
paths.len(),
warren.name()
@@ -65,7 +65,7 @@ impl WarrenNotifier for NotifierDebugLogger {
}
async fn warren_file_deleted(&self, warren: &Warren, path: &FilePath) {
log::debug!(
tracing::debug!(
"[Notifier] Deleted file {} from warren {}",
path,
warren.name(),
@@ -78,7 +78,7 @@ impl WarrenNotifier for NotifierDebugLogger {
old_path: &crate::domain::warren::models::file::AbsoluteFilePath,
new_path: &FilePath,
) {
log::debug!(
tracing::debug!(
"[Notifier] Renamed file {} to {} in warren {}",
old_path,
new_path,
@@ -89,26 +89,26 @@ impl WarrenNotifier for NotifierDebugLogger {
impl FileSystemNotifier for NotifierDebugLogger {
async fn files_listed(&self, files: &Vec<File>) {
log::debug!("[Notifier] Listed {} file(s)", files.len());
tracing::debug!("[Notifier] Listed {} file(s)", files.len());
}
async fn directory_created(&self, path: &FilePath) {
log::debug!("[Notifier] Created directory {}", path);
tracing::debug!("[Notifier] Created directory {}", path);
}
async fn directory_deleted(&self, path: &FilePath) {
log::debug!("[Notifier] Deleted directory {}", path);
tracing::debug!("[Notifier] Deleted directory {}", path);
}
async fn file_created(&self, path: &FilePath) {
log::debug!("[Notifier] Created file {}", path);
tracing::debug!("[Notifier] Created file {}", path);
}
async fn file_deleted(&self, path: &FilePath) {
log::debug!("[Notifier] Deleted file {}", path);
tracing::debug!("[Notifier] Deleted file {}", path);
}
async fn entry_renamed(&self, old_path: &FilePath, new_path: &FilePath) {
log::debug!("[Notifier] Renamed file {} to {}", old_path, new_path);
tracing::debug!("[Notifier] Renamed file {} to {}", old_path, new_path);
}
}

View File

@@ -114,7 +114,7 @@ impl WarrenRepository for Postgres {
let warrens = self
.list_warrens(&mut connection)
.await
.map_err(|err| anyhow!(err).context("Failed to fetch warren with id"))?;
.map_err(|err| anyhow!(err).context("Failed to list warrens"))?;
Ok(warrens)
}