From 20fc177280259d1e21e7baa6cb3f6d5f8c482c60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabor=20K=C3=B6rber?= Date: Sun, 12 May 2024 09:06:55 +0200 Subject: [PATCH] . --- src/admin_examples/file_repository.rs | 104 ++++++++++++++++++++++++-- 1 file changed, 97 insertions(+), 7 deletions(-) diff --git a/src/admin_examples/file_repository.rs b/src/admin_examples/file_repository.rs index 114d1ec..517859e 100644 --- a/src/admin_examples/file_repository.rs +++ b/src/admin_examples/file_repository.rs @@ -2,24 +2,43 @@ use crate::admin::domain::*; use async_trait::async_trait; 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] -impl AdminRepository for Repository { +impl AdminRepository for FileRepository { async fn info(&self, _: &RepositoryContext) -> RepositoryInfo { RepoInfo { - name: "My Empty Repository", - lookup_key: "id", - display_list: &["id"], - fields: &[], + name: "File Repository", + lookup_key: "path", + display_list: &["path"], + fields: &[ + "path", + "size", + "modified_time", + "created_time", + "is_directory", + "is_file", + "is_symlink", + ], } .into() } // GET on item collection. 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 } @@ -62,3 +81,74 @@ impl AdminRepository for Repository { 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> { + 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> { + let path = Path::new(file_path); + get_metadata_as_json(path) + } + + fn directory_info(directory_path: &str) -> Result, Box> { + 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) + } +}