create dirs + delete dirs + delete files

This commit is contained in:
2025-07-13 03:18:22 +02:00
parent 18be642181
commit b4731f6f81
53 changed files with 1056 additions and 59 deletions

View File

@@ -0,0 +1,24 @@
use axum::extract::{Path, State};
use serde::Deserialize;
use uuid::Uuid;
use crate::{Result, api::AppState, warrens::Warren};
#[derive(Debug, Deserialize)]
pub(super) struct CreateWarrenDirectoryPath {
warren_id: Uuid,
rest: String,
}
pub(super) async fn route(
State(state): State<AppState>,
Path(path): Path<CreateWarrenDirectoryPath>,
) -> Result<()> {
let warren = Warren::get(state.pool(), &path.warren_id).await?;
warren
.create_directory(state.serve_dir(), path.rest)
.await?;
Ok(())
}

View File

@@ -0,0 +1,31 @@
use axum::extract::{Path, Query, State};
use serde::Deserialize;
use uuid::Uuid;
use crate::{Result, api::AppState, fs::FileType, warrens::Warren};
#[derive(Deserialize)]
pub(super) struct DeleteWarrenDirectoryPath {
warren_id: Uuid,
rest: String,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DeleteWarrenParams {
file_type: FileType,
}
pub(super) async fn route(
State(state): State<AppState>,
Path(path): Path<DeleteWarrenDirectoryPath>,
Query(DeleteWarrenParams { file_type }): Query<DeleteWarrenParams>,
) -> Result<()> {
let warren = Warren::get(state.pool(), &path.warren_id).await?;
warren
.delete_entry(state.serve_dir(), path.rest, file_type)
.await?;
Ok(())
}

View File

@@ -1,7 +1,9 @@
mod create_directory;
mod delete_directory;
mod get_warren_path;
mod list_warrens;
use axum::routing::get;
use axum::routing::{delete, get, post};
use crate::server::Router;
@@ -10,4 +12,6 @@ pub(super) fn router() -> Router {
.route("/", get(list_warrens::route))
.route("/{warren_id}", get(get_warren_path::route))
.route("/{warren_id}/{*rest}", get(get_warren_path::route))
.route("/{warren_id}/{*rest}", post(create_directory::route))
.route("/{warren_id}/{*rest}", delete(delete_directory::route))
}

View File

@@ -36,3 +36,21 @@ where
Ok(files)
}
pub async fn create_dir<P>(path: P) -> Result<()>
where
P: AsRef<Path>,
{
fs::create_dir(path).await?;
Ok(())
}
pub async fn delete_dir<P>(path: P) -> Result<()>
where
P: AsRef<Path>,
{
fs::remove_dir_all(path).await?;
Ok(())
}

14
backend/src/fs/file.rs Normal file
View File

@@ -0,0 +1,14 @@
use std::path::Path;
use tokio::fs;
use crate::Result;
pub async fn delete_file<P>(path: P) -> Result<()>
where
P: AsRef<Path>,
{
fs::remove_file(path).await?;
Ok(())
}

View File

@@ -1,9 +1,11 @@
mod dir;
mod file;
pub use dir::*;
pub use file::*;
use serde::Serialize;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, Serialize)]
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum FileType {
File,

View File

@@ -22,9 +22,11 @@ pub(super) async fn start(pool: Pool<Postgres>) -> Result<()> {
let mut app = Router::new()
.nest("/api", api::router())
.layer(
CorsLayer::new().allow_origin(cors::AllowOrigin::exact(HeaderValue::from_str(
&cors_origin,
)?)),
CorsLayer::new()
.allow_origin(cors::AllowOrigin::exact(HeaderValue::from_str(
&cors_origin,
)?))
.allow_methods(cors::Any),
)
.with_state(AppState::new(pool, serve_dir));

View File

@@ -8,7 +8,7 @@ use uuid::Uuid;
use crate::{
Result,
fs::{DirectoryEntry, get_dir_entries},
fs::{DirectoryEntry, FileType, create_dir, delete_dir, delete_file, get_dir_entries},
};
#[derive(Debug, Clone, Serialize, FromRow)]
@@ -40,14 +40,40 @@ impl Warren {
serve_path: &str,
path: Option<String>,
) -> Result<Vec<DirectoryEntry>> {
let mut final_path = PathBuf::from(serve_path);
let path = build_path(serve_path, &self.path, path.as_deref());
final_path.push(self.path.strip_prefix("/").unwrap_or(&self.path));
get_dir_entries(path).await
}
if let Some(ref rest) = path {
final_path.push(rest);
pub async fn create_directory(&self, serve_path: &str, path: String) -> Result<()> {
let path = build_path(serve_path, &self.path, Some(&path));
create_dir(path).await
}
pub async fn delete_entry(
&self,
serve_path: &str,
path: String,
file_type: FileType,
) -> Result<()> {
let path = build_path(serve_path, &self.path, Some(&path));
match file_type {
FileType::File => delete_file(path).await,
FileType::Directory => delete_dir(path).await,
}
get_dir_entries(final_path).await
}
}
fn build_path(serve_path: &str, warren_path: &str, rest_path: Option<&str>) -> PathBuf {
let mut final_path = PathBuf::from(serve_path);
final_path.push(warren_path.strip_prefix("/").unwrap_or(warren_path));
if let Some(ref rest) = rest_path {
final_path.push(rest);
}
final_path
}