96 lines
2.1 KiB
Rust
96 lines
2.1 KiB
Rust
/* pub mod db;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use serde::Serialize;
|
|
use sqlx::prelude::FromRow;
|
|
use uuid::Uuid;
|
|
|
|
use crate::{
|
|
Result,
|
|
fs::{
|
|
DirectoryEntry, FileType, create_dir, delete_dir, delete_file, get_dir_entries, write_file,
|
|
},
|
|
};
|
|
|
|
#[derive(Debug, Clone, Serialize, FromRow)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct Warren {
|
|
id: Uuid,
|
|
name: String,
|
|
#[serde(skip)]
|
|
path: String,
|
|
}
|
|
|
|
impl Warren {
|
|
pub fn id(&self) -> &Uuid {
|
|
&self.id
|
|
}
|
|
|
|
pub fn name(&self) -> &str {
|
|
&self.name
|
|
}
|
|
|
|
pub fn path(&self) -> &str {
|
|
&self.path
|
|
}
|
|
}
|
|
|
|
impl Warren {
|
|
pub async fn read_path(
|
|
&self,
|
|
serve_path: &str,
|
|
path: Option<String>,
|
|
) -> Result<Vec<DirectoryEntry>> {
|
|
let path = build_path(serve_path, &self.path, path.as_deref());
|
|
|
|
get_dir_entries(path).await
|
|
}
|
|
|
|
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,
|
|
}
|
|
}
|
|
|
|
pub async fn upload(
|
|
&self,
|
|
serve_path: &str,
|
|
rest_path: Option<&str>,
|
|
file_name: &str,
|
|
data: &[u8],
|
|
) -> Result<()> {
|
|
let rest = format!("{}/{file_name}", rest_path.unwrap_or(""));
|
|
|
|
let path = build_path(serve_path, &self.path, Some(&rest));
|
|
|
|
write_file(path, data).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.strip_prefix("/").unwrap_or(rest));
|
|
}
|
|
|
|
final_path
|
|
} */
|