Add and prepare rust worker management system for file information processing and knowledge base framework
This commit is contained in:
parent
af82b71657
commit
da6ab3a782
12 changed files with 1402 additions and 251 deletions
34
rust-engine/src/storage.rs
Normal file
34
rust-engine/src/storage.rs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
use anyhow::Result;
|
||||
use std::fs;
|
||||
use std::io::Write;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
pub fn storage_dir() -> PathBuf {
|
||||
std::env::var("ASTRA_STORAGE")
|
||||
.map(PathBuf::from)
|
||||
.unwrap_or_else(|_| std::env::current_dir().unwrap().join("storage"))
|
||||
}
|
||||
|
||||
pub fn ensure_storage_dir() -> Result<()> {
|
||||
let dir = storage_dir();
|
||||
if !dir.exists() {
|
||||
fs::create_dir_all(&dir)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn save_file(filename: &str, contents: &[u8]) -> Result<PathBuf> {
|
||||
ensure_storage_dir()?;
|
||||
let mut path = storage_dir();
|
||||
path.push(filename);
|
||||
let mut f = fs::File::create(&path)?;
|
||||
f.write_all(contents)?;
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
pub fn delete_file(path: &Path) -> Result<()> {
|
||||
if path.exists() {
|
||||
fs::remove_file(path)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue