.
This commit is contained in:
parent
1f6d8a406b
commit
20fc177280
@ -2,24 +2,43 @@ use crate::admin::domain::*;
|
|||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
use serde_json::Value;
|
use serde_json::Value;
|
||||||
|
|
||||||
struct Repository {}
|
struct FileRepository {
|
||||||
|
directory_path: String,
|
||||||
|
}
|
||||||
|
|
||||||
impl Repository {}
|
impl FileRepository {
|
||||||
|
pub fn new(folder: String) -> Self {
|
||||||
|
FileRepository {
|
||||||
|
directory_path: folder,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl AdminRepository for Repository {
|
impl AdminRepository for FileRepository {
|
||||||
async fn info(&self, _: &RepositoryContext) -> RepositoryInfo {
|
async fn info(&self, _: &RepositoryContext) -> RepositoryInfo {
|
||||||
RepoInfo {
|
RepoInfo {
|
||||||
name: "My Empty Repository",
|
name: "File Repository",
|
||||||
lookup_key: "id",
|
lookup_key: "path",
|
||||||
display_list: &["id"],
|
display_list: &["path"],
|
||||||
fields: &[],
|
fields: &[
|
||||||
|
"path",
|
||||||
|
"size",
|
||||||
|
"modified_time",
|
||||||
|
"created_time",
|
||||||
|
"is_directory",
|
||||||
|
"is_file",
|
||||||
|
"is_symlink",
|
||||||
|
],
|
||||||
}
|
}
|
||||||
.into()
|
.into()
|
||||||
}
|
}
|
||||||
|
|
||||||
// GET on item collection.
|
// GET on item collection.
|
||||||
async fn list(&self, model: &RepositoryContext) -> RepositoryList {
|
async fn list(&self, model: &RepositoryContext) -> RepositoryList {
|
||||||
|
if let Some(file_data) = file_operations::directory_info(&self.directory_path) {
|
||||||
|
return RepositoryList::List(file_data);
|
||||||
|
}
|
||||||
RepositoryList::Empty
|
RepositoryList::Empty
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,3 +81,74 @@ impl AdminRepository for Repository {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn register(registry: &mut AdminRegistry, db: DatabaseConnection) {
|
||||||
|
let app_key = registry.register_app("Static");
|
||||||
|
let repo = FileRepository::new(db);
|
||||||
|
let model_config = AdminModelConfig {
|
||||||
|
app_key: app_key,
|
||||||
|
name: "User".to_owned(),
|
||||||
|
};
|
||||||
|
let model_result = registry.register_model(model_config, repo);
|
||||||
|
match model_result {
|
||||||
|
Err(err) => panic!("{}", err),
|
||||||
|
_ => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mod file_operations {
|
||||||
|
use serde_json::{json, Value};
|
||||||
|
use std::fs;
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
fn get_metadata_as_json(path: &Path) -> Result<Value, Box<dyn std::error::Error>> {
|
||||||
|
let metadata = fs::metadata(path)?;
|
||||||
|
|
||||||
|
// File size
|
||||||
|
let size = metadata.len();
|
||||||
|
|
||||||
|
// Modification and creation times (if available)
|
||||||
|
let modified_time = metadata.modified().ok();
|
||||||
|
let created_time = metadata.created().ok();
|
||||||
|
|
||||||
|
// File type information
|
||||||
|
let file_type = metadata.file_type();
|
||||||
|
let is_dir = file_type.is_dir();
|
||||||
|
let is_file = file_type.is_file();
|
||||||
|
let is_symlink = file_type.is_symlink();
|
||||||
|
|
||||||
|
// Format times as strings if they exist
|
||||||
|
let modified_time_str = modified_time.map(|t| t.to_string());
|
||||||
|
let created_time_str = created_time.map(|t| t.to_string());
|
||||||
|
|
||||||
|
Ok(json!({
|
||||||
|
"path": path.display().to_string(),
|
||||||
|
"size": size,
|
||||||
|
"modified_time": modified_time_str,
|
||||||
|
"created_time": created_time_str,
|
||||||
|
"is_directory": is_dir,
|
||||||
|
"is_file": is_file,
|
||||||
|
"is_symlink": is_symlink
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn file_info(file_path: &str) -> Result<Value, Box<dyn std::error::Error>> {
|
||||||
|
let path = Path::new(file_path);
|
||||||
|
get_metadata_as_json(path)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn directory_info(directory_path: &str) -> Result<Vec<Value>, Box<dyn std::error::Error>> {
|
||||||
|
let entries = fs::read_dir(directory_path)?;
|
||||||
|
let mut results = Vec::new();
|
||||||
|
|
||||||
|
for entry in entries {
|
||||||
|
let entry = entry?;
|
||||||
|
let path = entry.path();
|
||||||
|
if let Ok(metadata_json) = get_metadata_as_json(&path) {
|
||||||
|
results.push(metadata_json);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(results)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user